SlideShare a Scribd company logo
1 of 2
/* Task 2: Debugging a program with stacks, queues, and doubly-linked lists There are a number
of errors in the following program. All errors are located in main() and structure definitions.
Function declarations and definitions are correct! Locate all errors, fix them (as shown below),
run the program and save its output as a comment at the end of the source file. Example: int num
= 10; int *ptr; num = &ptr; // <== Error: Comment the line and write the correct line below //
Write a short justification where appropriate // num = &ptr; // Error #1 ptr = # Name: */ #include
#include #include #include #define DUMMY_TRAILER '177' // octal ASCII code of the // last
character in the ASCII table #define NUM_CITIES 10 typedef struct { char name[12]; int
temperature[5]; } CITY; // Stack and Queue Node typedef struct node NODE; struct node {
CITY city; node *next; }; // Doubly Linked List Node typedef struct d_node D_NODE; struct
d_node { CITY city; NODE *forw; NODE *back; }; // Stack Functions NODE *push(NODE
*stack, const CITY *pStu); NODE *pop(NODE **stack); // Queue Functions void
enqueue(NODE **queue, NODE **rear, const CITY *pStu); NODE *dequeue(NODE **queue,
NODE **rear); // Doubly Linked List Functions D_NODE *init_list(void); int insert(D_NODE
*list, const CITY *pStu); void traverse_forw(D_NODE *list); void traverse_back(D_NODE
*list); // Other Functions void printCity(const CITY *pCity); int main (void) { CITY
cList[NUM_CITIES] = { {"Cupertino", {88, 89, 87, 85, 89}}, {"Flagstaff", {81, 80, 88, 89,
89}}, {"Los Angeles", {87, 88, 89, 89, 90}}, {"Philadelphia", {96, 99, 99, 90, 95}}, {"Phoenix",
{106, 109, 109, 100, 105}}, {"Portland", {89, 90, 85, 89, 90}}, {"Reno", {108, 105, 109, 100,
108}}, {"Salem", {85, 90, 85, 89, 90}}, {"Tucson", {107, 100, 109, 100, 108}}, {"Yreka", {101,
109, 100, 108, 109}} }; NODE *stack = NULL; NODE *top = NULL; NODE *queue = NULL,
*rear = NULL; NODE *front; D_NODE *list; list = init_list(); // build stack and queue with data
from an array of CITY structures srand((unsigned int)time(NULL)); int count = rand() % 10; for
( int n = 0; n < count; n++) { int i = rand() % NUM_CITIES; int duplicate = insert(list,
&cList[i]); if(duplicate) { // already in the list! push(stack, &cList[i]); enqueue(&queue, &rear,
cList[i]); } } // display list printf("nLIST contents (forwards):n"); traverse_forw(list);
printf("nLIST contents (backwards):n"); traverse_back(list); // display stack if (top) {
printf("nSTACK contents from top to bottom:n"); while ((top = pop(stack))) { printCity(top-
>city); } } else printf ("Empty Stack!n"); // display queue if (front) { printf("nQUEUE contents
from front to rear:n"); while ((front = dequeue( queue, rear))) { printCity(front->city); } } else
printf ("Empty Queue!n"); return 0; }
/*************************************************** Displays the fileds of a
CIS_CLASS structure Pre pCls - a pointer to a CIS_CLASS structure Post */ void
printCity(const CITY *pCity) { printf("%-20s %3dn", pCity->name, pCity->temperature[0]); }
// Stack Functions /*************************************************** Stack Insert:
insert in the beginning */ NODE *push(NODE *stack, const CITY *pCity) { NODE *pnew;
pnew = (NODE *) malloc(sizeof (NODE)); if (!pnew) { printf("... error in push!n"); exit(1); }
pnew->city = *pCity; pnew->next = stack; stack = pnew; return stack; }
/*************************************************** Stack Delete: delete the first
node */ NODE *pop(NODE **stack) { NODE *first; if (*stack == NULL) return NULL; first =
*stack; *stack = (*stack)->next; first->next = NULL; return first; } // Queue Functions
/*************************************************** Queue Insert: insert at the end
*/ void enqueue(NODE **queue, NODE **rear, const CITY *pCity) { NODE *pnew; pnew =
(NODE *) malloc(sizeof (NODE)); if (!pnew) { printf("... error in enqueue!n"); exit(1); } pnew-
>city = *pCity; pnew->next = NULL; if (*queue == NULL) *queue = pnew; else (*rear)->next =
pnew; *rear = pnew; return; } /***************************************************
Queue Delete: remove the first node */ NODE *dequeue(NODE **queue, NODE **rear) {
NODE *first; if (*queue == NULL) return NULL; first = *queue; *queue = (*queue)->next; if
(*queue == NULL) *rear = NULL; first->next = NULL; return first; } // Doubly Linked List
Functions /*************************************************** Initialization of a
circularly doubly-linked list with one sentinel node */ D_NODE *init_list(void) { D_NODE
*list; // allocate the sentinel node list = (D_NODE *) malloc(sizeof (D_NODE)); if (!list) {
printf("Error in init_list!n"); exit(1); } list->city.name[0] = DUMMY_TRAILER; list-
>city.name[1] = '0'; list->forw = list; list->back = list; return list; }
/*************************************************** Insert a node in a sorted
circularly doubly-linked list with one sentinel node return 1 - if duplicate return 0 - otherwise */
int insert(D_NODE *list, const CITY *data) { D_NODE *curr = list->forw; D_NODE *prev =
list; D_NODE *pnew; int duplicate = 1; // search while (strcmp(data->name, curr->city.name) >
0) { prev = curr; curr = curr->forw; } if (strcmp(data->name, curr->city.name) ) { duplicate = 0;
// not a duplicate pnew = (D_NODE *) malloc(sizeof (D_NODE)); if (!pnew) { printf("Fatal
memory allocation error in insert!n"); exit(3); } pnew->city = *data; pnew->forw = curr; pnew-
>back = prev; prev->forw = pnew; curr->back = pnew; } return duplicate; }
/*************************************************** Traverses forward a circularly
doubly-linked list with one sentinel node to print out the contents of each node */ void
traverse_forw(D_NODE *list) { list = list->forw; // skip the dummy node while (list-
>city.name[0] != DUMMY_TRAILER) { printCity(&list->city); list = list->forw; } }
/*************************************************** Traverses backward a circularly
doubly-linked list with one sentinel node to print out the contents of each node */ void
traverse_back(D_NODE *list) { list = list->back; // skip the dummy node while (list-
>city.name[0] != DUMMY_TRAILER) { printCity(&list->city); list = list->back; } } /*
================= Sample Output #1 ================= */ /* LIST contents
(forwards): Cupertino 88 Los Angeles 87 Philadelphia 96 Phoenix 106 Reno 108 Tucson 107
LIST contents (backwards): Tucson 107 Reno 108 Phoenix 106 Philadelphia 96 Los Angeles 87
Cupertino 88 STACK contents from top to bottom: Tucson 107 Philadelphia 96 QUEUE
contents from front to rear: Philadelphia 96 Tucson 107 */ /* ================= Sample
Output #2 ================= */ /* LIST contents (forwards): Flagstaff 81 Philadelphia 96
Yreka 101 LIST contents (backwards): Yreka 101 Philadelphia 96 Flagstaff 81 Empty Stack!
Empty Queue! */

More Related Content

Similar to -- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx

#include iostream #include fstream #include cstdlib #.pdf
 #include iostream #include fstream #include cstdlib #.pdf #include iostream #include fstream #include cstdlib #.pdf
#include iostream #include fstream #include cstdlib #.pdfannesmkt
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdfanujmkt
 
Implementing Software Machines in Go and C
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and CEleanor McHugh
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdffeelinggift
 
solution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdfsolution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdfbrijmote
 
Data StructuresPLEASE USING THIS C++ PROGRAM BELOW, I NEED HEL.pdf
Data StructuresPLEASE USING THIS C++ PROGRAM BELOW, I NEED HEL.pdfData StructuresPLEASE USING THIS C++ PROGRAM BELOW, I NEED HEL.pdf
Data StructuresPLEASE USING THIS C++ PROGRAM BELOW, I NEED HEL.pdfrozakashif85
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfmichardsonkhaicarr37
 
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdfapleather
 
M 0 1 2 3 4 5 6 7 0.pdf
 M  0  1  2  3  4  5  6  7    0.pdf M  0  1  2  3  4  5  6  7    0.pdf
M 0 1 2 3 4 5 6 7 0.pdfajay1317
 
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfPlease do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfaioils
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxgordienaysmythe
 
c++ project on restaurant billing
c++ project on restaurant billing c++ project on restaurant billing
c++ project on restaurant billing Swakriti Rathore
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxteyaj1
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocationNaveen Gupta
 
calc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docx
calc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docxcalc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docx
calc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docxRAHUL126667
 

Similar to -- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx (20)

#include iostream #include fstream #include cstdlib #.pdf
 #include iostream #include fstream #include cstdlib #.pdf #include iostream #include fstream #include cstdlib #.pdf
#include iostream #include fstream #include cstdlib #.pdf
 
Program of sorting using shell sort #include stdio.h #de.pdf
 Program of sorting using shell sort  #include stdio.h #de.pdf Program of sorting using shell sort  #include stdio.h #de.pdf
Program of sorting using shell sort #include stdio.h #de.pdf
 
DS Code (CWH).docx
DS Code (CWH).docxDS Code (CWH).docx
DS Code (CWH).docx
 
Implementing Software Machines in Go and C
Implementing Software Machines in Go and CImplementing Software Machines in Go and C
Implementing Software Machines in Go and C
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
solution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdfsolution in c++program Program to implement a queue using two .pdf
solution in c++program Program to implement a queue using two .pdf
 
Linked lists
Linked listsLinked lists
Linked lists
 
Data StructuresPLEASE USING THIS C++ PROGRAM BELOW, I NEED HEL.pdf
Data StructuresPLEASE USING THIS C++ PROGRAM BELOW, I NEED HEL.pdfData StructuresPLEASE USING THIS C++ PROGRAM BELOW, I NEED HEL.pdf
Data StructuresPLEASE USING THIS C++ PROGRAM BELOW, I NEED HEL.pdf
 
C program
C programC program
C program
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
 
M 0 1 2 3 4 5 6 7 0.pdf
 M  0  1  2  3  4  5  6  7    0.pdf M  0  1  2  3  4  5  6  7    0.pdf
M 0 1 2 3 4 5 6 7 0.pdf
 
week-16x
week-16xweek-16x
week-16x
 
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdfPlease do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
Please do Part A, Ill be really gratefulThe main.c is the skeleto.pdf
 
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docxcmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
cmdfile.txtsleep 5ls -latrsleep 3pwdsleep 1wc .docx
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
c++ project on restaurant billing
c++ project on restaurant billing c++ project on restaurant billing
c++ project on restaurant billing
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
Dynamic memory allocation
Dynamic memory allocationDynamic memory allocation
Dynamic memory allocation
 
calc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docx
calc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docxcalc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docx
calc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docx
 

More from Adamq0DJonese

1) What are the five tenets of wireless LAN (802-11) troubleshooting- (1).docx
1) What are the five tenets of wireless LAN (802-11) troubleshooting- (1).docx1) What are the five tenets of wireless LAN (802-11) troubleshooting- (1).docx
1) What are the five tenets of wireless LAN (802-11) troubleshooting- (1).docxAdamq0DJonese
 
1) Water soluble compounds have lower bioconcentration factors than do.docx
1) Water soluble compounds have lower bioconcentration factors than do.docx1) Water soluble compounds have lower bioconcentration factors than do.docx
1) Water soluble compounds have lower bioconcentration factors than do.docxAdamq0DJonese
 
1) Spatial Concepts of Location- Direction and Distance (absolute and.docx
1) Spatial Concepts of Location- Direction and Distance (absolute and.docx1) Spatial Concepts of Location- Direction and Distance (absolute and.docx
1) Spatial Concepts of Location- Direction and Distance (absolute and.docxAdamq0DJonese
 
1) How are DNA and RNA similar and how are they different- 2) What are.docx
1) How are DNA and RNA similar and how are they different- 2) What are.docx1) How are DNA and RNA similar and how are they different- 2) What are.docx
1) How are DNA and RNA similar and how are they different- 2) What are.docxAdamq0DJonese
 
1) Identify the following parts of a flower-2) Identify these flowers.docx
1) Identify the following parts of a flower-2) Identify these flowers.docx1) Identify the following parts of a flower-2) Identify these flowers.docx
1) Identify the following parts of a flower-2) Identify these flowers.docxAdamq0DJonese
 
1) design a clocked latch version using NOR gates2) Design the version.docx
1) design a clocked latch version using NOR gates2) Design the version.docx1) design a clocked latch version using NOR gates2) Design the version.docx
1) design a clocked latch version using NOR gates2) Design the version.docxAdamq0DJonese
 
