STACK IMPLEMENTATION USING LINKED LIST
DYNAMIC IMPLEMENTATION OF STACK
In computer
science, a stack is a last in, first out (LIFO) abstract data type and linear data
structure. A stack can have any abstract data type as an element, but is characterized by two
fundamental operations, called push
and pop (or pull). The push operation adds a new item to the top
of the stack, or initializes the stack
if it is empty. If the stack is full and
does not contain enough space to accept the given item, the stack is then considered to be in an overflow
state. The pop operation removes an item from the top of the stack. A pop either reveals previously concealed
items, or esults in an empty stack, but
if the stack is empty then it goes into underflow state (It means no items are
present in stack to be removed). A stack
pointer is the register which holds the value of the stack. The stack pointer always points to the top
value of the stack.
A
stack is a restricted data structure, because only a small number of
operations are performed on it. The nature of the pop and push operations also
means that stack elements have a natural order. Elements are removed from the
stack in the reverse order to the order of their addition: therefore, the lower
elements are those that have been on the stack the longest.
[Ref:
wikipedia.org]
#include<stdio.h>
#include<stdlib.h>
typedef struct ll
{
int digit ;
struct ll *next ;
}mynode ;
mynode *head,*new1,*prev ;
void addnode(mynode **hd,int
data) ;
void deletenode(mynode **hd) ;
void traverse(mynode *hd) ;
void searchnode(mynode
*hd,int data) ;
void main()
{
int data,choice ;
clrscr() ;
head = (mynode*)malloc(sizeof(mynode)) ;
head->next = NULL ;
while(1)
{
printf("\n1.ADD
NODE") ;
printf("\n2.DELETE
NODE") ;
printf("\n3.TRAVERSE
LIST") ;
printf("\n4.SEARCH
NODE") ;
printf("\n5.EXIT") ;
printf("\nENTER CHOICE :
") ;
scanf("%d",&choice) ;
switch(choice)
{
case 1: printf("\nEnter The
Number : ") ;
scanf("%d",&data) ;
addnode(&head,data) ;
break ;
case 2: deletenode(&head) ;
break ;
case 3: traverse(head) ;
break ;
case 4: printf("\nEnter Data To
Be Searched : ") ;
scanf("%d",&data) ;
searchnode(head,data) ;
break ;
case 5: printf("Thank's For
Using Me !! Good Bye %c !!",1) ;
getch() ;
exit(0) ;
default: printf("\nInvalid Choice
Re-Enter : ") ;
break ;
}//switch
}//switch
} //while
}
void addnode(mynode **hd ,
int data)
{
clrscr() ;
new1 = (mynode*)malloc(sizeof(mynode)) ;
new1->digit = data ;
if((*hd)->next==NULL)
{
new1->next=*hd ;
printf("\nFirst Node
With Data %d Is Added",data) ;
(*hd)=new1 ;
return ;
}//if
new1->next = *hd ;
printf("\nNode With
Value %d Is Added ",data) ;
(*hd) = new1 ;
}
void deletenode(mynode **hd)
{
clrscr() ;
prev = *hd ;
if((*hd)->next == NULL)
{
printf("\nNothing Is In
List Enter Some Data First") ;
return ;
}//if
*hd = (*hd)->next ;
printf("Node With Data
%d Is Deleted",prev->digit) ;
free(prev) ;
}
void traverse(mynode *hd)
{
clrscr() ;
if(hd->next==NULL)
{
printf("\nNothing Found
Enter Some Data First ") ;
return ;
}//if
printf("\nFollowing Data
Are Found\n") ;
while(hd->next != NULL)
{
printf("%d,",hd->digit) ;
hd = hd->next ;
}//while
}
void searchnode(mynode
*hd,int data)
{
clrscr() ;
if((hd)->next==NULL)
{
printf("\nNothing Found
List Is Empty") ;
return ;
}//if
while((hd)->next!=NULL)
{
if((hd)->digit==data)
{
printf("\nData %d Is
Found",data) ;
return ;
}//if
hd=hd->next ;
}//while
printf("\nData %d Is Not
Found ",data) ;
}
nic ......
ReplyDelete