Note 1

Take Note:

Take a note while surfing.





Note With Ink

Give your Note a Colorful Tag.




Easy to Access

Stay on same information and in Sync wherever you are.

Note 2

Take Note:

Organize your information,It may take Shape.





Think With Ink

Differ your Content by Color.




Easy to Access

Easy to pull up your content from anywhere anytime.

Note 3

Take Note:

Don't Let information to miss,Because it take shape





Note With Ink

Simple an Easy Way to take a note.




Easy to Access

Get the same in next visit.

Program Discussion :: Stack
Home > Programs > Stack

88. Write a push() and pop() implementation using linked list in C.

Answer:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
struct node
{
    int info;
    struct node *ptr;
}*top,*top1,*temp;
void push(int data);
void pop();
void create();
int count = 0;
void main()
{
    int no, ch, e;
    cout<<"\n 1 - Push";
   cout<<"\n 2 - Pop";
      create();
       while (1)
    {
       cout<<"\n Enter choice : ";
       cin>>ch;
        switch (ch)
        {
        case 1:
            cout<<"Enter data : ";
            cin>>no;
            push(no);
            break;
        case 2:
            pop();
            break;
        default :
            cout<<" Wrong choice, Please enter correct choice  ";
            break;
        }
        }
    }
    void create()
{
    top = NULL;
}
 
/* Count stack elements */
void stack_count()
{
    printf("\n No. of elements in stack : %d", count);
}
 
/* Push data into stack */
void push(int data)
{
    if (top == NULL)
    {
        top =(struct node *)malloc(1*sizeof(struct node));
        top->ptr = NULL;
        top->info = data;
    }
    else
    {
        temp =(struct node *)malloc(1*sizeof(struct node));
        temp->ptr = top;
        temp->info = data;
        top = temp;
    }
    count++;
}
void pop()
{
    top1 = top;
 
    if (top1 == NULL)
    {
        printf("\n Error : Trying to pop from empty stack");
        return;
    }
    else
        top1 = top1->ptr;
    printf("\n Popped value : %d", top->info);
    free(top);
    top = top1;
    count--;
}

Post Your Answer Here:

Name *
Email

Language:

Post Your Reply Here:



Language:

Post Your Reply Here: