2016-03-27 7 views
0

Ich war hier in C. einfache Stack-Implementierung versucht ist:mit NULL in C Gleichsetzen

1 #include "stdio.h" 
2 #include "limits.h" 
3 #include "malloc.h" 
4  
5 typedef struct StackEntry StackEntry; 
6  
7 struct StackEntry 
8 { 
9  int data; 
10  StackEntry *next; 
11 }; 
12  
13 StackEntry* createStack(){ 
14  return NULL; 
15 } 
16  
17 int isEmptyStack(StackEntry **pStackTop) 
18 { 
19  return *pStackTop == NULL; 
20 } 
21  
22 void push(StackEntry **pStackTop, int pData) 
23 { 
24  StackEntry *lTempStackEntry; 
25  lTempStackEntry = (StackEntry*)malloc(sizeof(StackEntry)); 
26  
27  if(!lTempStackEntry) 
28   return; 
29  
30  lTempStackEntry->data = pData; 
31  lTempStackEntry->next = *pStackTop; 
32  
33  *pStackTop = lTempStackEntry; 
34 } 
35  
36 int pop(StackEntry **pStackTop) 
37 { 
38  int lTempData; 
39  StackEntry *lTempStackEntry; 
40  
41  if(isEmptyStack(pStackTop)) 
42   return INT_MIN; 
43  
44  lTempStackEntry = *pStackTop; 
45  *pStackTop = (*pStackTop)->next; 
46  lTempData = lTempStackEntry->data; 
47  free(lTempStackEntry); 
48  
49  return lTempData; 
50 } 
51  
52 int stackTop(StackEntry **pStackTop) 
53 { 
54  if(isEmptyStack(pStackTop)) 
55   return INT_MIN; 
56  
57  return (*pStackTop)->data; 
58 } 
59  
60 void deleteStack(StackEntry ** pStackTop) 
61 { 
62  StackEntry *secondTopNode, *topNode; 
63  topNode = *pStackTop; 
64  /* 
65   * In this we free all the nodes from second top to bottom, 
66   * by one by one attaching them to top->next. At the end 
67   * we free the top node. 
68   */ 
69  while(topNode->next) 
70  { 
71   secondTopNode = topNode->next; 
72   topNode->next = secondTopNode->next; //make third top node 
73             //second top node 
74   free(secondTopNode); 
75   secondTopNode = NULL; 
76  } 
77  free(topNode); 
78  topNode = NULL; 
79 } 
80  
81 int main(void) { 
82  // your code goes here 
83  StackEntry *stack = createStack(); //stack: 0x0 
84  printf("\n push 1"); 
85  push(&stack,1); 
86  printf("\n push 2"); 
87  push(&stack,2); 
88  printf("\n push 3"); 
89  push(&stack,3); 
90  printf("\n stack top: %d", stackTop(&stack)); 
91  printf("\n pop: %d ",pop(&stack)); 
92  printf("\n stack top: %d", stackTop(&stack)); 
93  printf("\n pop: %d ",pop(&stack)); 
94  printf("\n stack top: %d", stackTop(&stack)); 
95  
96  deleteStack(&stack); 
97  printf("\n stack deleted."); 
98  printf("\n is stack empty: %d", isEmptyStack(&stack)); //here it should print 1, but its printing 0 
99  
100  printf("\n push 1"); 
101  push(&stack,1); 
102  printf("\n push 2"); 
103  push(&stack,2); 
104  
105  printf("\n pop: %d ",pop(&stack)); 
106  printf("\n pop: %d ",pop(&stack)); 
107  return 0; 
108 } 

Ausgabe

push 1 
push 2 
push 3 
stack top: 3 
pop: 3 
stack top: 2 
pop: 2 
stack top: 1 
stack deleted. 
is stack empty: 0 
push 1 
push 2 
pop: 2 
pop: 1 

In deleteStack(), befreie ich alle im Stapel Eintrag und legen Sie den Stapel nach oben auf NULL. Überprüfen Sie in , ob der Stapel oben leer ist. Also sollte es 1 auswerten und Zeile 98 sollte 1 drucken (d. H. Wahr), aber es wird 0 ausgegeben. Was ist hier falsch? Here is the code on ideone.

das Debugging Ich nahm:

enter image description here

+1

Was ist hier falsch? Nun, du scheinst kein Debugging gemacht zu haben. –

+1

Vielen Dank für Ihre Kommentare, es ist nicht, dass ich nicht wissen, wie zu debuggen, es ist die Tatsache, dass ** mir nicht bewusst, dass ich "NULL" durch den Doppelzeiger ** setzen muss, wie von Mohit Jain in seinem gezeigt Antworten. Das liegt an mangelnder Erfahrung in der Zusammenarbeit mit C. Jedenfalls habe ich einen Screenshot meiner Debugging-Sitzung als Beweis dafür hinzugefügt, dass ich weiß, wie man debuggt. Wenn das zufriedenstellend war, können Sie bitte die Stimmen zurückziehen. Ich wusste vorher, dass ich etwas vermisste und das Debugging-Screenshot würde nichts bringen. Also, bevor ich den Debugging-Screenshot einsetzte, fing Mohit meinen Fehler ein. – Mahesha999

+0

Wenn Sie Ihren Code korrigiert haben, möchten Sie vielleicht auch http://codereview.stackexchange.com verwenden, da es einige Anfängerfehler und Stilprobleme gibt. –

Antwort

2

In Funktion deleteStack, Sie Stapel nicht gesetzt, anstatt eine Kopie des Stapelzeiger auf null auf null.

Was Sie tun, ist:

topNode = *pStackTop; 
topNode = NULL; 

die nicht korrekt ist.

Was sollten Sie stattdessen tun ist:

*pStackTop = NULL;