answersLogoWhite

0


Best Answer

c program for implement a stack using two queue First push the given elements in stack1... then pop those elements and push them in 2nd stack... now pop the elements from this stack... thus implemented queue

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

#include < stdio.h >

#include < conio.h >

#define MAX 10

int stack[MAX],top;

void main()

{

char ch;

int choice,item;

void display();

int pop();

void push(int);

clrscr();

top=-1;

do

{

printf("1.Insertion");

printf("\n2.Deletion");

printf("\n3.Display");

printf("\nEnter your choice(1-3):");

scanf("%d",&choice);

switch(choice)

{

case 1: printf("\nEnter the item to be inserted:");

scanf("%d",&item);

push(item);

display();

break;

case 2: item=pop();

printf("\nThe item deleted is %d",item);

display();

break;

case 3: display();

break;

default:printf("Wrong choice");

break;

}

printf("\nDo you want to continue(y/n):");

fflush(stdin);

scanf("%c",&ch);

}while(ch=='y' ch=='Y');

getch();

}

void push(int item)

{

if(top==MAX-1)

{

printf("Stack Overflow");

return;

}

top=top+1;

stack[top]=item;

}

int pop()

{

int t;

if(top==-1)

{

printf("Stack Underflow");

return -1;

}

t=stack[top];

top=top-1;

return t;

}

void display()

{

int i;

printf("\nThe Stack elements are:");

for(i=0;i < =top;i++)

printf("%5d",stack[i]);

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

//Class for Stack - Linked List implementation Class Stack { public: STACKPTR *stackptr; Stack(); ~Stack(); Protected: struct Element { int data; struct Element *next }; typedef struct Element *STACKPTR; Public: bool push(int data); bool pop(int* data); }; Stack::Stack() { stackptr = NULL; } Stack::~Stack() { while(stackptr) //delete all elements { STACKPTR temp = stackptr->next; delete stackptr; stackptr = temp } stackptr = NULL; } bool Stack::Push(int data) { //add nodes in front of Linked list STACKPTR newNode = new Element; newNode->data = data; if(!newNode) return false; newNode->next = stackptr; stackptr = newNode; return true; } bool Stack::pop(int* data) { STACKPTR temp; if(!stackptr) //stack is empty return false; temp = stackptr; *data = stackptr->data; stackptr = stackptr->next; delete temp; retrun true; }

This answer is:
User Avatar

User Avatar

Wiki User

8y ago
AnswerThis is a homework assignment. write homework assignments for you because you need to do this yourself or you will not learn the skills that the assignment is trying to teach you.

However if, while trying to do your assignment, you find a specific problem that you need help with, WikiAnswers will help you with these specific questions (e.g is this 'xxxxx' C++ statement correct).

Answervoid CreateStack_Core (your_stack_type &stack, int nhas, int nreq)

{

if (nhas

AddElement (stack);

CreateStack_Core (stack, nhas+1, nreq);

}

}

void CreateStack (your_stack_type &stack, int nreq)

{

CreateEmptyStack (stack);

CreateStack_Core (stack, 0, nreq);

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Supposing you have a class named foo and you wish to create a stack of foo objects using pointers, you would use the following declaration:

std::stack<std::unique<foo>> my_stack;

Note that the stack takes ownership of the pointer, thus when you pop a foo from the stack, the object is automatically destroyed. Therefore you should only pop the top stack element when you are actually done with it.

If you wish to use shared ownership, use std::shared_ptr instead. The same rule applies, but objects are only destroyed after a pop if there are no other references to that object.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

/* Static implementation of stack

using Array and local variables

and structure*/

#include<conio.h>

#include<stdio.h>

#define MAX 5

struct stack

{

int arr[ MAX ]; //array to be treated as stack

int top;

};

typedef struct stack stack;

void push( stack *, int );

int pop( stack * );

void display( stack );

void main()

{

stack stk;

int val;

int choice;

//initialization (initialize stack to be empty)

stk.top = -1;

clrscr();

do

{

clrscr();printf("\n**** Stack Opertions ****");printf( "\n1) Add Element" );printf( "\n2) Delete Element" );printf( "\n3) Display" );printf( "\n4) Exit" );printf("\n\nPlease enter your choice(1/2/3/4):");scanf("%d", &choice );switch( choice ){case 1 :printf("\nPlease enter the element"\" to be added : ");scanf("%d", &val );push( &stk, val );break;case 2 :val = pop( &stk );if( val != -9999 ){printf("\nThe element deleted"\" is %d", val );}break;case 3 :display( stk );break;case 4 :printf("\nFinished with stack operations : ");break;default:printf("\nIncorrect choice!!!! ");}printf("\nPress any key to continue : ");getch();}while( choice != 4 );return;

}

void push( stack *sptr, int val )

{

// CHECKING IF THE STACK IS FULLif( sptr->top -1 )

{

printf("\nThe stack is empty!!!");}

else

{

printf("\t%d", stk.arr[ stk.top ] ) ;}

return;

}

*/

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Yes, please do.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program in'C' language for the implementation of a Stack?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program to convert stack into queue using c language?

In order to write a program to convert stack into queue using c language you must be able to identify the proper program. Having a special certification in programing will be beneficial as well to make sure you recognize the proper queues for the programs.


Write a c program to perform stack operation?

int top=-1; int stack[10];


Write Client and server program in C language using UDP?

Write and run a client and a server program in C-language using UDP


How do you write an assembly language program to find the sum of n numbers using array?

write an assembly language program to find sum of N numbers


Write a c program to sort an unsorted stack?

A stack is implicitly sorted by hierarchical nested order. It does not make sense to sort a stack. Do you mean a list? If so, please ask the question again.


Write an assembly language program to print a to z on screen?

write a program to print A to Z on screen in c?


Can you write program by c language in dos?

Yes.


Write an algorithm to remove an item from the top of the stack?

It is not possible to write a code to POP from the stack when there is no your stack implementation information.Because of that I am going to talk more about Stack in computer architecture and there will be additional link to specific examples(-e).In x86 architecture there is three registers (BP, SP and SS) which are connected with stack and only SP and SS is needed.SS - Stack Segment (base register);SP - Stack Pointer (offset);This is how the POP instruction works:# operand = [SS:SP] (top of the stack) # SP = SP + 2; (change SP to point to new top element)


Write a program in C language to implement the apriori algorithm?

JavaScript is one program that has been written in C to implement the Apriori algorithm. There are also several other known programs available on the Internet that implement it as well.


Example of Password program code in assembly language?

How to write the program un Assembly language to set a password for personal computers?


What are the advantages and disadvantages of machine language?

AdvantageThe only advantage is that program of machine language run very fast because no translation program is required for the CPU.DisadvantagesIt is very difficult to program in machine language. The programmer has to know details of hardware to write program.The programmer has to remember a lot of codes to write a program which results in program errors.It is difficult to debug the program.


Assembly language program for string concatenation using 8086 microprocessor?

write program to concatenating two sting in 8086 assembly language