Data structue
Data structue
\#include<conio.h>
int top=-1;
int stack[10];
//defination of push function
void push(int size)
{
int x,i;
for(i=0;i<size;i++)
{
if(top==size-1)
{
printf("\n stack is overflow");
}
else
{
top=top+1;
printf("\n Enter the value in stack");
scanf("%d",&x);
stack[top]=x;
}
}
}
//display function
void display(int size)
{
int i;
if(top>=0)
{
for(i=top;i>=0;i--)
{
printf("\n stack=%d",stack[i]);
}
}
}
//defination of pop function
void pop()
{
if(top==-1)
{
printf("\n stack is empty");
}
else
{
int x=top;
top=top-1;
printf("\n The number which is popped is at index=%d",x);
printf("\n The number which is popped is=%d",stack[x]);
}
}
void main()
{
int size;
clrscr();
printf("\n Enter the number of element:");
scanf("%d",&size);
printf("\n stack is starting at index 0");
printf("\n Push function is called");
push(size);
display(size);
printf("\n Pop function is called");
pop();
getch();
}
-------output--------
Assignment 3
Que .Write a program to implement queue using static method.
#include<stdio.h>
#include<conio.h>
#define N 5
int queue[N];
int front=-1,rear=-1;
void enqueue()
{
int x;
printf("\n Enter the elements in queqe=");
scanf("%d",&x);
if(rear==N-1)
{
printf("\n Queue is overflow");
}
else if(front==-1 && rear==-1)
{
front=rear=0;
queue[rear]=x;
}
else
{
rear++;
queue[rear]=x;
}
}
void dequeue()
{
if(front==-1 && rear==-1)
{
printf("\n Queue is empty");
}
else if(front==rear)
{
printf("\n Dequeue element from queue=
%d",queue[front]);
front=rear=-1;
}
else
{
printf("\n Dequeu element from queue=
%d",queue[rear]);
front++;
}
}
void display()
{
int i;
if(front==1 && rear==-1)
{
printf("\n Queue is empty");
for(i=front;i<rear+1;i++)
{
printf("%d",queue[i]);
}
}
}
void peek()
{
if(front==-1 && rear==-1)
{
printf("\n Queue is empty");
}
else
{
printf("\n Element in first place is=");
printf("%d",queue[front]);
}
}
void main()
{
int n,i;
clrscr();
printf("\n Enter how many element in queue=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
enqueue();
}
printf("\n\n Display function called");
display();
printf("\n\n Dequeue function called");
dequeue();
printf("\n\n Display function called");
display();
printf("\n\n peek function called");
peek();
getch();
}
-------output--------
Assignment 7
Que. Write a program to implement Stack and Queue
using dynamically of method.
A)Stack
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*prev;
};
struct node*top=0;
void push()
{
int x,data,prev;
struct node*newnode;
printf("\n Enter the element in stack=");
scanf("%d",&x);
newnode=(struct node*)malloc(sizeof(struct
node));
newnode->data=x;
newnode->prev=top;
top=newnode;
}
void display()
{
struct node*temp;
temp=top;
if(top==0)
{
printf("\n stack is empty");
}
else
{
while(temp!=0)
{
printf("\n stack=%d",temp->data);
temp=temp->prev;
}
}
}
void pop()
{
struct node*temp;
temp=top;
if(top==0)
{
printf("\n stack is empty");
}
else
{
printf("\n\n Popped element=%d",top->data);
top=top->prev;
free(temp);
}
}
void main()
{
int i,size;
clrscr();
printf("\n Enter how many element you wnat to
push in stack=");
scanf("%d",&size);
for(i=0;i<size;i++)
{
push();
}
printf("\n Display function called(LIFO)");
display();
printf("\n\n Pop function is called");
pop();
getch();
}
-------output--------
B) Queue
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
};
struct node*front=0;
struct node*rear=0;
void enqueue()
{
int x;
struct node*newnode;
printf("\n enter element in queue=");
scanf("%d",&x);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=x;
newnode->next=0;
if(front==0 && rear==0)
{
front=rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
}
void dequeue()
{
struct node*temp;
temp=front;
if(front==0 && rear==0)
{
printf("\n queueis empty");
}
else
{
printf("\n ele dequeue from queue=%d",front-
>data);
front=front->next;
free(temp);
}
}
void display()
{
struct node*temp;
if(front==0 && rear==0)
{
printf("\n queue is empty");
}
else
{
temp=front;
printf("\nEle in queue are=");
while(temp!=0)
{
printf("%d",temp->data);
temp=temp->next;
}
}
}
void peek()
{
if(front==0 && rear==0)
{
printf("\n queue is empty");
}
else
{
printf("\n display first ele in queue=&d",front-
>data);
}
}
void main()
{
int n,i;
clrscr();
printf("\n enter how many ele in queue=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
enqueue();
}
printf("\n\n display function called called");
display();
printf("\n\n dequeue function called");
dequeue();
printf("\n\n display function called");
display();
printf("\n\n peek function called");
peek();
getch();
}
-------output--------
Assignment 8
Que .Write a program to sort given elements using bubble sort,insertion
sort,selection sort.
A) Bubble sort
#include<stdio.h>
#include<conio.h>
void main()
{ int i,j,temp,arr[5],n;
clrscr();
printf("enter the element");
scanf("%d",&n);
printf("enter array elements");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
//Bubble sort
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("sorted element:\n");
for(i=0;i<n;i++)
{
printf("%d\n",arr[i]);
}
getch();
}
-------output-------
B)Insertion sort
#include<stdio.h>
#include<conio.h>
void main()
{
int n,arr[5],i,j,key;
clrscr();
printf("enter the element:");
scanf("%d",&n);
printf("enter array element:");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
//insertion sort
for(i=1;i<n;i++)
{
key=arr[i];
j=i-1;
while(j>=0 && key<=arr[j])
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
}
printf("\n sorted array is \n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
getch();
}
-------output-------
C)Selection sort
#include<stdio.h>
#include<conio.h>
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
void main()
{
int arr[5],i,j,min,n;
clrscr();
printf("\n enter number of element:");
{
scanf("%d",&n);
}
printf("enter array element:");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
//selection sort
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(arr[j]<arr[min])
{
min=j;
}
if(min!=i)
{
swap(&arr[i],&arr[min]);
}
}
}
printf("sorted element;");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
getch();
}
-------output-------
Assignment 9
Que. Write a program to search given element using linear search.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,arr[10],x,i;
clrscr();
printf("\n Enter number of element:");
scanf("%d",&n);
printf("\n Enter array element:");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\n Enter the number for search");
scanf("%d",&x);
//linear search
for(i=0;i<n;i++)
{
if(arr[i]==x)
{
printf("\n Number is present in array at
location %d",i+1);
break;
}
}
if(i==n)
{
printf("\n number is not present in array");
}
getch();
}
-------output-------
Assignment 10
Que. Write a program to search given element using binary search.
}
else
{
r=mid-1;
}
return -1;
}
}
//main function
void main()
{ int arr[10],n,ele,found_num,i;
clrscr();
printf("\n Enter the element");
scanf("%d",&n);
printf("\n Enter array elements");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("\n Enter the element for search");
scanf("%d",&ele);
found_num=binarysearch(arr,0,n-1,ele);
if(found_num==-1)
{
printf("\n Element not found");
}
else
{
printf("\n Element found at
index:%d",found_num);
}
getch();
}
-------output-------