Data structure
Data structure
Stack
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
int top = -1, a[MAX];
void push();
void pop();
void show();
int main()
{
int choice;
while (1)
{
printf("\n1.Push the element\n2.Pop the element\n3.Show\n4.End");
printf("\n\nEnter the choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
exit(0);
default:
printf("\nInvalid choice!!");
}
}
}
void push()
{
int x;
if (top == MAX - 1)
printf("\nOverflow!!");
else
{
printf("\nEnter the element to be added onto the stack: ");
scanf("%d", &x);
top++;
a[top] = x;
}
}
void pop()
{
if (top == -1)
printf("Underflow!!\n");
else
{
printf("\nPopped element: %d", a[top]);
--top;
}
}
void show()
{
if (top == -1)
printf("Stack is Empty!!\n");
else
{
printf("\nElements present in the stack: \n");
for (int i = top; i >= 0; i--)
printf("%d\n",a[i]);
}
}
2. Queue
#include <stdio.h>
#include<stdlib.h>
#define SIZE 10
void enqueue();
void dequeue();
void display();
int arr[SIZE];
int rear = - 1;
int front = - 1;
int main()
{
int choice;
while (1)
{
printf("\n1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \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(1);
default:
printf("Wrong choice \n");
}
}
}
void enqueue()
{
int item;
if (rear == SIZE - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
front = 0;
printf("Enter the element to be inserted: ");
scanf("%d",&item);
rear = rear + 1;
arr[rear] = item;
}
}
void dequeue()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", arr[front]);
front = front + 1;
}
}
void display()
{
if (front == - 1 || front > rear)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (int i = front; i <= rear; i++)
printf("%d ", arr[i]);
printf("\n");
}
}
3. Infix to Postfix
#include<stdio.h>
#include<ctype.h>
#include<string.h>
char stack[20];
int top=-1;
void push(char x)
{
stack[++top]=x;
}
char pop()
{
if(top==-1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x=='(')
return 0;
if(x=='+' || x=='-')
return 1;
if(x=='*' || x=='/')
return 2;
if(x=='$')
return 3;
}
int main()
{
char exp[20];
char *e,x;
int op=0,opr=0;
printf("Enter the Infix Expression:: ");
scanf("%s", exp);
for (int i = 0; i < strlen(exp); i++)
{
if(isalnum (exp[i]))
op++; //counting number of operands
else
opr++; //counting number of operators
}
if(opr != (op-1))
printf("Invalid Expression!");
else
{
e=exp;
printf("\nPostfix Expression: ");
while(*e!='\0')
{
if(isalnum(*e))
printf("%c", *e);
else if(*e=='(')
push(*e);
else if(*e==')')
{
while((x=pop()) != '(')
printf("%c", x);
}
else
{
while(priority(stack[top])>=priority(*e))
printf("%c", pop());
push(*e);
}
e++;
}
while(top!=-1)
printf("%c", pop());
}
printf("\n");
}
4. Postfix Evaluation
#include<stdio.h>
#include<ctype.h>
int stack[20];
int top=-1;
void push(int x)
{
stack[++top]=x;
}
int pop()
{
return stack[top--];
}
int main()
{
char exp[20];
char *e;
int n1,n2,n3,num;
printf("\nEnter the postfix expression\n");
scanf("%s",exp);
e=exp;
while(*e!='\0')
{
if(isdigit(*e))
{
num=*e-48;
push(num);
}
else
{
n1=pop();
n2=pop();
switch(*e)
{
case '+':
n3=n2+n1;
break;
case '-':
n3=n2-n1;
break;
case '*':
n3=n2*n1;
break;
case '/':
n3=n2/n1;
break;
}
push(n3);
}
e++;
}
printf("the result of expression %s=%d",exp,pop());
return 0;
}
5. Insertion Sort
#include <stdio.h>
void insertionSort(int a[], int n)
{
// Starting from the second element
for (int i = 1; i < n; i++)
{
int key = a[i];
int j = i - 1;
/* Move elements of arr[0..i-1], that are greater than key, to one position to
the right of their current position */
while (j >= 0 && a[j] > key)
{
a[j + 1] = a[j];
j = j - 1;
}
// Move the key to its correct position
a[j + 1] = key;
}
}
int main()
{
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n];
printf("Enter the array elements:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
insertionSort(a, n);
printf("\nSorted array: \n");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
return 0;
}
6. Seleection Sort
#include <stdio.h>
void selectionSort(int a[], int n)
{
int i, j, min, temp;
for (i = 0; i < n-1; i++)
{
min = i;
for (j = i+1; j < n; j++)
{
if (a[j] < a[min])
{
min = j;
}
}
temp = a[min];
a[min] = a[i];
a[i] = temp;
}
}
int main()
{
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n];
printf("Enter the array elements:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
selectionSort(a, n);
printf("Sorted array: \n");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
return 0;
}
7. Bubble Sort
#include <stdio.h>
void bubbleSort(int a[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (a[j] > a[j + 1]) {
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
int main()
{
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int a[n];
printf("Enter the array elements:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
bubbleSort(a, n);
printf("\nSorted array: \n");
for (i = 0; i < n; i++)
{
printf("%d ", a[i]);
}
return 0;
}
8. Merge Sort
#include <stdio.h>
#include <stdlib.h>
void mergesort(int,int);
void merge(int,int,int);
int a[100];
int main() {
int i,n;
printf("enter the range : ");
scanf("%d",&n);
printf("\nEnter the %d numbers : ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(0,n-1);
printf("\nAfter sorting : ");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
9. Linear Search
#include <stdio.h>
int main() {
int n,arr[100],key;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
printf("enter the key element:");
scanf("%d",&key);
int i = linearSearch(arr, n, key);
if (i == -1)
printf("Key Not Found");
else
printf("Key Found at Index: %d", i);
return 0;
}
int main() {
int array[100],target,n,i;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
printf("enter the target element:");
scanf("%d",&target);
int result = binarySearch(array, n ,target);
if (result == -1)
printf("Element is not present in the array”);
else
printf(“Element is present at index %d\n", result);
return 0;
}