Académique Documents
Professionnel Documents
Culture Documents
OBJECTIVE:
To represent Stack using arrays and to perform operations like push, pop.
HARDWARE REQUIREMENTS:
PC (Pentium IV)
SOFTWARE REQUIREMENTS:
Turbo C Compiler
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define stack_size 5
void push (int value);
void pop();
void peek();
int size();
void view();
int stack[stack_size], top=-1;
void main()
{
int x, data, item;
clrscr();
printf("\n Representation of Stack:");
printf("\n1. Push, 2. Pop, 3. Peek, 4. Size, 5.View, 6. Exit");
while(1)
{
printf("\n Enter the choice:");
scanf("%d", &x);
switch(x)
{
case 1:
printf("\nEnter the element:");
scanf("%d", &data);
push(data);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
printf("\n stack size=%d", stack_size);
printf("\n Current stack size=%d", size());
break;
case 5:
view();
break;
default:
printf("\n End of the program");
exit(0);
}
}
getch();
}
int isfull()
{
extern int stack[], top;
if(top==stack_size-1)
return(1);
else
return(0);
}
int isempty()
{
extern int stack[], top;
if(top==-1)
return(1);
else
return(0);
}
void push(int value)
{
extern int stack[], top;
if(isfull())
printf("\n Stack is full");
else
{
top++;
stack[top]=value;
}
}
void pop()
{
int value;
extern int stack[], top;
if(isempty())
printf("\n Stack is empty");
else
{
value=stack[top];
printf("\n The popped value is %d", value);
top--;
}
}
void peek()
{
int item;
extern int stack[], top;
if(isempty(1))
printf("\n Stack is empty");
else
{
item=stack[top];
printf("\n The peek of the stack is %d", item);
}
}
int size()
{
extern int stack[], top;
if(isempty())
return(0);
else
return(top+1);
}
void view()
{
extern int stack[], top;
int f;
if(isempty())
printf("\n Stack is empty");
else
{
printf("\n Content of the stack is .... \n top-->");
for(f=top;f>=0;f--)
{
printf("%d-->", stack[f]);
}
}
if(isfull())
printf("\n Stack is full");
}
OUTPUT:
Representation of Stack:
1. Push, 2. Pop, 3. Peek, 4. Size, 5. View, 6. Exit
Stack is full.
Enter the choice: 3
Peek of the stack is: 50
RESULT:
The implementation of stack using array program is executed and the output
is verified.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define qsize 5
void enqueue(int value);
void dequeue();
void peek();
int size();
void view();
int queue[qsize], front=-1, rear=-1;
void main()
{
int choice, data, item;
clrscr();
printf("\n Representation of Linear Queue:");
printf("\n 1. Enqueue, 2. Dequeue, 3. Peek, 4. Size, 5. View, 6. Exit");
while(1)
{
printf("\n Enter the choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\n Enter the element:");
scanf("%d", &data);
enqueue(data);
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
printf("\n queue size = %d", qsize);
printf("\n Current queue size = %d", size());
break;
case 5:
view();
break;
default:
printf("\n End of the programme");
exit(0);
getch();
}
}
}
//getch();
int isfull()
{
extern int queue[], front, rear;
if(rear==(qsize-1))
return(1);
else
return(0);
}
int isempty()
{
extern int queue[], front, rear;
if(front==-1&&rear==-1)
return(1);
else
return(0);
}
void enqueue(int value)
{
extern int queue[], front, rear;
if(isfull())
printf("\n queue is full");
else
{
if(isempty())
front=rear=0;
else
rear=rear+1;
queue[rear]=value;
}
}
void dequeue()
{
int value;
extern int queue[], front, rear;
if (isempty())
printf("\n queue is empty");
else
{
value=queue[front];
printf("\n The dequeued value is %d", value);
}
if(front==rear)
front=rear=-1;
else
front=front+1;
}
void peek()
{
int item;
extern int queue[], front, rear;
if(isempty())
printf("\n queue is empty");
else
{
item=queue[front];
printf("\n The peek of the queue is %d", item);
}
}
int size()
{
extern int queue[], front, rear;
if(isempty())
return(0);
else
return(rear-front+1);
}
void view()
{
extern int queue[], front, rear;
int f;
if(isempty())
{
printf("\n queue is empty");
}
else
{
printf("\n content of the queue is .......\n Front-->");
for(f=front;f!=rear+1;f=f+1)
{
printf("%d-->", queue[f]);
}
printf("Rear");
}
if(isfull())
printf("\n queue is full");
OUTPUT:
Queue is full.
Enter the choice: 3
Peek of the Queue is: 50
PROGRAM:
#include<stdio.h>
#include<conio.h>
typedef struct stack
{
int data;
struct stack *next;
}node;
void push(int value);
void pop();
void display();
void peek();
void size();
int isempty();
node *get_node();
node *top;
void main()
{
int x,data,value;
top=NULL;
clrscr();
printf("\n Representation of stack using Linked List:");
printf("\n 1.push, 2.pop, 3.peek, 4.size, 5.view, 6.exit");
while(1)
{
printf("\n Enter your choice:");
scanf("%d", &x);
switch(x)
{
case 1:
printf("\n Enter the element to push:");
scanf("%d", &value);
push(value);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
size();
break;
case 5:
display();
break;
default:
printf("\n End of the program");
exit(0);
getch();
}
}
}
node *get_node()
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->next=NULL;
return(temp);
}
void pop()
{
extern node *top;
node *temp;
temp=top;
if(isempty())
printf("\n The stack underflow on pop");
else
{
printf("\n The popped value is %d", top->data);
top=top->next;
temp->next=NULL;
free(temp);
}
}
void peek()
{
extern node *top;
if(isempty())
printf("\n The stack is empty");
else
printf("\n The top most element is=%d", top->data);
}
void size()
{
extern node *top;
node *temp;
int count=0;
temp=top;
if(isempty())
printf("\n The stack is empty");
else
{
while(temp!=NULL)
{
count++;
temp=temp->next;
}
printf("\n The current size is=%d", count);
}
}
void display()
{
extern node *top;
int i;
node *temp;
temp=top;
if(isempty())
printf("\n The stack is empty");
else
{
printf("\n The stack is:\nTop");
while(temp!=NULL)
{
printf("-->%d", temp->data);
temp=temp->next;
}
}
}
int isempty()
{
extern node *top;
if(top==NULL)
return 1;
else
return 0;
}
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
typedef struct queue
{
int data;
struct queue *next;
}node;
void enqueue();
void dequeue();
void display();
void size();
void peek();
node *get_node();
node *front,*rear;
void main()
{
int ch,data;
front=NULL;
rear=NULL;
clrscr();
printf("Linear Queue implementation using Linked List:");
printf("\n1.ENQUEUE\n2.DEQUEUE\n3.VIEW\n4.SIZE\n5.PEEK\n6.EXIT");
while(1)
{
printf("\nEnter your choice...");
scanf("%d",&ch);
switch(ch)
{
case 1:enqueue();
break;
case 2:dequeue();
break;
case 3:display();
break;
case 4:size();
break;
case 5:peek();
break;
default:printf("\nEnd of program");
exit(0);
}
getch();
}
}
void enqueue()
{
extern node *front,*rear;
node *New;
int val;
New=get_node();
if(New==NULL)
{
printf("\nMemory not created");
return;
}
printf("\nEnter the element to Enqueue:");
scanf("%d",&val);
New->data=val;
New->next=NULL;
if(rear==NULL)
{
rear=New;
front=New;
}
else
{
rear->next=New;
rear=New;
}
}
node *get_node()
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->next=NULL;
return(temp);
}
void dequeue()
{
extern node *front,*rear;
node *temp;
int value;
temp=front;
if(isempty())
printf("\nThe QUEUE is empty");
else
{
value=front->data;
if(front==rear)
{
front=NULL;
rear=NULL;
}
else
{
front=front->next;
temp->next=NULL;
free(temp);
}
printf("\nThe Dequeued Elelement of the QUEUE is =%d",value);
}
}
void size()
{
extern node *front,*rear;
int count=0;
node *temp;
temp=front;
if(isempty())
printf("\nThe QUEUE is empty");
else
{
while(temp!=NULL)
{
count++;
temp=temp->next;
}
printf("\nThe size of the QUEUE is: %d",count);
}
}
void peek()
{
extern node *rear,*front;
if(isempty())
printf("\nThe QUEUE is empty");
else
printf("\nThe Front element of the QUEUE is =%d",front->data);
}
void display()
{
extern node *front,*rear;
int i;
node *temp;
temp=front;
if(isempty())
printf("\nThe QUEUE is empty");
else
{
printf("\nFRONT-->");
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->next;
}
printf("REAR");
}
}
int isempty()
{
extern node *front,*rear;
if(front==NULL)
return 1;
else
return 0;
}
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define TRUE 1
#define FALSE 0
typedef struct sll
{
int data;
struct sll *next;
}node;
node *create(node *);
node *insert(node *);
node *delet(node **);
node *search(node *,int);
void count(node *);
void display(node *);
node *get_node();
void main()
{
int choice,val;
node *head;
clrscr();
printf("\nRepresentation of singly linked list:");
printf("\n1.create\n2.insert\n3.delete\n4.search\n5.display\n6.count\n7.exit");
while(1)
{
printf("\n\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: head=create(head);
break;
case 2: head=insert(head);
break;
case 3: head=delet(&head);
break;
case 4: printf("Enter the element to search");
scanf("%d",&val);
search(head,val);
break;
case 5: display(head);
break;
case 6: count(head);
break;
default:printf("\nEnd of program");
exit(0);
}
}
}
node *get_node()
{
node *temp;
temp=(node*)malloc(sizeof(node));
temp->next=NULL;
return(temp);
}
OUTPUT:
Do u want to continue:(y/n)y
Enter the element: 20
Do u want to continue:(y/n)y
Enter the element: 30
Do u want to continue:(y/n)n
The singly linked list is created
SHELL SORT
AIM:
To sorting the given numbers using Shell sort.
OBJECTIVE:
HARDWARE REQUIREMENTS:
PC (Pentium IV)
SOFTWARE REQUIREMENTS:
Turbo C Compiler
PROGRAM:
#include<stdio.h>
#include<conio.h>
void shell_sort(int a[],int n);
void main()
{
int n,i,a[100];
clrscr();
printf("\t\t\n enter the size of the elements");
scanf("%d",&n);
printf("\t\t\n enter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
shell_sort(a,n);
printf("\t\t\n the sorted list is:");
for(i=0;i<n;i++)
{
printf("\t\n %d",a[i]);
}
getch();
}
void shell_sort(int a[],int n)
{
int i,j,k,temp;
for(k=n/2;k>0;k=k/2)
{
for(i=k;i<n;i++)
{
temp=a[i];
for(j=i;j>=k&&a[j-k]>temp;j=j-k)
{
a[j]=a[j-k];
}
a[j]=temp;
getch();
}
}
}
OUTPUT:
OUTPUT:
PROGRAM:
#include<stdio.h>
#include<conio.h>
void heap(int a[],int n);
void create(int a[],int n);
void main()
{
int a[50],i,n;
clrscr();
printf("enter the limit:");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
heap(a,n);
printf("The sorted list using HEAP is ...\n\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
void create(int a[],int n)
{
int i,j,q,key;
for(q=i;q<n;q++)
{
i=q;
key=a[q];
j=(int)(i/2);
while((i>0)&&key>a[j])
{
a[i]=a[j];
i=j;
j=(int)(i/2);
if(j<0)
j=0;
}
a[i]=key;
}
}
void heap(int a[],int n)
{
int i,j,q,key,temp;
create(a,n);
for(q=n-1;q>=1;q--)
{
temp=a[0];
a[0]=a[q];
a[q]=temp;
i=0;
key=a[0];
j=1;
if(a[j+1]<q)
if(a[j+1]>a[j])
j=j+1;
while(j<=(q-1)&&(a[j]>key))
{
a[i]=a[j];
i=j;
j=2*i;
if((j+1)<q)
if(a[j+1]>a[j])
j=j+1;
else if(j>n-1)
j=n-1;
a[i]=key;
}
}
}
OUTPUT:
11 16 18 40 43
10 12 18 19 41 42
MERGE SORT
PROGRAM:
#include<stdio.h>
#include<conio.h>
void mergesplit(int a[],int first,int last);
void merge(int a[],int f1,int l1,int f2,int l2);
int a[25],b[25];
void main()
{
int i,n;
clrscr();
printf("enter the limit:");
scanf("%d",&n);
printf("Enter the elements");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesplit(a,0,n-1);
printf("The sorted list using MERGE sort is ...\n\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
void mergesplit(int a[],int first,int last)
{
int mid;
if(first<last)
{
mid=(first+last)/2;
mergesplit(a,first,mid);
mergesplit(a,mid+1,last);
merge(a,first,mid,mid+1,last);
}
}
10 15 17 39 42
15 18 21 28 40 43
LINEAR SEARCH:
#include<stdio.h>
#include<conio.h>
svoid main()
{
int i,j,k,n,a[100],f=1;
clrscr();
printf("\t\t\t ENTER THE SIZE OF SORTING:");
scanf("%d",&n);
printf("\t\t\t ENTER THE ELEMENTS OF THE ARRAY:\n\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\t\t\t ENTER THE ELEMENTS TO BE SEARCHED:");
scanf("%d",&k);
for(i=0;i<=n;i++)
{
if(k==a[i])
{
printf("\n\n ELEMENTS %d IS IN THE POSITION %d",k,i+1);
f=2;
break;
}
}
if(f==1)
printf("\n THE ENTERED ELEMENT IS NOT IN THE ARRAY");
getch();
}
OUTPUT:
11 19 18 12 14
BINARY SEARCH :
#include<stdio.h>
#include<conio.h>
void main()
int a[100],m,i,n,l,h,t,f=1;
clrscr();
printf("\t\t\n ENTER THE SIZE OF ARRAY");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&t);
l=0;
h=n-1;
while(l<=h)
m=(l+h)/2;
if(t<a[m])
h=m-1;
else if(t>a[m])
l=m+1;
else
f=2;
break;
} if(f==1)
getch();