/*stack implementation using array
push pop operation
*/
#include<stdio.h>
void push(int item);
int pop();
void display();
int stack[10];
int top=-1;
int main()
{
push(10);
push(20);
push(30);
push(40);
push(50);
display();
printf("\n");
pop();
pop();
display();
return 0;
}
void push(int item)
{
if(top==9)
{
printf("Stack is full.\n");
return;
}
top++;
stack[top]=item;
}
int pop()
{
printf("\npop called.\n");
if(top==-1)
{
printf("stack is empty\n");
return -1;
}
int item;
item=stack[top];
top--;
return item;
}
void display()
{
printf("\ndisplay called.\n");
int i;
for(i=top;i>=0;i--)
{
printf("%d ",stack[i]);
}
}
/*
display called.
50 40 30 20 10
pop called.
pop called.
display called.
30 20 10
*/
//stack to reverse a string
//Stack appalication to reverse a sentence
//read array elts. push into stack,pop,display
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 20
char stack[SIZE];
int top=-1;
int i;
void push(char c);
int pop();
int main()
{
char arr[]="World is not enough";
printf("org string is: %s\n",arr);
for(i=0;i<SIZE-1;i++)
{
push(arr[i]);
}
for(i=0;i<SIZE-1;i++)
{
arr[i]=pop();
}
printf("rev string is: %s\n",arr);
return 0;
}
void push(char c)
{
if(top==SIZE-1)
printf("overflow");
else
stack[++top]=c;
}
int pop()
{
if(top==-1)
printf("underflow\n");
else
return stack[top--];
}
/*
org string is: World is not enough
rev string is: hguone ton si dlroW
*/
push pop operation
*/
#include<stdio.h>
void push(int item);
int pop();
void display();
int stack[10];
int top=-1;
int main()
{
push(10);
push(20);
push(30);
push(40);
push(50);
display();
printf("\n");
pop();
pop();
display();
return 0;
}
void push(int item)
{
if(top==9)
{
printf("Stack is full.\n");
return;
}
top++;
stack[top]=item;
}
int pop()
{
printf("\npop called.\n");
if(top==-1)
{
printf("stack is empty\n");
return -1;
}
int item;
item=stack[top];
top--;
return item;
}
void display()
{
printf("\ndisplay called.\n");
int i;
for(i=top;i>=0;i--)
{
printf("%d ",stack[i]);
}
}
/*
display called.
50 40 30 20 10
pop called.
pop called.
display called.
30 20 10
*/
//stack to reverse a string
//Stack appalication to reverse a sentence
//read array elts. push into stack,pop,display
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 20
char stack[SIZE];
int top=-1;
int i;
void push(char c);
int pop();
int main()
{
char arr[]="World is not enough";
printf("org string is: %s\n",arr);
for(i=0;i<SIZE-1;i++)
{
push(arr[i]);
}
for(i=0;i<SIZE-1;i++)
{
arr[i]=pop();
}
printf("rev string is: %s\n",arr);
return 0;
}
void push(char c)
{
if(top==SIZE-1)
printf("overflow");
else
stack[++top]=c;
}
int pop()
{
if(top==-1)
printf("underflow\n");
else
return stack[top--];
}
/*
org string is: World is not enough
rev string is: hguone ton si dlroW
*/
No comments:
Post a Comment