1) Given the following program- Count -0 While (Count !- 5)- Count - C.docx
1) Given the following program- Count -0 While (Count !- 5)- Count - C.docx1) Given the following program- Count -0 While (Count !- 5)- Count - C.docx
1) Given the following program- Count -0 While (Count !- 5)- Count - C.docxAdamq0DJonese
 
1) Explain at least two different things that can happen when a meteor.docx
1) Explain at least two different things that can happen when a meteor.docx1) Explain at least two different things that can happen when a meteor.docx
1) Explain at least two different things that can happen when a meteor.docxAdamq0DJonese
 
1) Assume that you measure the OD of two E-coli cultures- culture A an.docx
1) Assume that you measure the OD of two E-coli cultures- culture A an.docx1) Assume that you measure the OD of two E-coli cultures- culture A an.docx
1) Assume that you measure the OD of two E-coli cultures- culture A an.docxAdamq0DJonese
 
1) Define filtration- phagocytosis- pinocytosis- endocytosis and exocy.docx
1) Define filtration- phagocytosis- pinocytosis- endocytosis and exocy.docx1) Define filtration- phagocytosis- pinocytosis- endocytosis and exocy.docx
1) Define filtration- phagocytosis- pinocytosis- endocytosis and exocy.docxAdamq0DJonese
 
1) A pair of fair dice were tossed- Assuming X represents the sum of t.docx
1) A pair of fair dice were tossed- Assuming X represents the sum of t.docx1) A pair of fair dice were tossed- Assuming X represents the sum of t.docx
1) A pair of fair dice were tossed- Assuming X represents the sum of t.docxAdamq0DJonese
 
1 What are the advantages and disadvantages of globalization- Give exa.docx
1 What are the advantages and disadvantages of globalization- Give exa.docx1 What are the advantages and disadvantages of globalization- Give exa.docx
1 What are the advantages and disadvantages of globalization- Give exa.docxAdamq0DJonese
 
1 point- Regarding chronic disease- which of the following statements.docx
1 point- Regarding chronic disease- which of the following statements.docx1 point- Regarding chronic disease- which of the following statements.docx
1 point- Regarding chronic disease- which of the following statements.docxAdamq0DJonese
 
1 point Which of the following is an example of a Type I error- An inn.docx
1 point Which of the following is an example of a Type I error- An inn.docx1 point Which of the following is an example of a Type I error- An inn.docx
1 point Which of the following is an example of a Type I error- An inn.docxAdamq0DJonese
 
1 Elonis v- United States- is the main case on 875(c)- What is the men.docx
1 Elonis v- United States- is the main case on 875(c)- What is the men.docx1 Elonis v- United States- is the main case on 875(c)- What is the men.docx
1 Elonis v- United States- is the main case on 875(c)- What is the men.docxAdamq0DJonese
 
1 1- A government wants to provide student loans to students in their.docx
1 1- A government wants to provide student loans to students in their.docx1 1- A government wants to provide student loans to students in their.docx
1 1- A government wants to provide student loans to students in their.docxAdamq0DJonese
 
1 a- The analysis of sedimentary rock determines- The size and s.docx
1 a- The analysis of sedimentary rock determines-       The size and s.docx1 a- The analysis of sedimentary rock determines-       The size and s.docx
1 a- The analysis of sedimentary rock determines- The size and s.docxAdamq0DJonese
 
0 points) Answer the questions on ListReferenceBased class (NOTE that.docx
0 points) Answer the questions on ListReferenceBased class (NOTE that.docx0 points) Answer the questions on ListReferenceBased class (NOTE that.docx
0 points) Answer the questions on ListReferenceBased class (NOTE that.docxAdamq0DJonese
 
0 Conditional Format as Cell Cells Formatting - Table - Styles - Style.docx
0 Conditional Format as Cell Cells Formatting - Table - Styles - Style.docx0 Conditional Format as Cell Cells Formatting - Table - Styles - Style.docx
0 Conditional Format as Cell Cells Formatting - Table - Styles - Style.docxAdamq0DJonese
 
-Pyro-Diversity- means the many species that grow back after a fire re.docx
-Pyro-Diversity- means the many species that grow back after a fire re.docx-Pyro-Diversity- means the many species that grow back after a fire re.docx
-Pyro-Diversity- means the many species that grow back after a fire re.docxAdamq0DJonese
 

More from Adamq0DJonese (20)

1) What are the five tenets of wireless LAN (802-11) troubleshooting- (1).docx
1) What are the five tenets of wireless LAN (802-11) troubleshooting- (1).docx1) What are the five tenets of wireless LAN (802-11) troubleshooting- (1).docx
1) What are the five tenets of wireless LAN (802-11) troubleshooting- (1).docx
 
