Data Structure
Data Structure
LINEAR SEARCH
#include<stdio.h>
void main()
{ int a[10],search,i,n,flag=0,count;
printf("number of elements in an array");
scanf("%d",&n);
printf("enter integers in array");
for(i=0;i<n;i++)
{ scanf("%d",&a[i]); }
printf("enter the element to be search");
scanf("%d",&search);
for(i=0;i<n;i++)
{
if (a[i]==search)
{
flag=1;
count++;
}
}
if (flag==1)
{
printf("the element is found ");
}
else
{
printf("the element is not found");
} }
2.BINARY SEARCH
#include<stdio.h>
void main()
{int a[10],n,i,low=0,high,j,temp,mid,k,pos=-1;
printf("enter how many elements:");
scanf("%d",&n);
printf("enter array elements:");
for(i=0;i<n;i++)
{ scanf("%d",&a[i]); }
for(i=0;i<n;i++)
{ for(j=i+1;j<n;j++)
{ if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
} } }
printf("the sorted array is:\n");
for(i=0;i<n;i++)
{
printf("%d \n",a[i]);
}
printf("enter search element\n");
scanf("%d",&k);
low=0;
high=n-1;
mid=(low+high)/2;
while(low<=high)
{
if(a[mid]==k)
{ pos=mid;
printf("element found");
break; }
else if(a[mid]>k)
{ high=mid-1; }
else if(a[mid]<k)
{ low=mid+1; }
mid=(low+high)/2; }
if(pos==-1)
{ printf("element not found"); } }
3.STACK (ARRAY)
#include <stdio.h>
int top=-1,A[20],size=3;
void push();
void pop();
void show();
void main()
{
int choice,flag=0;
printf("STACK of elements\n");
while(flag==0)
{
printf("\n1.PUSH ELEMENT\n2.POP ELEMENT\n3.SHOW STACK\n4.END
OPERATION\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:show();
break;
case 4:
flag=1;
printf("\nThe program has ended\n");
break;
default:
printf("\nInvalid Choice\n");
}
}
}
void push()
{
if(top<size-1)
{
top++;
printf("\nenter the element :\n");
scanf("%d",&A[top]);
}
else
{
printf("\nOVERFLOW\n");
}
}
void pop()
{
int x;
if(top==-1)
{
printf("\nUNDERFLOW\n");
}
else
{
x=A[top];
top--;
printf("\nThe popped element is : %d\n",x);
}
}
void show()
{ printf("\nThe elements of the stack is :\n");
for(int i=0;i<=top;i++)
{
printf("\t%d",A[i]);
} printf("\n"); }
4. QUEUE (ARRAY)
#include<stdio.h>
#define size 3
void insert();
void delete();
void display();
void exit();
int max[size];
int rear=-1;
int front=-1;
int main()
{
int choice;
while(1)
{
printf("1.insertion\n");
printf("2.deletetion\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("invalid choice\n");
}
}
}
void insert()
{
int insert_item;
if(rear==size-1)
printf("overflow\n");
else
{
if(front==-1)
front=0;
printf("insert th element in queue\n");
scanf("%d",&insert_item);
rear=rear+1;
max[rear]=insert_item; } }
void delete()
{
if(front==-1||front>rear)
{ printf("queue underflow");
return; }
else
{ printf("element deleted from queue is: %d\n",max[front]);
front=front+1;
printf("insert th element in queue\n");
scanf("%d",&insert_item);
rear=rear+1;
max[rear]=insert_item; } }
void delete()
{
if(front==-1||front>rear)
{ printf("queue underflow");
return; }
else
{ printf("element deleted from queue is: %d\n",max[front]);
front=front+1; } }
void display()
{ int i;
if(front==-1)
printf("queue is empty\n");
else
{ printf("queue is:\n");
for(i=front;i<=rear;i++)
printf("%d",max[i]);
printf("\n"); }}
5. CIRCULAR QUEUE
#include<stdio.h>
int a[100],n,choice,flag=0,ch;
void enqueue();
void dequeue();
void display();
void exit();
int rear=-1;
int front=-1;
int main()
{ printf("enter array limit");
scanf("%d",&n);
do
{
printf("1.enqueue\n");
printf("2.dequeue\n");
printf("3.display\n");
printf("4.exit\n");
printf("enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("invalid choice\n");
} }
while(choice!=4);
}
void enqueue()
{ int item;
if(front==-1&&rear==-1)
{ front=0;
rear=0;
a[rear]=item; }
else if(front==(rear+1)%n)
{ printf("\n overflow\n"); }
else
{ rear=(rear+1)%n;
a[rear]=item;
}
printf("enter the element to be inserted");
scanf("%d",&item); }
void dequeue()
{ int item;
if(front==-1&&rear==-1)
{ printf("\n underflow\n"); }
else if(front==rear)
{ item=a[front];
front=-1;
rear=-1; }
else
{ item=a[front];
front=(front+1)%n; }}
void display()
{
{ if(rear==-1 && front==-1)
{ printf("\nThe queue is empty.\n"); }
else if(rear<front)
{ printf("\nThe elements of the queue are:\n");
for(int i=rear;i<=front;i++)
{ if(i==' ')
printf(" ");
else
printf("%d\t",a[i]);}
printf("\n"); }
else
{ printf("\nThe elements of the queue are:\n");
for(int i=front;i<=rear;i++)
{ printf("%d\t",a[i]); }
printf("\n"); } } }
6. STACK (LINKED LIST)
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
struct node *ptr;
}*top,*top1,*temp;
int count = 0;
void push(int data) {
if (top == NULL)
{ top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data; }
else
{ temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp; }
count++;
printf("Node is Inserted\n\n"); }
int pop() {
top1 = top;
if (top1 == NULL)
{ printf("\nStack Underflow\n");
return -1; }
else
top1 = top1->ptr;
int popped = top->info;
free(top);
top = top1;
count--;
return popped; }
void display() {
top1 = top;
if (top1 == NULL)
{ printf("\nStack Underflow\n");
return; }
printf("The stack is \n");
while (top1 != NULL)
{ printf("%d\t", top1->info);
top1 = top1->ptr;} }
int main() {
int choice, value;
printf("\nImplementation of Stack using Linked List\n");
while (1) {
printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("\nEnter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2: printf("Popped element is :%d\n", pop());
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
} }}
7. POLYNOMIAL ADDITION
#include<stdio.h>
struct poly
{int coeff;
int expo;
};
struct poly p1[10], p2[10], p3[10];
int readPoly(struct poly[]);
int addPoly(struct poly[], struct poly[], int, int, struct poly[]);
void displayPoly(struct poly[], int terms);
int main(){
int t1, t2, t3;
t1 = readPoly(p1);
printf(" \n First polynomial : ");
displayPoly(p1, t1);
t2 = readPoly(p2);
printf(" \n Second polynomial : ");
displayPoly(p2, t2);
t3 = addPoly(p1, p2, t1, t2, p3);
printf(" \n\n Resultant polynomial after addition : ");
displayPoly(p3, t3);
printf("\n");
return 0;
}
int readPoly(struct poly p[10])
{ int t1, i;
printf("\n\n Enter the total number of terms in the polynomial:");
scanf("%d", &t1);
printf("\n Enter the COEFFICIENT and EXPONENT in DESCENDING ORDER\n");
for (i = 0; i < t1; i++) {
printf(" Enter the Coefficient(%d): ", i + 1);
scanf("%d", &p[i].coeff);
printf(" Enter the exponent(%d): ", i + 1);
scanf("%d", &p[i].expo);
}
return (t1);
}
int addPoly(struct poly p1[10], struct poly p2[10], int t1, int t2, struct poly p3[10])
{int i, j, k;
i = 0;
j = 0;
k = 0;
while (i < t1 && j < t2) {
if (p1[i].expo == p2[j].expo) {
p3[k].coeff = p1[i].coeff + p2[j].coeff;
p3[k].expo = p1[i].expo;
i++;
j++;k++;
}
else if (p1[i].expo > p2[j].expo) {
p3[k].coeff = p1[i].coeff;
p3[k].expo = p1[i].expo;
i++;
j++;k++;
}
return (k);
struct node {
int data;
struct node * next;
};