But xpi0t0s, each time the pointer top moves to the next node as we move down the stack
Like this:
Say initially my stack was
1 <- top
2
3
4
NULL
1 printed out...
Next...
2 <-top
3
4
NULL
2 printed out...
Next....
3 <- top
4
NULL
3 printed out...
Next...
4 <- top
NULL
So actually when top is pointing the first element i.e. 4....it is not NULL yet...and in the next step it moves to NULL....and the loop stops iteration.....by the way all the elements of the stack has been printed out...
So where is the error occuring?? Could you design a bit more conceptual questions for me??
|
Contributor
|
|
| 22Nov2008,20:22 | #11 |
|
Last edited by back from retirement; 22Nov2008 at 20:38.. |
|
Mentor
|
![]() |
| 23Nov2008,00:54 | #12 |
|
OK, so top has moved down the stack to point beyond the end.
How are future functions going to add to the stack, now that the original value of top has been overwritten? |
|
Contributor
|
|
| 23Nov2008,10:23 | #13 |
|
Since now top -> NULL, according to our push function, any node further inserted will be assigned to top....for example, if we enter the no. 5 now....
*top=5. Thus now the list looks like... 5 <- top NULL Is it correct conception??? |
|
Mentor
|
![]() |
| 24Nov2008,04:16 | #14 |
|
Well, it depends. Is the print function supposed to obliterate the list? If it is, why don't you free the nodes? Surely if you have a stack of 1,2,3,4, then you print it, then you push 5, should the stack then be just 5 or should it be 1,2,3,4,5?
There's definitely something wrong with your program so far. Either print wrongly obliterates the list, or you have a memory leak. But I don't know what your design is, principally what I don't know is whether or not printing the stack is supposed to obliterate it. Only you can tell me that; I would have said not, but it's not an assignment that was given to me. |
|
Contributor
|
|
| 26Nov2008,19:27 | #15 |
|
Certainly, the stack will be 1,2,3,4,5. But As I have presented it in the form of a function, whenever it is getting called, it is traversing the entire stack, printing out all its nodes...so printf is not obliterating the list, is it??
Please tell if there is mistakes in my conceptions....maybe, I'm not getting to the point...if so, then I am exttremely sorry sir... |
|
Mentor
|
![]() |
| 26Nov2008,20:28 | #16 |
|
You've made me wonder. Ran the code (after fixing some bugs):
Welcome Enter 1 for PUSH, 2 for POP, 3 for DISPLAY, 4 for EXIT 1 Enter the value to be pushed=1 Enter 1 for PUSH, 2 for POP, 3 for DISPLAY, 4 for EXIT 1 Enter the value to be pushed=2 Enter 1 for PUSH, 2 for POP, 3 for DISPLAY, 4 for EXIT 3 2 1 Enter 1 for PUSH, 2 for POP, 3 for DISPLAY, 4 for EXIT 1 Enter the value to be pushed=3 Enter 1 for PUSH, 2 for POP, 3 for DISPLAY, 4 for EXIT 3 3 So, yes, I was correct. The display function *does* obliterate the list. As you can see I push 1 and 2, then display it and 1,2 appear as expected, then I push 3 and display, and only 3 is displayed, NOT 1,2,3 as you are expecting. The code I have for display is, as originally posted: Code:
void display(node **t)
{
if(*t==NULL)
printf("Empty Stack\n");
else
{
while(*t!=NULL)
{
printf("%d\n", (*t)->data);
*t=(*t)->link;
}
}
}
|
|
Contributor
|
|
| 26Nov2008,20:40 | #17 |
|
Excuse me....but in my compiler it is showing for the first display...
Code:
2 1 Code:
General Protection Exception MYSTACK.C 46 MYSTACK(2) 0x23E7:0x00D0 Processor Fault |
|
Contributor
|
|
| 26Nov2008,20:49 | #18 |
|
I have seen your post in the other thread Sir....
So what I have to do is.... Code:
void display(node **top)
{
node *t=*top;
if(t==NULL)
printf("\nEmpty Stack\n");
else
{
while(t!=NULL)
{
printf("\n%d\n", t->data);
t=t->link;
}
}
}
|
|
Mentor
|
![]() |
| 26Nov2008,22:00 | #19 |
|
Yes, you could do it that way, or just use pass-by-value and pass the contents of top into the function.
Code:
void display(node *t)
{ // etc
|
|
Contributor
|
|
| 30Nov2008,21:10 | #20 |
|
Hello xpi0t0s....I have written the complete code...for the stack using linked list....
I used the techniques used by Shabbir for implementing doubly linked list... Here it is... Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
};
void init(node *top, int value)
{
node *newnode;
newnode=(node *)malloc(sizeof(node));
newnode->data=value;
top->next=newnode;
newnode->next=NULL;
}
void push(node *top, int value)
{
node *newnode;
newnode=(node *)malloc(sizeof(node));
newnode->data=value;
newnode->next=top->next;
top->next=newnode;
}
void pop(node *top)
{
node *del;
if(top->next==NULL)
printf("\nList is empty....nothing to delete\n");
else if((top->next)->next==NULL)
printf("\nYou cannot pop out the last node\n");
else
{
del=top->next;
top->next=(top->next)->next;
free(del);
}
}
void display(node *top)
{
node *move=top->next;
if(top->next==NULL)
printf("\nList is empty....nothing to display\n");
else
{
while(move->next!=NULL)
{
printf("\n%5d\n", move->data);
move=move->next;
}
printf("\n%5d\n", move->data);
}
}
void main()
{
node *top;
int i,n;
static int k=0;
char ch;
top=(node *)malloc(sizeof(node));
top->next=NULL;
printf("\nWelcome\n");
do
{
again:
printf("\nEnter 1 for INITIALISE, 2 for PUSH, 3 for POP, 4 for DISPLAY\t");
scanf("%d", &i);
if(k==0 && i!=1)
{
printf("\nYou must initialise first\n");
goto again;
}
else if(k==0 && i==1)
k=1;
else if(k==1 && i==1)
{
printf("\nInitialisation can occur only once....now you can push values\n");
goto again;
}
else
goto done;
done:
switch(i)
{
case 1:
{
printf("\nEnter the value you want to initialise=");
scanf("%d", &n);
init(top,n);
break;
}
case 2:
{
printf("\nEnter the value you want to push=");
scanf("%d", &n);
push(top,n);
break;
}
case 3:
{
pop(top);
break;
}
case 4:
{
display(top);
break;
}
default:
{
printf("\nERROR...please re-enter the code\n");
break;
}
}
printf("\nDo you wish to continue?[y/n]\t");
ch=getche();
printf("\n");
}while(ch=='y'||ch=='Y');
printf("\nThank you....press any key to exit\n");
getch();
}
|

