Program to implementing stack using linked lists, Data Structure & Algorithms

Assignment Help:

include

include

include

/* Definition of structure node */

typedef struct node

{

int data;

struct node *next;

} ;

/* Definition of push function */

void push(node **tos,int item)

{

node *temp;

temp=(node*)malloc(sizeof(node));                             /* Dynamically create new node */

if(temp==NULL)                                                   /* If enough amount of memory is */

{                                                                            /* not available, the function malloc will */

 printf("\n Error: Memory Space is not sufficient ");         /* return NULL to temp */ getch();

return;

}

else                                                     /* otherwise*/

{

temp->data=item;  /* put item into the data portion of node*/

temp->next=*tos;                       /*Add this node at the front of the stack */

*tos=temp;                                  /* managed through linked list*/

}

}                                                             /*end of function push*/

/* Definition of pop function */

int pop(node **tos)

{

node *temp; temp=*tos; int item;

if(*tos==NULL)

return(NULL);

else

{

*tos=(*tos)->next;                             /* To pop an element from stack*/

item=temp->data;                              /* Eliminate the front node of the */ free(temp);                                                                     /* stack managed through L.L*/

return (item);

}

}  /*end of function pop*/

/* Definition of display function */

void display(node *tos)

{

node *temp=tos;

if(temp==NULL)                     /* verify whether the stack is empty*/

{

printf("\n Stack empty");

return;

}

else

{

while(temp!=NULL)

{

printf("\n%d",temp->data);   /* display all of the values of the stack*/

temp=temp->next;                /* from the front node to last node*/

}

}

}                                                               /*end of function display*/

/* Definition of main function */

void main()

{

int item, ch;

char choice='y'; node *p=NULL; do

{

clrscr();

printf("\t\t\t\t*****MENU*****");

printf("\n\t\t\t1. To PUSH an element");

printf("\n\t\t\t2. To POP an element");

printf("\n\t\t\t3. To DISPLAY the elements of stack");

printf("\n\t\t\t4. Exit");

printf("\n\n\n\t\t\t Enter your choice:-");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("\n Enter an element that you need to push ");

scanf("%d",&item); push(&p,item); break;

case 2:

item=pop(&p);

if(item!=NULL);

printf("\n Detected item is%d",item);

break;

case 3:

printf("\nThe elements of stack are");

display(p);

break;

case 4:

exit(0);

}           /*switch closed */

printf("\n\n\t\t Do you need to run it again y/n");

scanf("%c",&choice);

while(choice=='y');

}

/*end of function main*/

Likewise, as we did in the implementation of stack through arrays, to know the working of this program, we executed it thrice & pushed 3 elements (10, 20, 30). After that we call the function display in the next run to make out the elements in the stack.

At first, we defined a structure called node. Each of nodes contains two portions, data & a pointer which keeps the address of the next node into the list. The Push function will add a node at the front of the linked list, while pop function will delete the node from the front of the linked list. There is no requirement to declare the size of the stack in advance as we have done in the program where in we implemented the stack by using arrays as we create nodes as well as delete them dynamically. The function display will print elements of the stack.


Related Discussions:- Program to implementing stack using linked lists

Rotations in binary tree, H o w can you r ot a t e a B i n a r y...

H o w can you r ot a t e a B i n a r y Tr e e? E x pl a i n r i g h t a n d l eft r ot a tion s by taking an e x a mpl e.   If after

Which are the two standard ways of traversing a graph, Which are the two st...

Which are the two standard ways of traversing a graph? i. The depth-first traversal   ii. The breadth-first traversal

Find strongly connected components - dfs, A striking application of DFS is ...

A striking application of DFS is determine a strongly connected component of a graph. Definition: For graph G = (V, E) , where V refer to the set of vertices and E refer to the

Efficiency of binary search, Each of the comparison in the binary search de...

Each of the comparison in the binary search decrease the number of possible candidates where the key value can be searched by a factor of 2 as the array is divided into two halves

Sparse matrix, How sparse matrix stored in the memory of a computer?

How sparse matrix stored in the memory of a computer?

Red-black tree after insertion, The above 3 cases are also considered conve...

The above 3 cases are also considered conversely while the parent of Z is to the right of its own parent. All the different kind of cases can be illustrated through an instance. Le

Drawback of sequential file, Following are some of the drawback of sequenti...

Following are some of the drawback of sequential file organisation: Updates are not simply accommodated. By definition, random access is impossible. All records should be

What is the best case complexity of quick sort, What is the best case compl...

What is the best case complexity of quick sort In the best case complexity, the pivot is in the middle.

Complexity of an algorithm, compare two functions n and 2n for various valu...

compare two functions n and 2n for various values of n. determine when second becomes larger than first

Array, how to define the size of array

how to define the size of array

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd