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.:- M9536525

Have any Question?


Related Questions in Computer Engineering

Please discuss the followingas demand increased for these

Please discuss the following: As demand increased for these mortgage backed securities, lenders reacted by relaxing their approval standards to increase production. No longer were "all" borrowers required to document the ...

Roberto is the network administrator for an international

Roberto is the network administrator for an international law firm with offices and customers in North America, South America, Africa, and the Middle East. The lawyers frequently contact each other via e-mail, use the In ...

Command to move all the sh files from current folder to

Command to move all the .sh files from current folder to parent directory. (single line Unix)

Algorithms assignment -task -1 design and implement an

Algorithms Assignment - TASK - 1. Design and implement an efficient algorithm for determining the best route between two given stations in a given rail network. 2. The rail network should be passed to your program as an ...

After reading this weeks materials please respond to two 2

After reading this week's materials, please respond to TWO (2) of the following questions. PROVIDE CITATION IN APA 1. Describe the controls contained within the three Access Control categories that can be integrated with ...

Question suppose we have a system with frequency of

Question : Suppose we have a system with frequency of floating point operations = 35%. What should be the speedup of floating point operation in our design of the next CPU to achieve a desired overall system speedup in t ...

For the rosenberg land development problem problem 2 in

For the Rosenberg Land Development problem (Problem 2 in Chapter 14), suppose that the construction costs are uncertain. Specifically, assume that the distribution ofconstruction costs is normally distributed, with the m ...

Please pick a health care phenomenaproblem and use philip

Please pick a health care phenomena/problem and use Philip Jacobs's descriptive, explanatory and evaluative model (in studying Economics) to analyze it. (Please use the real statistics that you search online and cite the ...

Question what is the smallest accurate big-oh notation for

Question : What is the smallest accurate big-Oh notation for finding an optimal tour for a travelling sales person problem on a graph with with V vertices (nodes) and E edges (arcs) ? (a) O(1) (b) O(V) (c) O(V log V) (d) ...

A small sports club keeps information about its members and

A small sports club keeps information about its members and the fees they pay. The secretary wants to be able to record when members pay and print a report similar to that in the figure below. last name - first_narne - p ...

  • 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