STACKs (Using Linked Lists)
Implemented using C Language
C Language Code:
STACKLST.H
// DSTC - STACKLST.H .... Stacks using Linked Lised ...
#include <stdio.h>
#include <stdlib.h>
typedef struct stacklistnode
{
int info ;
struct stacklistnode *next ;
}node ;
typedef struct stacklist
{
node *top ;
}stacklst ;
stacklst *makestacklst()
{
stacklst *t ;
t = (stacklst *)malloc(sizeof(stacklst)) ;
t->top = NULL ;
return t;
}
node *makenode()
{
node *t ;
t = (node *)malloc(sizeof(node)) ;
return t ;
}
int empty(stacklst *s)
{
if (s->top == NULL)
return 1 ;
else
return 0 ;
}
void push(stacklst *s,int v)
{
node *n ;
n = makenode();
n->info = v;
n->next = s->top ;
s->top = n ;
}
int pop(stacklst *s)
Get Paid by Reading Ads on your Mobiles
{
int v;
node *t;
if (empty(s))
{
printf("\nStack Empty");
return NULL;
}
v = s->top->info ;
t = s->top ;
s->top = t->next ;
free(t) ;
return (v) ;
}
STACKLST.CPP
// DSTC ---- STACKLST.CPP ..stacks using Linked Lists
// ... including STACKLSt.H ..header file
#include "stacklst.h"
#include <conio.h>
void main()
{
int ch,i;
stacklst *s1;
s1=makestacklst();
clrscr();
while(1)
{
printf("Enter the choice (1-push, 2-pop, 3-quit): ");
scanf("%d", &ch);
if(ch==3)
break;
else if(ch==1)
{
printf("Enter information to insert: ");
scanf("%d", &i);
push(s1,i);
}
else if(ch==2)
{
i=pop(s1);
if(i!=NULL)
printf("Information is: %d\n", i);
}
else
printf("Invalid choice.......enter a value between 1 and 3\n");
}
}
No comments:
Post a Comment