In computer science, stack is like a books in library, or notes in a bank. Stack is also called last-in-first-out (LIFO) or first-in-last-out (FILO) list.Its mean that any element added in last to the stack is remove first and first element is remove at the last. In this addition of a new element or deletion of an existing element always takes place at the end. This end is often known as top of the stack. Top is a pointer which point the element are added at the last. There is two fundamental operations in the stack : push and pop. When an element is added in the stack, is called push. When an element is remove from the stack, is called pop.
When we use Stack.....
Advantages of Stack...
We can decompose a complex programming task into a simple programming.
We can reducing the duplication of same code in the program.
Disadvantages of Stack....
Operations on Stack...
struct snode
{
int value;
struct snode * link;
};
void push( struct snode **, int);
int pop( struct snode ** );
int peek( struct snode ** );
void deleteallnode( struct snode ** );
int count( struct snode * );
void display( struct snode * );
void main()
{
struct snode * sptr = NULL;
push( &sptr, value);
push( &sptr, value);
push( &sptr, value);
pop( &sptr);
peek( &sptr);
deleteallnode( &sptr);
count( sptr);
display( sptr);
}
void push ( struct snode ** top, int value)
{
struct snode * temp = NULL;
temp = (struct snode *) malloc( sizeof ( struct snode ));
temp-->data = value;
temp-->link = NULL;
if( *top== NULL)
{
*top= temp;
temp= NULL;
}
else
{
struct snode * temp1= *top;
temp-->link = temp1;
*top = temp;
}
}
int pop( struct snode ** top)
{
struct snode * temp= *top;
if( temp== NULL)
{
cout<<" stack is empty";
retrurn -1;
}
else
{
int value= temp-->data;
*top= temp-->link;
free( temp);
temp= NULL;
return value;
}
}
int peek( struct snode ** top)
{
struct snode * temp= *top;
if( temp== NULL)
{
cout<< "stack is empty";
return -1;
}
else
{
int value = temp-->data;
return value;
}
}
void deleteallnode( struct snode**top)
{
struct snode * temp= *top;
if(temp== NULL)
{
cout<< "stack is empty";
retrn -1;
}
else
{
while(temp!= NULL)
{
*top= temp-->link;
free( temp);
temp= *top;
}
}
}
int count( struct snode *sptr)
{
int c= 0;
if( sptr== NULL)
{
cout<< "stack is empty";
}
else
{
while( sptr != NULL)
{
c++;
sptr = sptr-->link;
}
}
return c;
}
void display( struct snode *sptr)
When we use Stack.....
Advantages of Stack...
We can decompose a complex programming task into a simple programming.
We can reducing the duplication of same code in the program.
Disadvantages of Stack....
Operations on Stack...
struct snode
{
int value;
struct snode * link;
};
void push( struct snode **, int);
int pop( struct snode ** );
int peek( struct snode ** );
void deleteallnode( struct snode ** );
int count( struct snode * );
void display( struct snode * );
void main()
{
struct snode * sptr = NULL;
push( &sptr, value);
push( &sptr, value);
push( &sptr, value);
pop( &sptr);
peek( &sptr);
deleteallnode( &sptr);
count( sptr);
display( sptr);
}
void push ( struct snode ** top, int value)
{
struct snode * temp = NULL;
temp = (struct snode *) malloc( sizeof ( struct snode ));
temp-->data = value;
temp-->link = NULL;
if( *top== NULL)
{
*top= temp;
temp= NULL;
}
else
{
struct snode * temp1= *top;
temp-->link = temp1;
*top = temp;
}
}
int pop( struct snode ** top)
{
struct snode * temp= *top;
if( temp== NULL)
{
cout<<" stack is empty";
retrurn -1;
}
else
{
int value= temp-->data;
*top= temp-->link;
free( temp);
temp= NULL;
return value;
}
}
int peek( struct snode ** top)
{
struct snode * temp= *top;
if( temp== NULL)
{
cout<< "stack is empty";
return -1;
}
else
{
int value = temp-->data;
return value;
}
}
void deleteallnode( struct snode**top)
{
struct snode * temp= *top;
if(temp== NULL)
{
cout<< "stack is empty";
retrn -1;
}
else
{
while(temp!= NULL)
{
*top= temp-->link;
free( temp);
temp= *top;
}
}
}
int count( struct snode *sptr)
{
int c= 0;
if( sptr== NULL)
{
cout<< "stack is empty";
}
else
{
while( sptr != NULL)
{
c++;
sptr = sptr-->link;
}
}
return c;
}
void display( struct snode *sptr)
{
if( sptr== NULL)
{
cout<< "stack is empty";
}
else
{
while( sptr != NULL)
{
cout<< sptr-->data;
sptr = sptr-->link;
}
}
}
No comments:
Post a Comment