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

Describe commonly used asymptotic notations, Q.1 Compare two functions n 2 ...

Q.1 Compare two functions n 2 and 2 n for various values of n. Determine when second becomes larger than first. Q.2 Why do we use asymptotic notation in the study of algorit

Compound interest, Write the algorithm for compound interest

Write the algorithm for compound interest

Graph traversal schemes, Q. Explain various graph traversal schemes and wri...

Q. Explain various graph traversal schemes and write their advantages and disadvantages. A n s . Graph Traversal Scheme is explained below In many troubles we wish

Methods of collision resolution, Methods of Collision Resolution 1)  Co...

Methods of Collision Resolution 1)  Collision Resolution by separate chaining  2)  Collision Resolution by open addressing

Graphs with negative edge costs, We have discussed that the above Dijkstra'...

We have discussed that the above Dijkstra's single source shortest-path algorithm works for graphs along with non-negative edges (like road networks). Given two scenarios can emerg

What is assertions, What is Assertions Introduction At every point...

What is Assertions Introduction At every point in a program, there are generally constraints on the computational state that should hold for program to be correct. For ins

Array implementation of lists, In the array implementation of the lists, we...

In the array implementation of the lists, we will use the array to hold the entries and a separate counter to keep track of the number of positions are occupied. A structure will b

Representation of records, Records are mapped onto a computer store by simp...

Records are mapped onto a computer store by simply juxtaposing their elements. The address of a component (field) r relative to the origin address of the record r is named the fiel

Queue be represented by circular linked list, Q. Can a Queue be represented...

Q. Can a Queue be represented by circular linked list with only one pointer pointing to the tail of the queue? Substantiate your answer using an example. A n s . Yes a

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