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


Please Help Members By Posting Answers For Below Questions

c language supports bitwise operations, why a) 'c' language is system oriented b) 'c' language is problem oriented c) 'c' language is middle level language d) all the above

708


Explain the concept and use of type void.

754


How are pointers declared in c?

688


What math functions are available for integers? For floating point?

731


What is integer constants?

722






Why do we use namespace feature?

681


Is c dynamically typed?

782


what is a constant pointer in C

776


Should I use symbolic names like true and false for boolean constants, or plain 1 and 0?

705


What are the features of the c language?

735


What is the correct declaration of main?

775


can we change the default calling convention in c if yes than how.........?

2151


Sir i need notes for structure,functions,pointers in c language can you help me please

2036


How does free() know explain how much memory to release?

707


What are variables and it what way is it different from constants?

884