1) Water soluble compounds have lower bioconcentration factors than do.docx
1) Water soluble compounds have lower bioconcentration factors than do.docx1) Water soluble compounds have lower bioconcentration factors than do.docx
1) Water soluble compounds have lower bioconcentration factors than do.docx
 
1) Spatial Concepts of Location- Direction and Distance (absolute and.docx
1) Spatial Concepts of Location- Direction and Distance (absolute and.docx1) Spatial Concepts of Location- Direction and Distance (absolute and.docx
1) Spatial Concepts of Location- Direction and Distance (absolute and.docx
 
1) How are DNA and RNA similar and how are they different- 2) What are.docx
1) How are DNA and RNA similar and how are they different- 2) What are.docx1) How are DNA and RNA similar and how are they different- 2) What are.docx
1) How are DNA and RNA similar and how are they different- 2) What are.docx
 
1) Identify the following parts of a flower-2) Identify these flowers.docx
1) Identify the following parts of a flower-2) Identify these flowers.docx1) Identify the following parts of a flower-2) Identify these flowers.docx
1) Identify the following parts of a flower-2) Identify these flowers.docx
 
1) design a clocked latch version using NOR gates2) Design the version.docx
1) design a clocked latch version using NOR gates2) Design the version.docx1) design a clocked latch version using NOR gates2) Design the version.docx
1) design a clocked latch version using NOR gates2) Design the version.docx
 
1) Given the following program- Count -0 While (Count !- 5)- Count - C.docx
1) Given the following program- Count -0 While (Count !- 5)- Count - C.docx1) Given the following program- Count -0 While (Count !- 5)- Count - C.docx
1) Given the following program- Count -0 While (Count !- 5)- Count - C.docx
 
1) Explain at least two different things that can happen when a meteor.docx
1) Explain at least two different things that can happen when a meteor.docx1) Explain at least two different things that can happen when a meteor.docx
1) Explain at least two different things that can happen when a meteor.docx
 
1) Assume that you measure the OD of two E-coli cultures- culture A an.docx
1) Assume that you measure the OD of two E-coli cultures- culture A an.docx1) Assume that you measure the OD of two E-coli cultures- culture A an.docx
1) Assume that you measure the OD of two E-coli cultures- culture A an.docx
 
1) Define filtration- phagocytosis- pinocytosis- endocytosis and exocy.docx
1) Define filtration- phagocytosis- pinocytosis- endocytosis and exocy.docx1) Define filtration- phagocytosis- pinocytosis- endocytosis and exocy.docx
1) Define filtration- phagocytosis- pinocytosis- endocytosis and exocy.docx
 
1) A pair of fair dice were tossed- Assuming X represents the sum of t.docx
1) A pair of fair dice were tossed- Assuming X represents the sum of t.docx1) A pair of fair dice were tossed- Assuming X represents the sum of t.docx
1) A pair of fair dice were tossed- Assuming X represents the sum of t.docx
 
1 What are the advantages and disadvantages of globalization- Give exa.docx
1 What are the advantages and disadvantages of globalization- Give exa.docx1 What are the advantages and disadvantages of globalization- Give exa.docx
1 What are the advantages and disadvantages of globalization- Give exa.docx
 
1 point- Regarding chronic disease- which of the following statements.docx
1 point- Regarding chronic disease- which of the following statements.docx1 point- Regarding chronic disease- which of the following statements.docx
1 point- Regarding chronic disease- which of the following statements.docx
 
1 point Which of the following is an example of a Type I error- An inn.docx
1 point Which of the following is an example of a Type I error- An inn.docx1 point Which of the following is an example of a Type I error- An inn.docx
1 point Which of the following is an example of a Type I error- An inn.docx
 
1 Elonis v- United States- is the main case on 875(c)- What is the men.docx
1 Elonis v- United States- is the main case on 875(c)- What is the men.docx1 Elonis v- United States- is the main case on 875(c)- What is the men.docx
1 Elonis v- United States- is the main case on 875(c)- What is the men.docx
 
