You are on page 1of 9

Gilbert L.

Getuya
IT 203 – Programming Paradigms
Assignment No. 1

Implement Stack on the following Programming Languages.(Code and .2


Screenshot)
Stack in C

>include <stdio.h#
>include <graphics.h#
>include <dos.h#
>include<conio.h#
>include<process.h#

define SIZE 6#

{static struct stack


;int top
;int items[SIZE]
;s1}

;void push(struct stack *,int)


;int pop(struct stack*)
;int stacktop(struct stack*)
;int empty(struct stack*)
;int search(struct stack *,int)
;void insert(struct stack *,int,int)
;void replace(struct stack *,int,int)
;void del(struct stack *,int)
;void printline(void)
;void startscreen(int,int)
;void shell(int,int)
;void close(void)
;)(void start_screen
;)(void press_esc

;int gd=DETECT,gm,maxx,maxy

)(void main
{
;char c
;int i,temp
;s1.top=-1
;initgraph(&gd,&gm,"f:\\tc\\bgi")
;)(maxx=getmaxx
;)(maxy=getmaxy
;startscreen(maxx,maxy)
;)(start_screen
;shell(maxx,maxy)
:START
;)(c=getch
switch (c)
{
;case '1': gotoxy(3,17)
;printf("What element do you wish to push?")
;scanf("%d",&temp)
;push(&s1,temp)
;break
;case '2': gotoxy(3,17)
;pop(&s1)
;break
;case '3': gotoxy(3,17)
;printf("Enter the element you wish to search?")
;scanf("%d",&temp)
;temp=search(&s1,temp)
;gotoxy(5,19)
if (temp==-1)
;printf("Sorry! No results Found!")
else
;printf("The element is at index %d",temp)
;)(getch
;break
;case '4': gotoxy(3,17)
;printf("Enter the POSTION and ELEMENT you wish to insert?")
;scanf("%d %d",&i,&temp)
;insert(&s1,i,temp)
;break
;case '5': gotoxy(3,17)
;printf("Enter the POSTION and ELEMENT you wish to replace?")
;scanf("%d %d",&i,&temp)
;replace(&s1,i,temp)
;break
;case '6': gotoxy(3,17)
;printf("Enter the POSTION you wish to delete from stack?")
;scanf("%d",&temp)
;del(&s1,temp)
;break
;default : gotoxy(3,17)
;printf("Invalid entry! Try again!")
;break
}
for (i=0;i<=6;i++)
{
;gotoxy(3,17+i)
;printf("\t\t\t\t\t\t\t\t\t")
}
;gotoxy(3,17)
;printf("Do You wish to contine?")
;)(c=getch
if (c=='y')
{
;printf("\t\t\t\t\t") gotoxy(3,17);
;goto START
}
;)(closegraph
;)(close
;)(restorecrtmode
}

void push(struct stack *sx,int x)


{
if (sx->top==SIZE-1)
{
;gotoxy(5,22)
;printf("\tSTACT OVERFLOW\n")
;)(getch
;return
}
;sx->items[++sx->top]=x
;gotoxy(66,14-2*sx->top)
;printf("%d",x)
}

int pop(struct stack *sx)


{
if (empty(sx))
{
;gotoxy(5,22)
;printf("\t STACK UNDERFLOW\n")
;)(getch
;return(0)
}
;gotoxy(66,14-2*sx->top)
;)" "(printf
;return(sx->items[sx->top--])
}

int stacktop(struct stack *sx)


{
;return(sx->items[sx->top])
}

int empty(struct stack *sx)


{
;return((sx->top==-1))
}

int search(struct stack *sx,int n)


{
;int arr[20]
;int i=0,j,result=-1
for (j=sx->top;!(empty(sx));j--)
{
if (n==stacktop(sx))
{
;result=sx->top
;break
}
;arr[i++]=pop(sx)
}
for (j=i-1;j>=0;j--)
;push(sx,arr[j])
;return(result)
}

void insert(struct stack *sx,int pos,int ele)


{
;int arr[20]
;int i=0,j
if (pos>sx->top)
{
;gotoxy(3,19)
;printf("Invalid Position!")
;)(getch
;return
}
for (j=sx->top;j>=pos;j--)
;arr[i++]=pop(sx)
;push(sx,ele)
for (j=i-1;j>=0;j--)
;push(sx,arr[j])
}

void replace(struct stack *sx,int pos,int ele)


{
;int arr[20]
;int i=0,j
if (pos>sx->top)
{
;gotoxy(3,19)
;printf("Invalid Position!")
;)(getch
;return
}
for (j=sx->top;j>=pos;j--)
;arr[i++]=pop(sx)
;push(sx,ele)
for (j=i-2;j>=0;j--)
;push(sx,arr[j])
}

void del(struct stack *sx,int pos)


{
;int arr[20]
;int i=0,j
if (pos>sx->top)
{
;gotoxy(3,19)
;printf("Invalid Position!")
;)(getch
;return
}
for (j=sx->top;j>pos;j--)
;arr[i++]=pop(sx)
;pop(sx)
for (j=i-1;j>=0;j--)
;push(sx,arr[j])
}

void printline(void)
{
;int i
for (i=0;i<40;i++)
;)"¯®"(printf
}

Sample Output
>include <iostream.h#
>include <graphics.h#
>include <dos.h#
>include<conio.h#
>include<process.h#

define SIZE 6#

{static struct stack


;int top
;int items[SIZE]
;s1}

;void push(struct stack *,int)


;int pop(struct stack*)
;int stacktop(struct stack*)
;int empty(struct stack*)
;int search(struct stack *,int)
;void insert(struct stack *,int,int)
;void replace(struct stack *,int,int)
;void del(struct stack *,int)
;void printline(void)
;void startscreen(int,int)
;void shell(int,int)
;void close(void)
;)(void start_screen
;)(void press_esc

;int gd=DETECT,gm,maxx,maxy

)(void main
{
;char c
;int i,temp
;s1.top=-1
;initgraph(&gd,&gm,"f:\\tc\\bgi")
;)(maxx=getmaxx
;)(maxy=getmaxy
;startscreen(maxx,maxy)
;)(start_screen
;shell(maxx,maxy)
:START
;)(c=getch
switch (c)
{
;case '1': gotoxy(3,17)
;"?cout<<"What element do you wish to push
;cin<<"%d",&temp
;push(&s1,temp)
;break
;case '2': gotoxy(3,17)
;pop(&s1)
;break
;case '3': gotoxy(3,17)
;"?cout<<"Enter the element you wish to search
;cin<<"%d",&temp
;temp=search(&s1,temp)
;gotoxy(5,19)
if (temp==-1)
;printf("Sorry! No results Found!")
else
;printf("The element is at index %d",temp)
;)(getch
;break
;case '4': gotoxy(3,17)
;"?cout<<"Enter the POSTION and ELEMENT you wish to insert
;cin<<"%d %d",&i,&temp
;insert(&s1,i,temp)
;break
;case '5': gotoxy(3,17)
;"?cout<<"Enter the POSTION and ELEMENT you wish to replace
;cin<<"%d %d",&i,&temp
;replace(&s1,i,temp)
;break
;case '6': gotoxy(3,17)
;"?cout<<"Enter the POSTION you wish to delete from stack
;cin<<"%d",&temp
;del(&s1,temp)
;break
;default : gotoxy(3,17)
;"!cout<<"Invalid entry! Try again
;break
}
for (i=0;i<=6;i++)
{
;gotoxy(3,17+i)
;printf("\t\t\t\t\t\t\t\t\t")
}
;gotoxy(3,17)
;"?cout<<"Do You wish to contine
;)(c=getch
if (c=='y')
{
;printf("\t\t\t\t\t") gotoxy(3,17);
;goto START
}
;)(closegraph
;)(close
;)(restorecrtmode
}

void push(struct stack *sx,int x)


{
if (sx->top==SIZE-1)
{
;gotoxy(5,22)
;"cout<<"\tSTACT OVERFLOW\n
;)(getch
;return
}
;sx->items[++sx->top]=x
;gotoxy(66,14-2*sx->top)
;printf("%d",x)
}

int pop(struct stack *sx)


{
if (empty(sx))
{
;gotoxy(5,22)
;"cout<<"\t STACK UNDERFLOW\n
;)(getch
;return(0)
}
;gotoxy(66,14-2*sx->top)
;)" "(printf
;return(sx->items[sx->top--])
}

int stacktop(struct stack *sx)


{
;return(sx->items[sx->top])
}

int empty(struct stack *sx)


{
;return((sx->top==-1))
}

int search(struct stack *sx,int n)


{
;int arr[20]
;int i=0,j,result=-1
for (j=sx->top;!(empty(sx));j--)
{
if (n==stacktop(sx))
{
;result=sx->top
;break
}
;arr[i++]=pop(sx)
}
for (j=i-1;j>=0;j--)
;push(sx,arr[j])
;return(result)
}

void insert(struct stack *sx,int pos,int ele)


{
;int arr[20]
;int i=0,j
if (pos>sx->top)
{
;gotoxy(3,19)
;"!cout<<"Invalid Position
;)(getch
;return
}
for (j=sx->top;j>=pos;j--)
;arr[i++]=pop(sx)
;push(sx,ele)
for (j=i-1;j>=0;j--)
;push(sx,arr[j])
}

void replace(struct stack *sx,int pos,int ele)


{
;int arr[20]
;int i=0,j
if (pos>sx->top)
{
;gotoxy(3,19)
;"!cout<<"Invalid Position
;)(getch
;return
}
for (j=sx->top;j>=pos;j--)
;arr[i++]=pop(sx)
;push(sx,ele)
for (j=i-2;j>=0;j--)
;push(sx,arr[j])
}

void del(struct stack *sx,int pos)


{
;int arr[20]
;int i=0,j
if (pos>sx->top)
{
;gotoxy(3,19)
;"!cout<<"Invalid Position
;)(getch
;return
}
for (j=sx->top;j>pos;j--)
;arr[i++]=pop(sx)
;pop(sx)
for (j=i-1;j>=0;j--)
;push(sx,arr[j])
}

void printline(void)
{
;int i
for (i=0;i<40;i++)
;)"¯®"(printf
}

Sample Output
Java

;*.import java.util

{ public class Stack


{ public static void main(String[] args)
;)(Stack stack=new Stack
;stack.push(new Integer(10))
;stack.push("a")
;System.out.println("The contents of Stack is" + stack)
;System.out.println("The size of an Stack is" + stack.size())
;System.out.println("The number poped out is" + stack.pop())
;System.out.println("The number poped out is " + stack.pop())
;System.out.println("The number poped out is" + stack.pop())//
;System.out.println("The contents of stack is" + stack)
;System.out.println("The size of an stack is" + stack.size())
}
}

Sample Output

You might also like