Sie sind auf Seite 1von 5

Stack

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
int top,i;
char key,temp,e;
char stack[10];
void delay();
void pushanim()
{
for(i=0;i<=17; i++)
{
gotoxy(22+i,7);cout<<" ";
gotoxy(23+i,7); cout<<temp; delay();
}
for(i=1;i<=(14-top);i++)
{
delay();
gotoxy(40,6+i); cout<<" ";
gotoxy(40,7+i); cout<<temp;
}
}
void popanim(char temp)
{
for(i=1;i<=(14-top);i++)
{
delay();
gotoxy(40,22-i-top); cout<<" ";
gotoxy(40,21-i-top); cout<<temp;
}
for(i=1;i<=19;i++)
{
delay();
gotoxy(38+i,7); cout<<" ";
gotoxy(39+i,7); cout<<temp; delay();
}
gotoxy(58,7);cout<<" ";
}
void push(char e)
{
top=top+1;
stack[top]=e;
pushanim();
}
void pop(char e)
{
if(top !=0)
{
e=stack[top]; popanim(e);
top=top-1;
}
else
{
gotoxy(1,7); cout<<"stack telah kosong"<<endl;
gotoxy(1,7);
}
}
void main()
{
clrscr();
cout<<"Animasi Stack"<<endl;
cout<<"1.Push"<<endl;
cout<<"2.Pop"<<endl;
cout<<"3.quit"<<endl;
//cout<<"pilih [1/2/3] ="<<endl;
gotoxy(59,6); cout<<"=";
gotoxy(59,8); cout<<"=";
gotoxy(37,9); cout<<"|| ||";
for(i=1;i<=11;i++)
{
gotoxy(38,10+i);
if(i==11)
cout<<"|___|";
else
cout<<"| |";
}
top=0;
do
{
input:
gotoxy(1,5);
cout<<"masukkan pilihan anda[1/2/3] : ";
key=getche();
if(int(key)==27 || key=='3')
break;
else if(key=='1')
{
if(top != 10)
{
gotoxy(1,7); cout<<"masukkan suatu huruf : ";
cin>>temp;
push(temp);
gotoxy(1,7); cout<<" ";
}
else
{
gotoxy(1,7); cout<<"stack penuh";
getch();
gotoxy(1,7); cout<<" ";
}
}
else if(key=='2')
pop(temp);
else
goto input;
}while(1);
getch();
}
void delay()
{
for(int y=1;y<100;y++)
for(int x=1;x<100;x++)
for(int p=1;p<100;p++)
cout<<"";
}

Program to implement Stack as Linked List


#include<stdio.h>
#include<conio.h>
# include "malloc.h"
struct node
{
int data;
struct node *link;
} ;
struct node *top;
void main()
{
void push(int);
void display();
int wish, num,will,a;
wish = 1;
top = NULL;
clrscr();
printf("Program for Stack as Linked List demo..
");
while(wish == 1)
{
printf("
Main Menu
1.Enter data in stack
2.Delete from stack
");
scanf("%d",&will);
switch(will)
{
case 1:
printf("
Enter the data");
scanf("%d",&num);
push(num);
display();
break;
case 2:
a=pop();
printf("
Value returned from top of the stack is %d",a);
break;
default:
printf("
Invalid choice");
}
printf("
Do you want to continue, press 1");
scanf("%d",&wish);
}
}
void push(int y)
{
struct node *x;
x=malloc(sizeof(struct node));
printf("
Address of newly created node x is %d",x);
x->data = y;
x->link = top;
top = x;
}
void display()
{
int i =0;
struct node * temp;
temp = top;
while(temp!=NULL)
{
printf("
Item No. %d : Data %d Link %d ",i++,temp->data,temp->link);
temp=temp->link;
}
}
/// THIS FUNCTION REMOVES TOP NODE FROM THE STACK AND RETURNS ITS VALUE///
int pop()
{
int a;
if(top==NULL)
{printf("
STACK EMPTY...
"); return 0;}
else
{
a=top->data;
printf("The value returned is %d ",a);
free(top);
top=top->link; return (a);
}
}

Contoh program stack


#include stdio.h
void main()
{
int stack[100];
int top=-1;
int pilih, i;
do
{
printf( MENU\n );
printf( 1. PUSH\n2. POP\n3. VIEW\n4. EXIT\n );
printf( Pilih = ); scanf( %d , &pilih);
switch(pilih)
{
case 1://push
if (top > 100)
printf( Stack penuh!\n );
else
{ printf( Data = ); scanf( %d , &stack[top+1]);
top++;
}
break;
case 2://pop
if (top < 0)
printf( Stack kosong!\n );
else
{
printf( Data keluar = %d\n , stack[top]);
top ;
}
break;
case 3://view
for(i=top; i>=0; i )
printf( %d , stack[i]);
printf( \n );
break;
case 4:
printf( Exit \n );
break;
}
}while (pilih!=4);
}

Das könnte Ihnen auch gefallen