0% found this document useful (0 votes)
186 views

Data Structure Important Programs

The document contains code snippets for various data structures and algorithms problems in C and C++ including: 1) Binary search and binary tree traversal algorithms 2) Predecessor and successor functions in a binary search tree 3) Sorting arrays using bubble sort and descending order 4) Converting infix to postfix expressions using a stack 5) Insertion operations in a binary tree 6) Linear search on an array 7) Implementing a singly linked list with functions for creation, insertion, deletion and traversal.

Uploaded by

Chirag Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
186 views

Data Structure Important Programs

The document contains code snippets for various data structures and algorithms problems in C and C++ including: 1) Binary search and binary tree traversal algorithms 2) Predecessor and successor functions in a binary search tree 3) Sorting arrays using bubble sort and descending order 4) Converting infix to postfix expressions using a stack 5) Insertion operations in a binary tree 6) Linear search on an array 7) Implementing a singly linked list with functions for creation, insertion, deletion and traversal.

Uploaded by

Chirag Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

/* C Program to implement Binary Search */

#include <stdio.h>

#include<conio.h>

int main()
{
int c, first, last, middle, n, search, array[100];

printf("Enter number of elements\n");


scanf("%d",&n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d",&array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last) {


if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

return 0;
}
/* C Program to implement Binary Tree*/

#include<stdio.h>
#include<conio.h>

typedef struct node


{
int data;
struct node *left;
struct node *right;
} node;

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 inorder(node *t)


{
if(t!=NULL)
{
inorder(t->left);
printf(" %d",t->data);
inorder(t->right);
}
}

void postorder(node *t)


{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf(" %d",t->data);
}
}

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;
};

void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)


{
if (root == NULL) return ;
if (root->key == key)
{
if (root->left != NULL)
{
Node* tmp = root->left;
while (tmp->right)
tmp = tmp->right;
pre = tmp ;
}

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) ;
}
}

Node *newNode(int item)


{
Node *temp = new Node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

Node* insert(Node* node, int key)


{
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}

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;

findPreSuc(root, pre, suc, key);


if (pre != NULL)
cout << "Predecessor is " << pre->key;
else
cout << "No Predecessor";
if (suc != NULL)
cout << "Successor is " << suc->key;
else
cout << "No Successor";
return 0;
}
/* C Program to sort an array in descending order
using bubble sort */

#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>

using namespace std;

struct Node {
int key;
struct Node* left, *right;
};

struct Node* newNode(int key)


{
struct Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return temp;
};

void inorder(struct Node* temp)


{
if (!temp)
return;

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);

cout << "Inorder traversal before insertion:";


inorder(root);

int key = 12;


insert(root, key);

cout << endl;


cout << "Inorder traversal after insertion:";
inorder(root);

return 0;
}
/* C Program to implement Linear Search */

#include <stdio.h>
#include<conio.h>

int main()
{
int array[100], search, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);

printf("Enter a number to search\n");


scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d isn't present in the array.\n", search);

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];

cout << "Enter number of rows and columns of matrix A : ";


cin >> r1 >> c1;
cout << "Enter number of rows and columns of matrix B : ";
cin >> r2 >> c2;

if (c1 != r2)
{
cout << "Matrices cannot be multiplied!";
exit(0);
}

cout << "Enter elements of matrix A : ";


for (i = 0; i < r1; i++)
for (j = 0; j < c1; j++)
cin >> A[i][j];

cout << "Enter elements of matrix B : ";


for (i = 0; i < r2; i++)
for (j = 0; j < c2; j++)
cin >> B[i][j];
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
C[i][j] = 0;
for (k = 0; k < r2; k++)
{
C[i][j] += A[i][k] * B[k][j];
}
}
}

cout << "Product of matrices\n";


for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
cout << C[i][j] << " ";
cout << "\n";
}

return 0;
}
/* C Program for finding maximum and minimum
value in an array */

#include <stdio.h>
#include<conio.h>

#define MAX_SIZE 100

int main()
{
int arr[MAX_SIZE];
int i, max, min, size;

printf("Enter size of the array: ");


scanf("%d", &size);

printf("Enter elements in the array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

max = arr[0];
min = arr[0];

for(i=1; i<size; i++)


{
if(arr[i] > max)
{
max = arr[i];
}
if(arr[i] < min)
{
min = arr[i];
}
}

printf("Maximum element = %d\n", max);


printf("Minimum element = %d", min);

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

int stack[MAX], top=-1, item;

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");

printf("\n\n Enter your choice ");


scanf("%d",&ch);

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]);
}
}
}

You might also like