Vous êtes sur la page 1sur 17

DATA STRUCTURE

Term - Paper
ON
STACKS & QUEUES IMPLEMENTATION_

SUBMITTED TO: SUBMITTED BY:


SANDEEP KUMAR ANKUR SINGH
RE3801A29
CAP 204
Stack and queue

Stack and queue are both general-purpose abstract data types.


There are several and various applications, where stack or queue
is handy component to use. Stacks and queues are specialised
lists. There are two main differences between lists and stacks or
queues. The first one is that stack or queue has only restricted
operation functions of lists. Only few operations of list are
allowed for stack or queue. The second difference is that in the
case of stack and queue the order is always the order of entry.
//////////////////////////////////////////////////////////////
// //
// Implimentation Of STACK using LINKLIST //
// Created By //
// AnKuR sInGh //
// //
/////////////////////////////////////////////////////////////

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0

main()
{
struct node
{
int data,n;
struct node *link;
};

int n,item;
typedef struct node node;
node *temp,*sp=NULL;
while(1)
{
//clrscr();
printf("Please select your choice\n1. PUSH\n2. POP\n3. Search\n4. Display\n5. Ex!t\n");
scanf("%d",&n);

switch(n)
{
case 1:
printf("Enter the data to PUSH into Stack\n");
scanf("%d",&item);
temp=(node *) malloc(sizeof(node));
temp->data=item;
temp->link=sp;
sp=temp;
break;
case 2:
if(sp==NULL)
{
printf("\nSTACK underflow\n");
getch();
}
else
{
item=sp->data;
sp=sp->link;
printf("\nPOP Successful Removed Data Is %d",item);
getch();
}
break;
case 3:
if(sp==NULL)
{
printf("\nSTACK underflow\n");
}
else
{
temp=sp;
printf("\nEnter data to search from Stack");
scanf("%d",&item);
n=0;
while(temp->data!=item)
{
if(temp->link==NULL)
goto print;
temp=temp->link;
n++;
}
print:
if(temp->data==item)
{
printf("Data %d Found In Stack at position %d",item,n+1);
}
else
{
printf("Data not present in Stack");
}
getch();
break;
case 4:
if(sp==NULL)
{
printf("\nSTACK is empty\n");
}
else
{
temp=sp;
printf("\n%d",temp->data);
while(temp->link!=NULL)
{
temp=temp->link;
printf("\n%d",temp->data);
}
}
getch();
break;
case 5:
exit(0);
break;
}
}
}
}

// IMPLIMENTATION OF STACK USING ARRAYS


// ************************************

#include<stdlib.h>
#include<conio.h>
#define SIZE 3
int top=-1;

// TO DISPLAY ELEMENTS OF STACK


//****************************************************************************
void display(int s[])
{
int i=0;
if(top<0)
printf("\nSTACK IS EMPTY! ");
else
{
printf("\nNOW DISPLAYING STACK :- \n\n");
for(i=top;i>=0;i--)
printf(" %d ",s[i]);
}

// TO INSERT AN ELEMENT IN THE STACK


//****************************************************************************
void push(int s[])
{
int x;
if(top==(SIZE-1))
printf("\nSTACK IS FULL! [OVERFLOW]");
else
{
printf("\nENTER AN ELEMENT IN A STACK : ");
scanf("%d",&x);
top++;
s[top]=x;
display(s);
}
}

//TO REMOVE AN ELEMENT FROM THE STACK


//****************************************************************************
void pop(int arr[])
{
if(top<0)
printf("\nSTACK IS EMPTY! [UNDER FLOW] ");
else
{
printf("\nPOPPING ......ITEM [%d] \n",arr[top]);
top--;
display(arr);
}
}
void main()
{
int ch=0,arr[SIZE];
do{
clrscr();
printf("\n M * E * N * U\n");
printf(" ************* \n");
printf("\n[1] PUSH\n");
printf("\n[2] POP\n");
printf("\n[0] EXIT\n\n");
printf("\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1: push(arr);
break;
case 2: pop(arr);
break;
case 0: exit(0);
break;
default: printf("\n ENTERED WRONG KEY!");
}
getch();
}while(ch!=0);
}

//////////////////////////////////////////////////////////////
// //
// Implimentation Of QUEUE using LINKLIST //
// Created By //
// AnKuR sInGh //
// //
/////////////////////////////////////////////////////////////

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Queue{

struct Queue *node;


char item[1];
};

struct Queue *volatile q;

int QueueAdd(const char *_item)


{
int _q;
struct Queue *queue;
_q = sizeof(struct Queue) + strlen(_item) + 4;
queue = (struct Queue *)malloc(_q); /* allocate the space in memory we need */
if (queue == 0) return 1;
memset(queue, 0, _q);
strcpy(queue->item, _item);
queue->node = q;
q = queue;

return 0;
}
void print q()
{
struct Queue *qq;

int n,j,num;
n=0;
for (qq=q, num=0; qq; qq=qq->node, num++); /* enumerate items in queue */
if(num==NULL)
{
printf("No items currently in the Queue");
return;
}
printf("Here are the items in the Queue \n");
cycle:
for (j=0,qq=q; (j < n) && qq; qq=qq->node, j++); /* lets hop to the next node */
printf("%s \n",qq->item);
n++;
if(n<num)
goto cycle;
}
int main()
{
struct Queue *qq;
int i,num;
char in[100]; /* array of 100 bytes */
/* print to stdout */
printf("Welcome to the Queue!");
printf("\n Dequeue not implemented \n");
printf("Usage: \n");
input: /* input label */
printf("\n1. Add to the queue\n");
printf("2. Print number of items in Queue \n");
printf("3. Print the items in the Queue \n");
printf("4. Exit the program \n");

scanf("%d",&i); /* check for inputted data */


switch(i)
{
case 1:
printf("Enter Item to add to the Queue \n");
scanf("%s",&in);

QueueAdd(in); /* call 'QueueAdd', let us assume everything goes o.k. */


printf("%s successfully added to the queue",in);
goto input;
break;
case 2:
for (qq=q, num=0; qq; qq=qq->node, num++);
/* Null statement above to enumerate items in the queue */
printf("There are %d items in the queue",num);
goto input;
break;
case 3:
printq();
goto input;
break;
case 4:
printf("Exiting program");
exit(0);
break; /* why not? :) */

default:
printf("Tu esta estupido!");
goto input;
}
}
//******************************************************//
//*****IMPLIMENTATION OF QUEUE USING ARRAY*****//
//******************************************************//

#include<stdlib.h>
#include<conio.h>
#define SIZE 2
int rear=-1,front=-1;

//TO DISPLAY ELEMENTS OF QUEUE


//****************************************************************************
void display(int arr[])
{
int i=0;
if(front==-1)
printf("\n QUEUE IS EMPTY ");
else
{
printf("\nNOW DISPLAYING QUEUE : \n\n");
for(i=front;i<=rear;i++)
printf("%d ",arr[i]);
}
}

//TO INSERT AN ELEMENT IN A QUEUE


//****************************************************************************
void insert(int arr[])
{
int x;
if(rear==(SIZE-1))
printf("\nQUEUE IS FULL [OVERFLOW]");
else
{
printf("\nENTER AN ELEMENT IS A QUEUE : ");
scanf("%d",&x);
if(rear<0)
rear=front=0;
else
rear++;
arr[rear]=x;
display(arr);
}
}

//TO DELETE ELEMENTS OF QUEUE


//****************************************************************************
void del(int arr[])
{
if(front==-1)
printf("\nQUEUE IS EMPTY [UNDERFLOW] ");
else
{
printf("\nDELETING ....ITEM [%d] \n",arr[front]);
if(front==rear)
front=rear=-1;
else
front++;
display(arr);
}
}
void main()
{
int ch,arr[50];
do
{
clrscr();
printf("\n *M*E*N*U* ");
printf("\n *********\n");
printf("\n[1] INSERT\n");
printf("\n[2] DELETE\n");
printf("\n[0] EXIT\n");
printf("\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(arr);
break;
case 2: del(arr);
break;
default: exit(0);
}
getch();
}while(ch!=0);

Vous aimerez peut-être aussi