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
What are header files in c?
Is malloc memset faster than calloc?
What do you mean by dynamic memory allocation in c? What functions are used?
Which header file is essential for using strcmp function?
What does *p++ do? What does it point to?
What does the characters “r” and “w” mean when writing programs that will make use of files?
which of the following shows the correct hierarchy of arithmetic operations in C a) (), **, * or/,+ or - b) (),**,*,/,+,- c) (),**,/,*,+,- d) (),/ or *,- or +
What is scope rule of function in c?
Can 'this' pointer by used in the constructor?
Is javascript based on c?
What is clrscr ()?
Tell me when is a void pointer used?
What are local static variables?
Tell me is null always defined as 0(zero)?
in case any function return float value we must declare a) the function must be declared as 'float' in main() as well b) the function automatically returned float values c) function before declared 'float' keyword d) all the above