C program to perform stack operation using singly linked list
Answer Posted / bin fang
the above has bugs! for example, the count is not
decremented in Pop function...
I rewrote the Push and Pop code as follows:
node *cur = NULL;
node *head = NULL;
void Push(int info)
{
node *new;
new = (node *)malloc(sizeof(node));
new->data = info;
new->next = NULL;
if (head == NULL)
head = new;
else
cur->next = new;
cur = new;
count++;
}
void Pop(void)
{
node *pre = NULL;
node *temp = head;
while (temp != cur) {
pre = temp;
temp = temp->next;
}
printf("\n\tNode (%d) is deleted.", cur->data);
free(cur);
count--;
cur = pre;
if (cur)
cur->next = NULL;
else
head = NULL;
}
| Is This Answer Correct ? | 10 Yes | 1 No |
Post New Answer View All Answers
Explain the difference between getch() and getche() in c?
How can you restore a redirected standard stream?
How can I open files mentioned on the command line, and parse option flags?
What do you mean by dynamic memory allocation in c?
What does do in c?
What is derived datatype in c?
What is structure padding and packing in c?
Here is a neat trick for checking whether two strings are equal
What is the purpose of the following code? Is there any problem with the code? void send(int count, short *to, short *from) { /* count > 0 assumed */ register n = (count + 7) / 8; switch (count % 8) { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while (--n > 0); } }
What are the various types of control structures in programming?
Write a program to know whether the input number is an armstrong number.
What are the two types of functions in c?
`write a program to display the recomended action depends on a color of trafic light using nested if statments
Where register variables are stored in c?
Explain About fork()?