Home > Data Structures > C programs for implementation of stack, queue and linked lists

C programs for implementation of stack, queue and linked lists

November 12, 2010 Leave a comment Go to comments

FOR STACK

#include<stdio.h>
#define MAX_SIZE 20
void push(int a);
int pop();
void show();

int stack[MAX_SIZE];
int top=-1;
main()
{
int i,x;
printf(“1 for push 2 for pop and 3for exit”);
scanf(“%d”,&i);
switch(i)
{
case 1:
printf(“enter the element”);
scanf(“%d”,&x);
push(x);
show();
main();
break;
case 2:
x=pop();
printf(“element is %d\n”,x);
show();
main();
break;
case 3:
show();
}
system(“pause”);
}

void push(int x)
{
if(top==19){
printf(“full”);
exit(1);}
stack[++top]=x;
}

int pop()
{

if(top==-1){
printf(“empty”);
exit(1);
}
return(stack[top–]);
}

void show()
{
int b;
b=top;
while(b>=0)
printf(“%d\n”,stack[b–]);
}

FOR QUEUE

#include<stdio.h>

void enqueue(int x);
int dequeue();
void show();
int queue[5];
int front=0,rear=0;
main()
{
int i;
int x;
printf(“enter 1 for enter 2 for remove 3 for exit”);
scanf(“%d”,&i);
switch(i)
{
case 1:
printf(“\nEnter the element to enter”);
scanf(“%d”,&x);
enqueue(x);
show();
main();
break;
case 2:
x=dequeue();
printf(“\nThe item removed is %d\n”,x);
show();
main();
case 3:
show();

}
system(“pause”);
}
void enqueue(int x)
{

rear=(rear+1)%5;
if(rear==front){
printf(“\nFull”);
rear=(rear+4)%5;
main();
}
queue[rear]=x;
}

int dequeue()
{
if(front==rear)
{
printf(“\nEmpty…put elements first”);
main();
}
front=(front+1)%5;
return queue[front];
}

void show()
{
int r;
r=front;
printf(“The elements of the queue are\n”);
while(r!=rear)
{
printf(“%d\t”,queue[r+1]);
r++;
}
}

FOR LINKED LISTS

//Linked lists
#include<stdio.h>
typedef struct student node;
struct student
{
int key;
node *next;
};

void enter(node *s);
void print(node *p);

main()
{
node *p;
printf(“enter the list”);
p=(node *)malloc(sizeof(node));
enter(p);
printf(“\n\nthe list is\n”);
print(p);
printf(“\n”);
system(“pause”);

}

void enter(node* p)
{
char c;
printf(“\nenter the key:”);
scanf(“%d”,&p->key);
printf(“want to enter a new entry press y:”);
c=getch();
if(c==’y’ || c==’Y’)
{
p->next=(node *)malloc(sizeof(node));
enter(p->next);
}
else
{
p->next=NULL;

}
return;

}

void print(node *p)
{
if(p)
{
printf(“%d\n”,p->key);
print(p->next);
}

}

FOR CIRCULAR LINKED LISTS

//Circular Linked lists
#include<stdio.h>
typedef struct student node;
struct student
{
int key;
node *next;
};

node* enter(node *s);
void print(node *p);

main()
{
node *p,*q;
printf(“enter the list”);
p=(node *)malloc(sizeof(node));
q=enter(p);
q->next=p;
printf(“\n\nthe list is\n”);
print(p);
printf(“\n”);
system(“pause”);

}

node* enter(node* p)
{
char c;
enternode: printf(“\nenter the key:”);
scanf(“%d”,&p->key);
printf(“want to enter a new entry press y:”);
c=getch();
if(c==’y’ || c==’Y’)
{
p->next=(node *)malloc(sizeof(node));
p=p->next;
goto enternode;
}
else
{
return(p);

}
return;

}

void print(node *p)
{
node *q;
q=p;
do
{
if(p)
{
printf(“%d\n”,p->key);
p=p->next;
}
}while(p!=q);
}

#include<stdio.h>

void enqueue(int x);
int dequeue();
void show();
int queue[5];
int front=0,rear=0;
main()
{
int i;
int x;
printf(“enter 1 for enter 2 for remove 3 for exit”);
scanf(“%d”,&i);
switch(i)
{
case 1:
printf(“\nEnter the element to enter”);
scanf(“%d”,&x);
enqueue(x);
show();
main();
break;
case 2:
x=dequeue();
printf(“\nThe item removed is %d\n”,x);
show();
main();
case 3:
show();

}
system(“pause”);
}
void enqueue(int x)
{

rear=(rear+1)%5;
if(rear==front){
printf(“\nFull”);
rear=(rear+4)%5;
main();
}
queue[rear]=x;
}

int dequeue()
{
if(front==rear)
{
printf(“\nEmpty…put elements first”);
main();
}
front=(front+1)%5;
return queue[front];
}

void show()
{
int r;
r=front;
printf(“The elements of the queue are\n”);
while(r!=rear)
{
printf(“%d\t”,queue[r+1]);
r++;
}
}

Advertisements
Categories: Data Structures
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: