Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct Node {
- int data;
- struct Node *next, *prev;
- }node;
- node* head=NULL, *tail;
- void push(int x)
- {
- node* newnode;
- newnode=(node*)malloc(sizeof(node));
- newnode->data=x;
- newnode->next=NULL;
- newnode->prev=NULL;
- if(head==NULL) {
- tail=newnode;
- head=newnode;
- }
- else {
- newnode->next=head;
- head->prev=newnode;
- head=newnode;
- }
- }
- void pop()
- {
- if(head == NULL) {
- printf("Stack is Empty !\n");
- }
- else if(head->next == NULL) {
- head = NULL;
- }
- else {
- node *temp = head;
- head = head->next;
- head->prev=NULL;
- free(temp);
- }
- }
- void print()
- {
- node*current=tail;
- while(current!=NULL) {
- printf(" data %d\n", current->data);
- current=current->prev;
- }
- printf("\n");
- }
- int main(){
- int x,n,i;
- printf("Enter input number: ");
- scanf("%d",&n);
- for(i=0;i<n;i++){
- printf("Enter stack data");
- scanf("%d",&x);
- push(x);}
- print();
- pop();
- print();
- }
Advertisement
Add Comment
Please, Sign In to add comment