Data Structure Important Programs
Data Structure Important Programs
Data Structure Important Programs
#include <stdio.h>
#include<conio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
/* C Program to implement Binary Tree*/
#include<stdio.h>
#include<conio.h>
node *create()
{
node *p;
int x;
printf("Enter data(-1 for no node):");
scanf("%d",&x);
if(x==-1)
return NULL;
p=(node*)malloc(sizeof(node));
p->data=x;
printf("Enter left child of %d:\n",x);
p->left=create();
printf("Enter right child of %d:\n",x);
p->right=create();
return p;
}
void preorder(node *t)
{
if(t!=NULL)
{
printf(" %d",t->data);
preorder(t->left);
preorder(t->right);
}
}
void main()
{
clrscr();
node *root;
root=create();
printf("\nThe preorder traversal of tree is: ");
preorder(root);
printf("\nThe inorder traversal of tree is: ");
inorder(root);
printf("\nThe postorder traversal of tree is: ");
postorder(root);
getch();
}
/* CPP Program to find Predecessor and
Successor in a BST */
#include <iostream>
using namespace std;
struct Node
{
int key;
struct Node *left, *right;
};
if (root->right != NULL)
{
Node* tmp = root->right ;
while (tmp->left)
tmp = tmp->left ;
suc = tmp ;
}
return ;
}
if (root->key > key)
{
suc = root ;
findPreSuc(root->left, pre, suc, key) ;
}
else
{
pre = root ;
findPreSuc(root->right, pre, suc, key) ;
}
}
int main()
{
int key = 65;
Node *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
Node* pre = NULL, *suc = NULL;
#include<stdio.h>
#include<conio.h>
int main()
{
int array[50], n, i, j, temp;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for(i = 0; i < n; i++)
scanf("%d", &array[i]);
for (i = 0 ; i < ( n - 1 ); i++){
for (j= 0 ; j < n - i - 1; j++){
if(array[j] < array[j+1]){
temp=array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("Sorted list in descending order:\n");
for ( i = 0 ; i < n ; i++ )
printf("%d\n", array[i]);
return 0;
}
/* C Program to Convert Infix to Postfix Expression
using Stack */
#include<stdio.h>
#include<conio.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;
}
int main()
{
char exp[20];
char *e, x;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
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());
}
}
/* CPP Program for Insertion Operation in a
Binary Tree*/
#include <iostream.h>
#include <queue.h>
struct Node {
int key;
struct Node* left, *right;
};
inorder(temp->left);
cout << temp->key << " ";
inorder(temp->right);
}
void insert(struct Node* temp, int key)
{
queue<struct Node*> q;
q.push(temp);
while (!q.empty()) {
struct Node* temp = q.front();
q.pop();
if (!temp->left) {
temp->left = newNode(key);
break;
} else
q.push(temp->left);
if (!temp->right) {
temp->right = newNode(key);
break;
} else
q.push(temp->right);
}
}
int main()
{
struct Node* root = newNode(10);
root->left = newNode(11);
root->left->left = newNode(7);
root->right = newNode(9);
root->right->left = newNode(15);
root->right->right = newNode(8);
return 0;
}
/* C Program to implement Linear Search */
#include <stdio.h>
#include<conio.h>
int main()
{
int array[100], search, c, n;
return 0;
}
/* C Program for implementing Singly Linked List
*/
#include <stdio.h>
#include <conio.h>
void create();
void display();
void insert();
void clean();
void insert_begin();
void insert_end();
void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
typedef struct node
{
int data;
struct node *link;
}node;
node *init=NULL, *ptr, *temp;
int ch;
void main()
{
clrscr();
while(1){
printf("\n***SINGLE LINKED LIST OPERATIONS:****");
printf("\n MENU ");
printf("\n---------------------------------------");
printf("\n 1.Create ");
printf("\n 2.Display ");
printf("\n 3.Insert ");
printf("\n 4. Delete");
printf("\n 5.Exit ");
printf("\n--------------------------------------\n");
printf("Enter your choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert();
break;
case 4:
clean();
break;
case 5:
exit(0);
default:
printf("\n Wrong Choice:\n");
break;
}
}
}
void create()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
exit(0);
}
printf("\nEnter the data value for the node:\t");
scanf("%d",&temp->data);
temp->link=NULL;
if(init==NULL)
{
init=temp;
}
else
{
ptr=init;
while(ptr->link!=NULL)
{
ptr=ptr->link;
}
ptr->link=temp;
}
}
void display()
{
struct node *ptr;
if(init==NULL)
{
printf("\nList is empty:\n");
return;
}
else
{
ptr=init;
printf("\nThe List elements are:\n");
while(ptr!=NULL)
{
printf("%d\t",ptr->data );
ptr=ptr->link;
}
}
}
void insert()
{
int ch;
printf("\n1. Insert at beginning \n2. Insert at last \n3. Insert at
specific location");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_begin();
break;
case 2:
insert_end();
break;
case 3:
insert_pos();
break;
default:
printf("Invalid choice");
}
}
void insert_begin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->data);
temp->link =NULL;
if(init==NULL)
{
init=temp;
}
else
{
temp->link=init;
init=temp;
}
}
void insert_end()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->data);
temp->link =NULL;
if(init==NULL)
{
init=temp;
}
else
{
ptr=init;
while(ptr->link!=NULL)
{
ptr=ptr->link ;
}
ptr->link =temp;
}
}
void insert_pos()
{
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the position for the new node to be
inserted:\t");
scanf("%d",&pos);
printf("\nEnter the data value of the node:\t");
scanf("%d",&temp->data) ;
temp->link=NULL;
if(pos==0)
{
temp->link=init;
init=temp;
}
else
{
for(i=0,ptr=init;ilink;
if(ptr==NULL)
{
printf("\nPosition not found:[Handle with care]\n");
return;
}
}
temp->link =ptr->link ;
ptr->link=temp;
}
}
void delete_begin()
{
struct node *ptr;
if(ptr==NULL)
{
printf("\nList is Empty:\n");
return;
}
else
{
ptr=init;
init=init->link ;
printf("\nThe deleted element is :%d\t",ptr->data);
free(ptr);
}
}
void delete_end()
{
struct node *temp,*ptr;
if(init==NULL)
{
printf("\nList is Empty:");
exit(0);
}
else if(init->link ==NULL)
{
ptr=init;
init=NULL;
printf("\nThe deleted element is:%d\t",ptr->data);
free(ptr);
}
else
{
ptr=init;
while(ptr->link!=NULL)
{
temp=ptr;
ptr=ptr->link;
}
temp->link=NULL;
printf("\nThe deleted element is:%d\t",ptr->data);
free(ptr);
}
}
void delete_pos()
{
int i,pos;
struct node *temp,*ptr;
if(init==NULL)
{
printf("\nThe List is Empty:\n");
exit(0);
}
else
{
printf("\nEnter the position of the node to be deleted:\t");
scanf("%d",&pos);
if(pos==0)
{
ptr=init;
init=init->link ;
printf("\nThe deleted element is:%d\t",ptr->data );
free(ptr);
}
else
{
ptr=init;
for(i=0;ilink ;
if(ptr==NULL)
{
printf("\nPosition not Found:\n");
return;
}
}
temp->link =ptr->link ;
printf("\nThe deleted element is:%d\t",ptr->data);
free(ptr);
}
}
}
void clean()
{
int choice;
printf("\n1. Delete from beginning \n2. Delete from last \n3.
Delete specific location");
scanf("%d",&ch);
switch(ch)
{
case 1:
delete_begin();
break;
case 2:
delete_end();
break;
case 3:
delete_pos();
break;
default:
printf("Invalid choice");
}
}
/* CPP Program for Matrix Multiplication */
#include<iostream>
using namespace std;
int main ()
{
int r1, c1, r2, c2, i, j, k;
int A[5][5], B[5][5], C[5][5];
if (c1 != r2)
{
cout << "Matrices cannot be multiplied!";
exit(0);
}
return 0;
}
/* C Program for finding maximum and minimum
value in an array */
#include <stdio.h>
#include<conio.h>
int main()
{
int arr[MAX_SIZE];
int i, max, min, size;
max = arr[0];
min = arr[0];
return 0;
}
/* C Program for implementing Queue using static
array*/
#include <stdio.h>
#include<conio.h>
#define MAX 50
int queue[MAX];
int rear = - 1;
int front = - 1;
void insert();
void delete();
void display();
int main()
{
int ch;
do
{
printf("\n1.Insert element to queue \n");
printf("\n2.Delete element from queue \n");
printf("\n3.Display all elements of queue \n");
printf("\n4.Quit \n");
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(1);
default:
printf("\nWrong choice \n");
}
}
while(ch!=4);
return 0;
}
void insert()
{
int item;
if (rear == MAX - 1)
printf("\nQueue Overflow \n");
else
{
if (front == - 1)
front = 0;
printf("\nInset the element in queue : ");
scanf("%d", &item);
rear++;
queue[rear] = item;
}
}
void delete()
{
if (front == - 1 || front > rear)
{
printf("\nQueue Underflow \n");
return ;
}
else
{
printf("\nElement deleted from queue is : %d\n",
queue[front]);
front++;
}
}
void display()
{
int i;
if (front == - 1)
printf("\nQueue is empty \n");
else
{
printf("\nQueue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue[i]);
printf("\n");
}
}
/* C Program for implementing Stack using static
array */
#include<stdio.h>
#include<conio.h>
#define MAX 4
void push();
void pop();
void display();
int main()
{
int ch;
do
{
printf("\n\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. DISPLAY");
printf("\n 4. EXIT");
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:
display();
break;
case 4: exit(1);
}
}
while(ch!=4);
return 0;
}
void push()
{
if(top==MAX-1)
printf("\n Stack Overflow");
else
{
printf("\nEnter the element to be inserted: ");
scanf("%d",&item);
top++;
stack[top]=item;
printf("\nThe Element %d is inserted at the stack",item);
}
}
void pop()
{
if(top==-1)
{
printf("\nStack is Underflow");
}
else
{
item=stack[top];
top--;
printf("\nElement %d deleted from the Stack",item);
}
}
void display()
{
int i;
if(top==-1)
printf("Empty stack");
else
{
for(i=top;i>=0;i--)
{
printf("\n %d",stack[i]);
}
}
}