Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- struct list{
- int data;
- struct list *next;
- };
- typedef struct list node;
- node *top=NULL;
- int peek()
- {
- if(top==NULL)
- {
- printf("STACK IS EMPTY\n");
- return -1;
- }
- return top->data;
- }
- void display()
- {
- node *temp;
- temp=top;
- if(top==NULL)
- {
- printf("STACK IS EMPTY\n");
- }
- else{
- while(temp!=NULL)
- {
- printf("ELEMENTS ARE:%d ",temp->data);
- temp=temp->next;
- }
- }
- }
- void push(int item)
- {
- node *new_node;
- new_node = new node();
- new_node->data=item;
- new_node->next=top;
- top=new_node;
- }
- void pop()
- {
- if(top==NULL)
- {
- printf("STACK IS EMPTY\n");
- }
- else{
- top=top->next;
- }
- }
- int main()
- {
- push(5);
- push(10);
- push(20);
- int x=peek();
- printf("TOP IS : %d ",x);
- printf("\n");
- display();
- pop();
- pop();
- printf("\n");
- display();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement