Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

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.

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M9619047

Have any Question?


Related Questions in Computer Engineering

Sql transactions exercisesperform the test for the

SQL Transactions Exercises Perform the test for the non-additive join property (lossless join) for the relation R(A 1 , A 2 , A 3 , A 4 , A 5 ), and the decompositions D a , D b , D c , D d  and set of functional depende ...

Please explain which formula i should use to complete the

Please explain which formula I should use to complete the following probability question: The probability that a family will buy a vacation home in Miami, malibu, or newport is 0.25, 0.10 and 0.35. What is the probabilit ...

The freemont automobile factory has discovered that the

The Freemont Automobile Factory has discovered that the longer a worker has been on the job, the more parts the worker can produce. I need help finding an application that computes and displays a worker's anticipated out ...

Be sure to answer all partsphosgenenbspcocl2nbspis a toxic

Be sure to answer all parts. Phosgene (COCl 2 ) is a toxic substance that forms readily from carbon monoxide and chlorine at elevated temperatures: CO( g ) + Cl 2 ( g ) ? COCl 2 ( g ) If 0.490 mol of each reactant is pla ...

Assignmentanalyze web programs in order to test debug and

Assignment Analyze web programs in order to test, debug, and improve them. Provide your own original example for each of the explanations you provide. Explain how geologation can determine your location. Discuss the accu ...

Seamus was assigned to created web site plan for a charity

Seamus was assigned to created Web site plan for a charity organization. He must ensure that the site includes the following features: -A message stating the purpose of the charity -An online form that will receive perso ...

Question show the dynamic programming table of the longest

Question : Show the dynamic programming table of the longest common subsequence problem for two sequences: S1 = ABAABBA and S2 = BAAABAB. Also show how to find the LCS itself from the table.

Sutures sutures are strands or fibers used to sew living

Sutures: Sutures are strands or fibers used to sew living tissue together after an injury or an operation. Packages of sutures must be sealed carefully before they are shipped to hospitals to prevent contamination. The p ...

Systems analysis projectpersonal trainer inc owns and

Systems analysis project Personal Trainer, Inc. owns and operates fitness centers in a dozen Midwestern cities. The centers have done well, and the company is planning an international expansion by opening a new "superce ...

A streaming company needs to measure the average time

A streaming company needs to measure the AVERAGE time spread for users who watch video on their platform in a session. Each user has one time spread, which is the difference between her longest session streaming video an ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As