answersLogoWhite

0


Best Answer

#include<stdio.h>

#include<conio.h>

#define MAX 10

typedef struct stack

{

int item[MAX];

int Top;

}stk;

void createstack(stk *);

void push(stk *,int);

int pop(stk *);

int isempty(stk *);

int isfull(stk *);

int main()

{

int choice;

int value;

stk s;

createstack(&s);

do{

clrscr();

printf("\n\t Main Menu");

printf("\n1. Push");

printf("\n2. Pop");

printf("\n3. Exit");

printf("\nEnter your choice=");

scanf("%d",choice);

switch(choice)

{

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

scanf("%d",value);

push(&s,value);

getch();

break;

case 2: value=pop(&s);

if (value==0)

printf("\n Underflow: Stack is empty");

else

printf("\n Popped Element is %d",value)

getch();

break;

case 3: exit();

defualt: printf("\nInvalid choice");

}

}while(1);

return 0;

}

void createstack(stk *s)

{

s->Top=-1;

}

void push(stk *s,int element)

{

if(isfull(s))

{

printf("\nOverflow: Stack is full");

return;

}

s->Top++;

s->item[s->Top]=element;

printf("\nValue is pushed onto the stack");

}

int pop(stk *)

{

int popped;

if (isempty(s))

return 0;

popped=s->item[s-.Top];

s->Top-;

return popped;

}

int isempty(stk *)

{

return s->Top==-1;

}

int isfull(stk *)

{

return s->Top==MAX-1;

}

User Avatar

Wiki User

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

Wiki User

14y ago

Program for Stack implementation through Array

#include <stdio.h>

#include<ctype.h>

# define MAXSIZE 200

int stack[MAXSIZE];

int top; //index pointing to the top of stack

void main()

{

void push(int);

int pop();

int will=1,i,num;

clrscr();

while(will ==1)

{

printf("

MAIN MENU:

1.Add element to stack

2.Delete element from the stack

");

scanf("%d",&will);

switch(will)

{

case 1:

printf("

Enter the data... ");

scanf("%d",&num);

push(num);

break;

case 2: i=pop();

printf("

Value returned from pop function is %d ",i);

break;

default: printf("Invalid Choice . ");

}

printf("

Do you want to do more operations on Stack ( 1 for yes, any other key to exit) ");

scanf("%d" , &will);

} //end of outer while

} //end of main

void push(int y)

{

if(top>MAXSIZE)

{

printf("

STACK FULL

");

return;

}

else

{

top++;

stack[top]=y;

}

}

int pop()

{

int a;

if(top<=0)

{

printf("

STACK EMPTY

");

return 0;

}

else

{

a=stack[top];

top--;

}

return(a);

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

/* Program to implement stacks through arrays */

#include <stdio.h>

#include <conio.h>

int stack[100];

int sz,top=-1,n;

void push()

{

top++;

if(top>=sz)

{

printf("\nStack full");

return;

}

else

{

printf("\nEnter element to be pushed:");

scanf("%d",&n);

stack[top]=n;

printf("\n%d pushed onto stack");

}

}

int pop()

{

top--;

if(top<0)

return -1;

else

return stack[top];

}

void main()

{

char ch,op;

clrscr();

printf("\nEnter size of stack:");

scanf("%d",&sz);

do

{

printf("\nEnter (p)ush,p(o)p:");

fflush(stdin);

op=getchar();

switch(op)

{

case 'p':push();break;

case 'o':n=pop();

if(n!=-1)printf("\n%d popped",n);

else printf("\nStack is empty");

break;

default:printf("\nInvalid option");break;

}

printf("\nTo continue,enter y:");

fflush(stdin);

ch=getchar();

}

while(ch=='y')

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

#include<conio.h>

#define MAX 10

typedef struct stack

{

int item[MAX];

int Top;

}stk;

void createstack(stk *);

void push(stk *,int);

int pop(stk *);

int isempty(stk *);

int isfull(stk *);

int main()

{

int choice;

int value;

stk s;

createstack(&s);

do{

clrscr();

printf("\n\t Main Menu");

printf("\n1. Push");

printf("\n2. Pop");

printf("\n3. Exit");

printf("\nEnter your choice=");

scanf("%d",choice);

switch(choice)

{

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

scanf("%d",value);

push(&s,value);

getch();

break;

case 2: value=pop(&s);

if (value==0)

printf("\n Underflow: Stack is empty");

else

printf("\n Popped Element is %d",value)

getch();

break;

case 3: exit();

defualt: printf("\nInvalid choice");

}

}while(1);

return 0;

}

void createstack(stk *s)

{

s->Top=-1;

}

void push(stk *s,int element)

{

if(isfull(s))

{

printf("\nOverflow: Stack is full");

return;

}

s->Top++;

s->item[s->Top]=element;

printf("\nValue is pushed onto the stack");

}

int pop(stk *)

{

int popped;

if (isempty(s))

return 0;

popped=s->item[s-.Top];

s->Top-;

return popped;

}

int isempty(stk *)

{

return s->Top==-1;

}

int isfull(stk *)

{

return s->Top==MAX-1;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a C program to implement stacks through arrays?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How many stacks needs to implement queue?

One.


What is the minimum number of stacks of size 10 required to implement a queue to size 10?

2 stacks required


What are application of stack?

Stacks are primarily used to implement backtracking algorithms.


Is String a linear data structure?

yes it is, other linear data structures are lists,queues,stacks,arrays


C program for insertion and deletion in stacks?

chk out the following link showing you how to implement stack. Though it is a c++ implementation it may still help you. http://thetechnofreaks.com/2011/10/26/4-creating-a-stack/#ixzz1bvp8FXdb


C plus plus program using a stacks converting a postfix-infix?

Yes


How do you implement stack without array?

Stacks are often implemented using the same node structure as a linked list.


How many stacks of 100 dollar bills are in 193000000?

193,000,000/100 = 1,930,000 stacks. 193,000,000/100 = 1,930,000 stacks. 193,000,000/100 = 1,930,000 stacks. 193,000,000/100 = 1,930,000 stacks.


What are stacks of thylakoids called?

granum are stacks of thylakoids. grana are several stacks of thylakoids. :)


Where would one find information about stacks?

That would depend on what type of stacks, or stacks of what objects, the individual is looking for. Long Island University has a comprehensive document online that guides the reader through the organization and uses of library stacks, or the shelves on which books are housed. Cisco offers tutorials on Stack protocol in computer systems. Lyrics for the song Stacks by Bon Iver can be found on YouTube and lyrics sites such as Metro Lyrics. Businesses called Stacks range from bookstores to coin and collectibles outlets to clothing boutiques.


How do you insert stacks footer?

stacks footer


What are the application of the stack?

One of the applications that I use a stack for is searching through a file system (or a large/complex tree). stack s; s.push(start_directory); while( !s.empty() ) current = s.pop(); s.pushAll( current.children ); if( s is what we're looking for ) exit(); // otherwise, keep looping