1 1- A government wants to provide student loans to students in their.docx
1 1- A government wants to provide student loans to students in their.docx1 1- A government wants to provide student loans to students in their.docx
1 1- A government wants to provide student loans to students in their.docx
 
1 a- The analysis of sedimentary rock determines- The size and s.docx
1 a- The analysis of sedimentary rock determines-       The size and s.docx1 a- The analysis of sedimentary rock determines-       The size and s.docx
1 a- The analysis of sedimentary rock determines- The size and s.docx
 
0 points) Answer the questions on ListReferenceBased class (NOTE that.docx
0 points) Answer the questions on ListReferenceBased class (NOTE that.docx0 points) Answer the questions on ListReferenceBased class (NOTE that.docx
0 points) Answer the questions on ListReferenceBased class (NOTE that.docx
 
0 Conditional Format as Cell Cells Formatting - Table - Styles - Style.docx
0 Conditional Format as Cell Cells Formatting - Table - Styles - Style.docx0 Conditional Format as Cell Cells Formatting - Table - Styles - Style.docx
0 Conditional Format as Cell Cells Formatting - Table - Styles - Style.docx
 
-Pyro-Diversity- means the many species that grow back after a fire re.docx
-Pyro-Diversity- means the many species that grow back after a fire re.docx-Pyro-Diversity- means the many species that grow back after a fire re.docx
-Pyro-Diversity- means the many species that grow back after a fire re.docx
 

Recently uploaded

Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 

Recently uploaded (20)

Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 

-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx

  • 1. /* Task 2: Debugging a program with stacks, queues, and doubly-linked lists There are a number of errors in the following program. All errors are located in main() and structure definitions. Function declarations and definitions are correct! Locate all errors, fix them (as shown below), run the program and save its output as a comment at the end of the source file. Example: int num = 10; int *ptr; num = &ptr; // <== Error: Comment the line and write the correct line below // Write a short justification where appropriate // num = &ptr; // Error #1 ptr = # Name: */ #include #include #include #include #define DUMMY_TRAILER '177' // octal ASCII code of the // last character in the ASCII table #define NUM_CITIES 10 typedef struct { char name[12]; int temperature[5]; } CITY; // Stack and Queue Node typedef struct node NODE; struct node { CITY city; node *next; }; // Doubly Linked List Node typedef struct d_node D_NODE; struct d_node { CITY city; NODE *forw; NODE *back; }; // Stack Functions NODE *push(NODE *stack, const CITY *pStu); NODE *pop(NODE **stack); // Queue Functions void enqueue(NODE **queue, NODE **rear, const CITY *pStu); NODE *dequeue(NODE **queue, NODE **rear); // Doubly Linked List Functions D_NODE *init_list(void); int insert(D_NODE *list, const CITY *pStu); void traverse_forw(D_NODE *list); void traverse_back(D_NODE *list); // Other Functions void printCity(const CITY *pCity); int main (void) { CITY cList[NUM_CITIES] = { {"Cupertino", {88, 89, 87, 85, 89}}, {"Flagstaff", {81, 80, 88, 89, 89}}, {"Los Angeles", {87, 88, 89, 89, 90}}, {"Philadelphia", {96, 99, 99, 90, 95}}, {"Phoenix", {106, 109, 109, 100, 105}}, {"Portland", {89, 90, 85, 89, 90}}, {"Reno", {108, 105, 109, 100, 108}}, {"Salem", {85, 90, 85, 89, 90}}, {"Tucson", {107, 100, 109, 100, 108}}, {"Yreka", {101, 109, 100, 108, 109}} }; NODE *stack = NULL; NODE *top = NULL; NODE *queue = NULL, *rear = NULL; NODE *front; D_NODE *list; list = init_list(); // build stack and queue with data from an array of CITY structures srand((unsigned int)time(NULL)); int count = rand() % 10; for ( int n = 0; n < count; n++) { int i = rand() % NUM_CITIES; int duplicate = insert(list, &cList[i]); if(duplicate) { // already in the list! push(stack, &cList[i]); enqueue(&queue, &rear, cList[i]); } } // display list printf("nLIST contents (forwards):n"); traverse_forw(list); printf("nLIST contents (backwards):n"); traverse_back(list); // display stack if (top) { printf("nSTACK contents from top to bottom:n"); while ((top = pop(stack))) { printCity(top- >city); } } else printf ("Empty Stack!n"); // display queue if (front) { printf("nQUEUE contents from front to rear:n"); while ((front = dequeue( queue, rear))) { printCity(front->city); } } else printf ("Empty Queue!n"); return 0; } /*************************************************** Displays the fileds of a CIS_CLASS structure Pre pCls - a pointer to a CIS_CLASS structure Post */ void printCity(const CITY *pCity) { printf("%-20s %3dn", pCity->name, pCity->temperature[0]); } // Stack Functions /*************************************************** Stack Insert: insert in the beginning */ NODE *push(NODE *stack, const CITY *pCity) { NODE *pnew; pnew = (NODE *) malloc(sizeof (NODE)); if (!pnew) { printf("... error in push!n"); exit(1); } pnew->city = *pCity; pnew->next = stack; stack = pnew; return stack; } /*************************************************** Stack Delete: delete the first node */ NODE *pop(NODE **stack) { NODE *first; if (*stack == NULL) return NULL; first = *stack; *stack = (*stack)->next; first->next = NULL; return first; } // Queue Functions /*************************************************** Queue Insert: insert at the end */ void enqueue(NODE **queue, NODE **rear, const CITY *pCity) { NODE *pnew; pnew = (NODE *) malloc(sizeof (NODE)); if (!pnew) { printf("... error in enqueue!n"); exit(1); } pnew- >city = *pCity; pnew->next = NULL; if (*queue == NULL) *queue = pnew; else (*rear)->next = pnew; *rear = pnew; return; } /***************************************************
  • 2. Queue Delete: remove the first node */ NODE *dequeue(NODE **queue, NODE **rear) { NODE *first; if (*queue == NULL) return NULL; first = *queue; *queue = (*queue)->next; if (*queue == NULL) *rear = NULL; first->next = NULL; return first; } // Doubly Linked List Functions /*************************************************** Initialization of a circularly doubly-linked list with one sentinel node */ D_NODE *init_list(void) { D_NODE *list; // allocate the sentinel node list = (D_NODE *) malloc(sizeof (D_NODE)); if (!list) { printf("Error in init_list!n"); exit(1); } list->city.name[0] = DUMMY_TRAILER; list- >city.name[1] = '0'; list->forw = list; list->back = list; return list; } /*************************************************** Insert a node in a sorted circularly doubly-linked list with one sentinel node return 1 - if duplicate return 0 - otherwise */ int insert(D_NODE *list, const CITY *data) { D_NODE *curr = list->forw; D_NODE *prev = list; D_NODE *pnew; int duplicate = 1; // search while (strcmp(data->name, curr->city.name) > 0) { prev = curr; curr = curr->forw; } if (strcmp(data->name, curr->city.name) ) { duplicate = 0; // not a duplicate pnew = (D_NODE *) malloc(sizeof (D_NODE)); if (!pnew) { printf("Fatal memory allocation error in insert!n"); exit(3); } pnew->city = *data; pnew->forw = curr; pnew- >back = prev; prev->forw = pnew; curr->back = pnew; } return duplicate; } /*************************************************** Traverses forward a circularly doubly-linked list with one sentinel node to print out the contents of each node */ void traverse_forw(D_NODE *list) { list = list->forw; // skip the dummy node while (list- >city.name[0] != DUMMY_TRAILER) { printCity(&list->city); list = list->forw; } } /*************************************************** Traverses backward a circularly doubly-linked list with one sentinel node to print out the contents of each node */ void traverse_back(D_NODE *list) { list = list->back; // skip the dummy node while (list- >city.name[0] != DUMMY_TRAILER) { printCity(&list->city); list = list->back; } } /* ================= Sample Output #1 ================= */ /* LIST contents (forwards): Cupertino 88 Los Angeles 87 Philadelphia 96 Phoenix 106 Reno 108 Tucson 107 LIST contents (backwards): Tucson 107 Reno 108 Phoenix 106 Philadelphia 96 Los Angeles 87 Cupertino 88 STACK contents from top to bottom: Tucson 107 Philadelphia 96 QUEUE contents from front to rear: Philadelphia 96 Tucson 107 */ /* ================= Sample Output #2 ================= */ /* LIST contents (forwards): Flagstaff 81 Philadelphia 96 Yreka 101 LIST contents (backwards): Yreka 101 Philadelphia 96 Flagstaff 81 Empty Stack! Empty Queue! */