Data Structure File
Data Structure File
Data Structure File
Code:
#include<stdio.h>
#include<conio.h>
int la[20],k,j,ch,n,item,maxm,minm;
void insert();
void delete();
void display();
void max();
void min();
void sort();
void lin_search();
void bin_search();
int main()
{
printf("How many elements you want to insert in an array:");
scanf("%d",&n);
printf("\n Enter the %d elements:",n);
for(j=1;j<=n;j++)
{
scanf("%d",&la[j]);
}
do{
for(j=1;j<=n;j++)
{
printf("%d\n",la[j]);
}
}
void delete()
{
int j;
for(j=k;j<=n-1;j++)
{
la[j]=la[j+1];
}
n=n-1;
display();
}
void max()
{
maxm=la[1];
for(j=1;j<=n;j++)
{
if(maxm<la[j])
{
maxm=la[j];
}
}
printf("\nGreatest number is:%d\n",maxm);
}
void min()
{
minm=la[1];
for(j=1;j<=n;j++)
{
if(minm>la[j])
{
minm=la[j];
}
}
printf("\nSmallest number is:%d\n",minm);
}
void sort()
{
for(k=1;k<=n-1;k++)
{
for(j=0;j<n-k;j++)
{
if(la[j]>la[j+1])
{
item= la[j];
la[j]=la[j+1];
la[j+1]=item;
}
}
}
display();
}
void lin_search()
{
int loc=0;
printf("Enter the element to be searched:");
scanf("%d",&item);
for(j=1;j<=n;j++)
{
if(item == la[j])
{
loc = j;
printf("Item %d is present at location %d in array\
n",item,loc);
}
else
{
loc = loc+1;
}
}
if(loc==0)
{
printf("Item is not present in array");
}
}
void bin_search()
{
int beg,end,mid,loc;
printf("Enter the element to be searched:");
scanf("%d",&item);
beg=1;
end =n;
mid= (beg+end)/2;
while(beg<=end&&la[mid]!=item)
{
if(item<la[mid])
end = mid - 1;
else
beg = mid + 1;
mid= (beg+end)/2;
}
if(la[mid]=item)
{
loc = mid;
printf("The element %d is present at %d location\n",item,loc);
}
else
printf("The number is not present\n");
Output:
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
printf("\t%d\t",c[i][j]);
}
printf("\n");
}
}
Output:
5. Transpose of matrix
do
{
printf("\nEnter");
printf("\n1.Insertion at begning");
printf("\n2.Insertion at the end");
printf("\n3.Insertion at given location");
printf("\n4.Insertion into sorted list");
printf("\n5.Deletion from beginning");
printf("\n6.Deletion from end");
printf("\n7.Deletion from given location:");
printf("\n8.Deleting an item from list");
printf("\n9.Reversing of linked list ");
printf("\n10.Display");
printf("\n11.Exit");
printf("\nWhich operation you want to perform:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the element you want to insert:");
scanf("%d",&item);
beginsert(item);
break;
case 2:printf("\nEnter the element you want to insert:");
scanf("%d",&item);
lastinsert(item);
break;
case 3: printf("\nEnter the item you want to insert:");
scanf("%d",&item);
insertloc(item);
break;
case 4: printf("\nEnter the item you want to insert:");
scanf("%d",&item);
sortinsert(item);
break;
case 5:begdelete();
break;
case 6: enddelete();
break;
case 7: locdelete();
break;
case 8: printf("\nEnter the item you want to delete:");
scanf("%d",&item);
itemdelete(item);
break;
case 9: reverse();
break;
case 10: display();
break;
}
}while(ch<12);
}
void beginsert(int item)
{
new1=(struct node*)malloc(sizeof(struct node*));
new1->info=item;
new1->link=start;
start =new1;
printf("Element inserted\n\n");
}
void insertloc(int item)
{
int loc,ctr=1;
ptr=start;
printf("Enter the location where you want to insert the element:");
scanf("%d",&loc);
while(ctr<loc)
{
ptr=ptr->link;
ctr+=1;
}
new1 = (struct node*)malloc(sizeof(struct node*));
new1->info=item;
new1->link=ptr->link;
ptr->link=new1;
printf("Item is inserted at location %d\n",&loc);
display();
}
void lastinsert(int item)
{
ptr=start;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
new1 = (struct node*)malloc(sizeof(struct node*));
new1->info=item;
new1->link=NULL;
ptr->link=new1;
printf("Item is inserted at end\n");
display();
}
void sortinsert(int item)
{
struct node *save;
if(item<start->info)
{
new1=(struct node*)malloc(sizeof(struct node*));
new1->info=item;
new1->link=start;
start=new1;
printf("\n Item has been inserted" );
}
else
if(item>start->info)
{
save =start;
ptr=start->link;
while(ptr!=NULL)
{
if(item<ptr->info)
{
new1=(struct node*)malloc(sizeof(struct node*));
new1->info=item;
new1->link=save->link;
save->link=new1;
while(ptr!=NULL)
{
ptr=ptr->link;
}
}
else
{
save=ptr;
ptr=ptr->link;
if(ptr==NULL)
{
new1=(struct node*)malloc(sizeof(struct node*));
new1->info=item;
new1->link=NULL;
save->link=new1;
}
}
}
}
}
void reverse()
{
struct node *prev;
struct node *next;
next = prev = NULL;
ptr = start;
while(ptr!= NULL)
{
next = ptr->link;
ptr->link = prev;
prev = ptr;
ptr = next;
}
start = prev;
printf("Reverse linked list is:\n");
display();
}
void begdelete()
{
ptr = start;
start = start->link;
new1->info=ptr->info;
ptr->link=new1;
ptr=new1;
printf("Element is deleted\n");
display();
}
void enddelete()
{
struct node *save;
struct node *ptr1;
save = start;
ptr = start->link;
while(ptr!=NULL)
{
ptr1 = save;
save = ptr;
ptr= ptr->link;
}
ptr1->link=NULL;
printf("Element is deleted\n");
display();
}
void locdelete()
{
int loc;
printf("Enter the location from where you want to delete the element:");
scanf("%d",&loc);
struct node *save;
save = start;
ptr = start->link;
while(ptr<loc)
{
save = ptr;
ptr = ptr->link;
}
save->link=NULL;
save->link= ptr->link;
display();
}
void itemdelete(int item)
{
struct node *loc;
struct node *locp;
struct node *save;
if(start==NULL)
{
loc = NULL;
locp = NULL;
}
else if(start->info ==item)
{
loc = start;
locp = NULL;
}
else
{
save = start;
ptr = start->link;
while(ptr != NULL)
{
if(ptr->info == item)
{
loc = ptr;
locp = save;
locp ->link = loc->link;
}
save = ptr ;
ptr = ptr->link;
}
}
}
void display()
{
ptr = start;
while(ptr!=NULL)
{
printf("%d->",ptr->info);
ptr= ptr->link;
}
printf("\n");
}
Output:
Program 4. Write a program to create Stack using array and perform the
operations.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void push(int);
void pop();
void display();
int item;
int top;
int stack[20];
int main()
{
int i,n,ch;
printf("\nEnter the number of elements you need in a stack:");
scanf("%d",&n);
top = n;
printf("Enter the elements:");
for(i=1;i<=n;i++)
{
scanf("%d",&stack[i]);
}
do
{
printf("\nEnter");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("\nEnter the item to insert(push):");
scanf("%d",&item);
push(item);
break;
case 2: pop();
break;
case 3: display();
break;
}
}while(ch<4);
}
void push(int item)
{
int maxsize= 7;
if(top==maxsize)
{
printf("Overflow");
}
else
{
top = top + 1;
stack[top]= item;
printf("\nItem is inserted at location %d\n",top);
}
display();
}
void pop()
{
int item;
if(top==NULL)
{
printf("Underflow");
}
else
{
item = stack[top];
top = top - 1;
printf("\nItem is deleted\n");
}
display();
}
void display()
{
int i;
for(i=1;i<=top;i++)
{
printf("\t%d",stack[i]);
printf("\n");
}
}
Output:
Program 5. Write a program to create Stack using Linked List and perform the
operations.
Code:
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;
void main ()
{
int choice=0;
do
{
printf("\nEnter");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: printf("Exiting");
break;
};
}while(choice<4);
}
void push ()
{
int val;
struct node *ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("not able to push the element");
}
else
{
printf("\nEnter the item to push:");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Element %d is pushed\n",val);
}
}
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
{
printf("Underflow");
}
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped\n");
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d->",ptr->val);
ptr = ptr->next;
}
printf("\n");
}
}
Output:
1. Performing Push operation
2. Performing pop operation
Program 6. Write a program to evaluate a Postfix expression.
Code:
while(top!=-1)
{
printf("%c",pop());
}
return 0;
}
Output:
Program 8. Write a program to check whether the parenthesis are balanced or
not.
Code:
#include<stdio.h>
int main()
{
char expression[50];
int x=0, i=0;
printf("\nEnter an expression :");
scanf("%s", expression);
while(expression[i]!= '\0')
{
if(expression[i]=='(')
{
x++;
}
else if(expression[i]==')')
{
x--;
if(x<0)
break;
}
i++;
}
if(x==0)
{
printf("\nParenthesis are balanced");
}
else
{
printf("\nParenthesis are unbalanced");
}
return 0;
}
Output:
}
else
{
item = queue[front];
if(front == rear)
{
front = -1;
rear = -1 ;
}
else
{
front = front + 1;
}
printf("\nvalue deleted ");
}
}
void display()
{
int i;
if(rear == -1)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values\n");
for(i=front;i<=rear;i++)
{
printf("%d\n",queue[i]);
}
}
}
Output:
1. Performing Enqueue
2. Performing dequeue
Program 10. Write a program to perform operation on queues using Linked
List.
Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
do
{ printf("\nEnter");
printf("\n1.Enqueue");
printf("\n2.Dequeue");
printf("\n3.Display the queue");
printf("\n4.Exit");
printf("\nEnter your choice :");
scanf("%d",& choice);
switch(choice)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
break;
default:
printf("\nEnter valid choice\n");
}
} while(choice<4);
}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value:\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
printf("Item %d is inserted\n",item);
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
printf("Item %d is inserted\n",item);
}
}
}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
printf("\nElement is deleted\n");
display();
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values\n");
while(ptr != NULL)
{
printf("%d->",ptr -> data);
ptr = ptr -> next;
}
printf("\n");
}
}
Output:
1. Performing Enqueue
2. Performing dequeue
Program 11. Write a program to perform Quicksort.
Code:
#include<stdio.h>
#include<conio.h>
void quicksort(int arr[], int low, int high);
int partition(int arr[], int low, int high);
void swap(int* a, int* b);
int i,j;
int main() {
int n;
printf("\nEnter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for ( i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Perform quicksort
quicksort(arr, 0, n - 1);
printf("\nSorted array:\n");
for ( i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
// Function to perform quicksort
void quicksort(int arr[], int low, int high) {
if (low < high) {
// Find the partition index
int pi = partition(arr, low, high);
// Recursively sort the two subarrays
quicksort(arr, low, pi - 1);
quicksort(arr, pi + 1, high);
}
}
// Function to partition the array
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
// Function to swap two elements
void swap(int* a, int* b)
{int temp = *a;
*a = *b;
*b = temp;
}
Output:
Output:
printf("\nSorted array:\n");
displayArray(arr, n);
return 0;
}
// Function to perform selection sort
void selectionSort(int arr[], int n)
{
int min_idx;
for (i = 0; i < n - 1; i++)
{
// Find the minimum element in the unsorted part of the array
min_idx = i;
for (j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
// Function to display the elements of an array
void displayArray(int arr[], int n)
{
for (i = 0; i < n; i++)
{
printf("\t%d ", arr[i]);
printf("\n");
}
}
Output: