Created
July 10, 2016 15:07
-
-
Save haridutt12/095f58b586d53f69110afb5d2f3946d3 to your computer and use it in GitHub Desktop.
link list traversal using stack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
struct node | |
{ | |
int no; | |
struct node *next; | |
}*h,*c,*p; | |
struct stack | |
{ | |
int a[10]; | |
int top; | |
}; | |
void initialise(struct stack *p) | |
{ | |
p->top=-1; | |
} | |
void push(struct stack *p,struct node *c) | |
{ | |
p->top++; | |
p->a[p->top]=c->no; | |
} | |
int pop(struct stack *p) | |
{ | |
int x=p->a[p->top]; | |
p->top--; | |
return x; | |
} | |
void main() | |
{ | |
struct stack s; | |
p=(struct node*)malloc(sizeof(struct node)); | |
int n; | |
int i=0; | |
scanf("%d",&n); | |
scanf("%d",&p->no); | |
c=p; | |
h=c; | |
initialise(&s); | |
for(i=0;i<n-1;i++) | |
{ | |
p=(struct node*)malloc(sizeof(struct node)); | |
scanf("%d",&p->no); | |
c->next=p; | |
c=p; | |
} | |
c->next=NULL; | |
c=h; | |
while(c!=NULL) | |
{ | |
push(&s,c); | |
c=c->next; | |
} | |
c=h; | |
while(c!=NULL) | |
{ | |
printf("%d",pop(&s)); | |
c=c->next; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment