Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 42

DATA STRUCTURES LAB

PRACTICAL FILE

RAHUL KHANNA
760/IT/14

IT II

Rahul Khanna
760/IT/14

INDEX

Q1. Write a Program to search an Element in an array using iterative.


Q2. Write a Program to search an Element in an array using recursion.
Q3. Write a Program to Implement Bubble sort.
Q4. Write a Program to implement selection sort.
Q5. Write a Program to implement insertion sort.
Q6. Write a Program to implement quick sort.
Q7. Write a Program to implement merge sort.
Q8. Write a Program to implement stack using array.
Q9. Write a Program to implement queue using array.
Q10. Program to Create linked list & display the elements of the list.
Q11. Program to illustrate the various linked list operations
Q12. Program to illustrate the various doubly linked list operations.
Q13. Program to implement binary tree using linked list.
Q14. Write a program to simulate various graph traversing techniques.
Q15. Write a program to convert infix to postfix expression using stack.
Q16. Write a program to evaluate postfix expression using stacks.
Q17. Write a program to simulate various tree transversal techniques.

IT II

Rahul Khanna
760/IT/14

IT II

Rahul Khanna
760/IT/14

Q1. Write a program to search an Element in an array using iterative binary search

#include <stdio.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 is not present in the list.\n", search);
return 0;
}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q2. Write a program to search an Element in an array using recursive binary search

#include<stdio.h>
#include<stdlib.h>
#define size 10
int binsearch(int[], int, int, int);
int main() {
int num, i, key, position;
int low, high, list[size];
printf("\nEnter the total number of elements");
scanf("%d", &num);
printf("\nEnter the elements of list :");
for (i = 0; i < num; i++) {
scanf("%d", &list[i]);
}
low = 0;
high = num - 1;
printf("\nEnter element to be searched : ");
scanf("%d", &key);
position = binsearch(list, key, low, high);
if (position != -1) {
printf("\nNumber present at %d", (position + 1));
} else
printf("\n The number is not present in the list");
return (0);
}
int binsearch(int a[], int x, int low, int high) {
int mid;
if (low > high)
return -1;
mid = (low + high) / 2;
if (x == a[mid]) {
return (mid);
} else if (x < a[mid]) {
binsearch(a, x, low, mid - 1);
} else {
binsearch(a, x, mid + 1, high);
}}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q3. Write a Program to Implement Bubble sort.

#include <stdio.h>
int main()
{
int data[100],i,n,step,temp;
printf("Enter the number of elements to be sorted: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("%d. Enter element: ",i+1);
scanf("%d",&data[i]);
}
for(step=0;step<n-1;++step)
for(i=0;i<n-step-1;++i)
{
if(data[i]>data[i+1])
{
temp=data[i];
data[i]=data[i+1];
data[i+1]=temp;
}
}
printf("In ascending order: ");
for(i=0;i<n;++i)
printf("%d ",data[i]);
return 0;
}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q4. Write a Program to implement Selection sort.

#include <iostream>
using namespace std;
void print (int [], int);
void selection_sort (int [], int);
int main ()
{
int min_ele_loc;
int ar[] = {10, 2, 45, 18, 16, 30, 29, 1, 1, 100};
cout << "Array initially : ";
print (ar, 10);
selection_sort (ar, 10);
cout << "Array after selection sort : ";
print (ar, 10);
return 0;
}
void selection_sort (int ar[], int size)
{
int min_ele_loc;
for (int i = 0; i < 9; ++i)
{
min_ele_loc = i;
for (int j = i + 1; j < 10; ++j)
{
if (ar[j] < ar[min_ele_loc])
min_ele_loc = j;
}
swap (ar[i], ar[min_ele_loc]);
}
}
void print (int temp_ar[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << temp_ar[i] << " ";
}
cout << endl;
}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q5. Write a Program to implement Insertion sort.

#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
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]);
}
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t
= array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d ", array[c]);
}
return 0;
}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q6. Write a Program to implement quick sort.

#include<stdio.h>
void quicksort(int [10],int,int);
int main(){
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q7. Write a Program to implement merge sort.

#include<stdio.h>
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);
void partition(int arr[],int low,int high);
int main(){
int merge[MAX],i,n;
printf("Enter the total number of elements: ");
scanf("%d",&n);
printf("Enter the elements which to be sort: ");
for(i=0;i<n;i++){
scanf("%d",&merge[i]);
}
partition(merge,0,n-1);
printf("After merge sorting elements are: ");
for(i=0;i<n;i++){
printf("%d ",merge[i]);
}
return 0;
}
void partition(int arr[],int low,int high){
int mid;
if(low<high){
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
mergeSort(arr,low,mid,high);
}
}
void mergeSort(int arr[],int low,int mid,int high){
int i,m,k,l,temp[MAX];
l=low;
i=low;
m=mid+1;
while((l<=mid)&&(m<=high)){
if(arr[l]<=arr[m]){
temp[i]=arr[l];
l++;
}
else{
temp[i]=arr[m];
m++;
}
i++;
}
if(l>mid){

IT II

Rahul Khanna
760/IT/14

for(k=m;k<=high;k++){
temp[i]=arr[k];
i++;
}
}
else{
for(k=l;k<=mid;k++){
temp[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++){
arr[k]=temp[k];
}
}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q8. Write a Program to implement a stack using an array.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
struct stack {
int s[size];
int top;
} st;
int stfull() {
if (st.top >= size - 1)
return 1;
else
return 0;}
void push(int item) {
st.top++;
st.s[st.top] = item;
}
int stempty() {
if (st.top == -1)
return 1;
else
return 0;
}
int pop() {
int item;
item = st.s[st.top];
st.top--;
return (item);
}
void display() {
int i;
if (stempty())
printf("\nStack Is Empty!");
else {
for (i = st.top; i >= 0; i--)
printf("\n%d", st.s[i]); }}
int main() {
int item, choice;
char ans;
st.top = -1;
printf("\n\tImplementation Of Stack");
do {
printf("\nMain Menu");
printf("\n1.Push \n2.Pop \n3.Display \n4.exit");
printf("\nEnter Your Choice");
scanf("%d", &choice);
switch (choice) {
case 1:

IT II

Rahul Khanna
760/IT/14

printf("\nEnter The item to be pushed");


scanf("%d", &item);
if (stfull())
printf("\nStack is Full!");
else
push(item);
break;
case 2:
if (stempty())
printf("\nEmpty stack!Underflow !!");
else {
item = pop();
printf("\nThe popped element is %d", item);
}
break;
case 3:
display();
break;
case 4:
exit(0);
}
printf("\nDo You want To Continue?");
ans = getche();
} while (ans == 'Y' || ans == 'y');
return 0;
}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q9. Write a Program to implement a queue using an array.


#include<stdio.h>
#include<conio.h>
#define MAX 10
int queue[MAX],front=-1,rear=-1;
void insert_element();
void delete_element();
void display_queue();
int main()
{
int option;
do
{
printf("\n\n 1.Insert an element");
printf("\n 2.Delete an element");
printf("\n 3.Display queue");
printf("\n 4.Exit");
printf("\n Enter your choice: ");
scanf("%d",&option);
switch(option)
{
case 1: insert_element();
break;
case 2: delete_element();
break;
case 3: display_queue();
break;
case 4: return 0;
} }while(option!=4);
}
void insert_element()
{ int num;
printf("\n Enter the number to be inserted: ");
scanf("%d",&num);
if(front==0 && rear==MAX-1)
printf("\n Queue OverFlow Occured");
else if(front==-1&&rear==-1)
{
front=rear=0;
queue[rear]=num;
}
else if(rear==MAX-1 && front!=0)
{
rear=0;
queue[rear]=num;}
else
{
rear++;
queue[rear]=num;

IT II

Rahul Khanna
760/IT/14

}}
void delete_element()
{
int element;
if(front==-1)
{
printf("\n Underflow");
}
element=queue[front];
if(front==rear)
front=rear=-1;
else
{
if(front==MAX-1)
front=0;
else
front++;
printf("\n The deleted element is: %d",element);
}}
void display_queue()
{ int i;
if(front==-1)
printf("\n No elements to display");
else
{
printf("\n The queue elements are:\n ");
for(i=front;i<=rear;i++)
printf("\t %d",queue[i]); }}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q11.Linked List Operations:

i) Program to illustrate the operations of singly linked list.


ii) Program to search for an element in linked list using recursion.
iii). Program to display nodes of a linked list in reverse using recursion.
iv). Program to print alternate nodes in a linked list using recursion.
v). Program to print middle most node a linked list.
vi). Program to remove duplicates from a linked list.

#include<iostream>
using namespace std;
struct node
{
int value;
node * addr;
};
node* createlist()
{
node *head=0;
node*current,*prev;
int value;
cin>>value;
while(value!=-1)
{
current=new node;
current->value=value;
current->addr=0;
if(head==0)
{
head=current;
}
else
{
prev->addr=current;
}
prev=current;
cin>>value;
}
return head;

IT II

Rahul Khanna
760/IT/14

}
node* findk(node*head,int k)
{
while(k&&head)
{
head=head->addr;
k--;
}
return head;
}
void insertatk(node*&head,int k)
{
if(k==0)
{
node*inserted=new node;
inserted->addr=head;
head=inserted;
cin>>inserted->value;
}
else
{
node*inserted=new node;
node*prev=findk(head,k-1);
inserted->addr=prev->addr;
prev->addr=inserted;
cin>>inserted->value;
}
}
int lengthoflinkedlist(node *head)
{
int l=0;
while(head)
{
head=head->addr;
l++;
}
return l;
}
node *reverselinkedlistrec(node * &head,node* current,node*prev)
{
if(current==0)
{
head=prev;
return head;
}
node* nextaddr=current->addr;
head=reverselinkedlistrec(head,nextaddr,current);
current->addr=prev;
return head;
}
void searchel(node * head,int el)
{

IT II

Rahul Khanna
760/IT/14

while(head)
{
if(head->value==el)
{
cout<<"found";
return;
}
head=head->addr;
}
cout<<"not found";
return;
}
node* middle(node * head)
{
node *sptr=head,*fptr=head;
while(fptr&&fptr->addr)
{
sptr=sptr->addr;
fptr=fptr->addr->addr;
}
return sptr;
}
void printlistrec(node * head,node* current,node*prev)
{
if(current==0)
{
return;
}
node* nextaddr=current->addr;
cout<<current->value<<' ';
printlistrec(head,nextaddr,current);
}
void alternateprintlistrec(node* head)
{
if(!head)
{
return;
}
cout<<head->value<<' ';
if(head->addr)
alternateprintlistrec(head->addr->addr);
return;
}
node* removeduplicates(node * head)
{
int a[100000];
while(head)
{
a[head->value]++;
head=head->addr;
}

IT II

Rahul Khanna
760/IT/14

int k=0;
while(head)
{
if(a[head->value])
{
if(k==0)
{
head=head->addr;
}
else
{
node *temp=findk(head,k-1);
temp->addr=temp->addr->addr;
}
}
k++;
head=head->addr;
}
return head;
}
int main()
{
node *head1=createlist();
cout<<endl;
head1=removeduplicates(head1);
cout<<endl;
alternateprintlistrec(head1);
cout<<endl;
cout<<"value at middle element is = "<<middle(head1)->value<<endl;
printlistrec(head1,head1,0);
cout<<endl;
}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q12. Doubly Linked List Operations:

i)Program to implement a doubly linked list & provide insertion,deletion & display operations
ii) Program to implement circular doubly linked list.

i)
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node *next;
struct node *prev;
}*start;
class double_llist
{
public:
void create_list(int value);
void add_begin(int value);
void add_after(int value, int position);
void delete_element(int value);
void search_element(int value);
void display_dlist();
double_llist()
{
start = NULL;
}};
int main()
{
int choice, element, position;
double_llist dl;
while (1)
{
cout<<endl<<"----------------------------"<<endl;
cout<<endl<<"Operations on Doubly linked list"<<endl;
cout<<endl<<"----------------------------"<<endl;
cout<<"1.Create Node"<<endl;
cout<<"2.Add at begining"<<endl;
cout<<"3.Add after position"<<endl;
cout<<"4.Delete"<<endl;
cout<<"5.Display"<<endl;
cout<<"6.Count"<<endl;
cout<<"7.Reverse"<<endl;
cout<<"8.Quit"<<endl;

IT II

Rahul Khanna
760/IT/14

cout<<"Enter your choice : ";


cin>>choice;
switch ( choice )
{
case 1:
cout<<"Enter the element: ";
cin>>element;
dl.create_list(element);
cout<<endl;
break;
case 2:
cout<<"Enter the element: ";
cin>>element;
dl.add_begin(element);
cout<<endl;
break;
case 3:
cout<<"Enter the element: ";
cin>>element;
cout<<"Insert Element after postion: ";
cin>>position;
dl.add_after(element, position);
cout<<endl;
break;
case 4:
if (start == NULL)
{
cout<<"List empty,nothing to delete"<<endl;
break;
}
cout<<"Enter the element for deletion: ";
cin>>element;
dl.delete_element(element);
cout<<endl;
break;
case 5:
dl.display_dlist();
cout<<endl;
break;
case 8:
exit(1);
default:
cout<<"Wrong choice"<<endl;
}}
return 0;
}
void double_llist::create_list(int value)
{
struct node *s, *temp;
temp = new(struct node);
temp->info = value;
temp->next = NULL;

IT II

Rahul Khanna
760/IT/14

if (start == NULL)
{
temp->prev = NULL;
start = temp;
} else
{
s = start;
while (s->next != NULL)
s = s->next;
s->next = temp;
temp->prev = s;
}}
void double_llist::add_begin(int value)
{
if (start == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *temp;
temp = new(struct node);
temp->prev = NULL;
temp->info = value;
temp->next = start;
start->prev = temp;
start = temp;
cout<<"Element Inserted"<<endl;
}
void double_llist::add_after(int value, int pos)
{
if (start == NULL)
{
cout<<"First Create the list."<<endl;
return;
} struct node *tmp, *q;
int i;
q = start;
for (i = 0;i < pos - 1;i++)
{
q = q->next;
if (q == NULL)
{
cout<<"There are less than ";
cout<<pos<<" elements."<<endl;
return;
}
}
tmp = new(struct node);
tmp->info = value;
if (q->next == NULL)
{
q->next = tmp;

IT II

Rahul Khanna
760/IT/14

tmp->next = NULL;
tmp->prev = q;
}
else
{
tmp->next = q->next;
tmp->next->prev = tmp;
q->next = tmp;
tmp->prev = q;
}
cout<<"Element Inserted"<<endl;
}
void double_llist::delete_element(int value)
{ struct node *tmp, *q;
if (start->info == value)
{
tmp = start;
start = start->next;
start->prev = NULL;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
} q = start;
while (q->next->next != NULL)
{
if (q->next->info == value)
{
tmp = q->next;
q->next = tmp->next;
tmp->next->prev = q;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
}
q = q->next;
}
if (q->next->info == value)
{
tmp = q->next;
free(tmp);
q->next = NULL;
cout<<"Element Deleted"<<endl;
return;
}
cout<<"Element "<<value<<" not found"<<endl;
}
void double_llist::display_dlist()
{ struct node *q;
if (start == NULL)
{
cout<<"List empty,nothing to display"<<endl;
return;
} q = start;

IT II

Rahul Khanna
760/IT/14

cout<<"The Doubly Link List is :"<<endl;


while (q != NULL)
{
cout<<q->info<<" <-> ";
q = q->next;
}
cout<<"NULL"<<endl;
}

OUTPUT

ii)
#include <stdio.h>
#include <stdlib.h>
struct node
{
int val;
struct node *next;
struct node *prev;
};
typedef struct node n;
n* create_node(int);
void add_node();
void insert_at_first();
void insert_at_end();
void insert_at_position();
void delete_node_position();
void sort_list();
void update();
void search();
void display_from_beg();
void display_in_rev();
n *new, *ptr, *prev;
n *first = NULL, *last = NULL;

IT II

Rahul Khanna
760/IT/14

int number = 0;
void main()
{
int ch;
printf("\nlinked list\n");
printf("1.insert at beginning \n2.insert at end\n3.insert at position\n4.sort linked list\n5.delete node at
position\n6.updatenodevalue\n7.search element\n8.displaylist from beginning\n9.display list from
end\n10.exit ");
while (1)
{
printf("\n enter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1 :
insert_at_first();
break;
case 2 :
insert_at_end();
break;
case 3 :
insert_at_position();
break;
case 4 :
sort_list();
break;
case 5 :
delete_node_position();
break;
case 6 :
update();
break;
case 7 :
search();
break;
case 8 :
display_from_beg();
break;
case 9 :
display_in_rev();
break;
case 10 :
exit(0);
case 11 :
add_node();
break;
default:
printf("\ninvalid choice");
}
}
}
n* create_node(int info)

IT II

Rahul Khanna
760/IT/14

{
number++;
new = (n *)malloc(sizeof(n));
new->val = info;
new->next = NULL;
new->prev = NULL;
return new;
}
void add_node()
{
int info;
printf("\nenter the value you would like to add:");
scanf("%d", &info);
new = create_node(info);
if (first == last && first == NULL)
{
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}
else
{
last->next = new;
new->prev = last;
last = new;
last->next = first;
first->prev = last;
}
}
void insert_at_first()
{
int info;
printf("\nenter the value to be inserted at first:");
scanf("%d",&info);
new = create_node(info);
if (first == last && first == NULL)
{
printf("\ninitially it is empty linked list later insertion is done");
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}
else
{
new->next = first;
first->prev = new;
first = new;
first->prev = last;
last->next = first;

IT II

Rahul Khanna
760/IT/14

printf("\n the value is inserted at begining");


}
}
void insert_at_end()
{
int info;
printf("\nenter the value that has to be inserted at last:");
scanf("%d", &info);
new = create_node(info);
if (first == last && first == NULL)
{
printf("\ninitially the list is empty and now new node is inserted but at first");
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}
else
{
last->next = new;
new->prev = last;
last = new;
first->prev = last;
last->next = first;
}
}
void insert_at_position()
{
int info, pos, len = 0, i;
n *prevnode;
printf("\n enter the value that you would like to insert:");
scanf("%d", &info);
printf("\n enter the position where you have to enter:");
scanf("%d", &pos);
new = create_node(info);
if (first == last && first == NULL)
{
if (pos == 1)
{
first = last = new;
first->next = last->next = NULL;
first->prev = last->prev = NULL;
}
else
printf("\n empty linked list you cant insert at that particular position");
}
else
{
if (number < pos)
printf("\n node cant be inserted as position is exceeding the linkedlist length");
else
{

IT II

Rahul Khanna
760/IT/14

for (ptr = first, i = 1;i <= number;i++)


{
prevnode = ptr;
ptr = ptr->next;
if (i == pos-1)
{
prevnode->next = new;
new->prev = prevnode;
new->next = ptr;
ptr->prev = new;
printf("\ninserted at position %d succesfully", pos);
break;
}
}
}
}
}
void sort_list()
{
n *temp;
int tempval, i, j;
if (first == last && first == NULL)
printf("\nlinked list is empty no elements to sort");
else
{
for (ptr = first,i = 0;i < number;ptr = ptr->next,i++)
{
for (temp = ptr->next,j=i;j<number;j++)
{
if (ptr->val > temp->val)
{
tempval = ptr->val;
ptr->val = temp->val;
temp->val = tempval;
}
}
}
for (ptr = first, i = 0;i < number;ptr = ptr->next,i++)
printf("\n%d", ptr->val);
}
}
void delete_node_position()
{
int pos, count = 0, i;
n *temp, *prevnode;
printf("\n enter the position which u wanted to delete:");
scanf("%d", &pos);
if (first == last && first == NULL)
printf("\n empty linked list you cant delete");

IT II

Rahul Khanna
760/IT/14

else
{
if (number < pos)
printf("\n node cant be deleted at position as it is exceeding the linkedlist length");
else
{
for (ptr = first,i = 1;i <= number;i++)
{
prevnode = ptr;
ptr = ptr->next;
if (pos == 1)
{
number--;
last->next = prevnode->next;
ptr->prev = prevnode->prev;
first = ptr;
printf("%d is deleted", prevnode->val);
free(prevnode);
break;
}
else if (i == pos - 1)
{
number--;
prevnode->next = ptr->next;
ptr->next->prev = prevnode;
printf("%d is deleted", ptr->val);
free(ptr);
break;
}}}}}
void update()
{
int oldval, newval, i, f = 0;
printf("\n enter the value old value:");
scanf("%d", &oldval);
printf("\n enter the value new value:");
scanf("%d", &newval);
if (first == last && first == NULL)
printf("\n list is empty no elemnts for updation");
else
{
for (ptr = first, i = 0;i < number;ptr = ptr->next,i++)
{
if (ptr->val == oldval)
{
ptr->val = newval;
printf("value is updated to %d", ptr->val);
f = 1;
}
}
if (f == 0)

IT II

Rahul Khanna
760/IT/14

printf("\n no such old value to be get updated");


}}
void search()
{
int count = 0, key, i, f = 0;
printf("\nenter the value to be searched:");
scanf("%d", &key);
if (first == last && first == NULL)
printf("\nlist is empty no elemnets in list to search");
else
{
for (ptr = first,i = 0;i < number;i++,ptr = ptr->next)
{
count++;
if (ptr->val == key)
{
printf("\n the value is found at position at %d", count);
f = 1;
}
}
if (f == 0)
printf("\n the value is not found in linkedlist");
}
}
void display_from_beg()
{
int i;
if (first == last && first == NULL)
printf("\nlist is empty no elemnts to print");
else
{
printf("\n%d number of nodes are there", number);
for (ptr = first, i = 0;i < number;i++,ptr = ptr->next)
printf("\n %d", ptr->val);
}
}
void display_in_rev()
{
int i;
if (first == last && first == NULL)
printf("\nlist is empty there are no elments");
else
{
for (ptr = last, i = 0;i < number;i++,ptr = ptr->prev)
{
printf("\n%d", ptr->val);
}
}}

OUTPUT
IT II

Rahul Khanna
760/IT/14

IT II

Rahul Khanna
760/IT/14

Q13. Detect a cycle in a linked list.

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void push(struct node** head_ref, int new_data)
{
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int detectloop(struct node *list)
{
struct node *slow_p = list, *fast_p = list;
while(slow_p && fast_p &&
fast_p->next )
{
slow_p = slow_p->next;
fast_p = fast_p->next->next;
if (slow_p == fast_p)
{
printf("Found Loop");
return 1;
}}
return 0;
}
int main()
{
struct node* head = NULL;
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 10);
head->next->next->next->next = head;
detectloop(head);
}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q14.Write a Program to implement a binary tree using a linked list

#include <stdio.h>
#include <malloc.h>
struct node {
struct node * left;
char data;
struct node * right;
};
struct node *constructTree( int );
void inorder(struct node *);
char array[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H' };
int leftcount[ ] = { 1, 3, 5, -1, 9, -1, -1, -1, -1, -1 };
int rightcount[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 };
void main() {
struct node *root;
root = constructTree( 0 );
printf("In-order Traversal: \n");
inorder(root);
}
struct node * constructTree( int index ) {
struct node *temp = NULL;
if (index != -1) {
temp = (struct node *)malloc( sizeof ( struct node ) );
temp->left = constructTree( leftcount[index] );
temp->data = array[index];
temp->right = constructTree( rightcount[index] );
}
return temp;
}
void inorder( struct node *root ) {
if (root != NULL) {
inorder(root->left);
printf("%c\t", root->data);
inorder(root->right);
}
}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q15.Write a Program to implement various graph traversal techniques

I) BREADTH FIRST SEARCH


#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
int cost[10][10],i,j,k,n,queue[10],front,rear,v,visit[10],visited[10];
int main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"enter initial vertex";
cin >>v;
cout <<"Visitied vertices\n";
cout << v;
visited[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
queue[rear++]=j;
}
v=queue[front++];
cout<<v << " ";
k++;
visit[v]=0;
visited[v]=1;
}}

OUTPUT

II) DEPTH FIRST SEARCH


#include<iostream>
#include<conio.h>
#include<stdlib.h>

IT II

Rahul Khanna
760/IT/14

using namespace std;


int cost[10][10],i,j,k,n,stack[10],top,v,visit[10],visited[10];
int main()
{
int m;
cout <<"enterno of vertices";
cin >> n;
cout <<"ente no of edges";
cin >> m;
cout <<"\nEDGES \n";
for(k=1;k<=m;k++)
{
cin >>i>>j;
cost[i][j]=1;
}
cout <<"enter initial vertex";
cin >>v;
cout <<"ORDER OF VISITED VERTICES";
cout << v <<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=n;j>=1;j--)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
stack [top]=j;
top++;
}
v= stack [--top];
cout<<v << " ";
k++;
visit[v]=0; visited[v]=1;
}
getch();
}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q15.Write a Program to convert infix to postfix using stack

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define MAX 100
typedef struct stack
{
int data[MAX];
int top;
}stack;
int priority(char);
void init(stack *);
int empty(stack *);
int full(stack *);
char pop(stack *);
void push(stack *,char);
char top(stack *);
int main()
{
stack s;
char x;
int token;
init(&s);
printf("Enter infix expression:");
while((token=getchar())!='n')
{
if(isalnum(token))
printf("%c",token);
else
if(token == '(')
push(&s,'(');
else
{
if(token == ')')
while((x=pop(&s))!='(')
printf("%c",x);
else
{
while(priority(token)<=priority(top(&s)) && !empty(&s))
{
x=pop(&s);
printf("%c",x);
}
push(&s,token);
}} }
while(!empty(&s))
{
x=pop(&s);
printf("%c",x);
}
getch();
}

IT II

Rahul Khanna
760/IT/14

int priority(char x)
{
if(x == '(')
return(0);
if(x == '+' || x == '-')
return(1);
if(x == '*' || x == '/' || x == '%')
return(2);
return(3);
}
void init(stack *s)
{
s->top=-1;
}
int empty(stack *s)
{
if(s->top==-1)
return(1);
else
return(0);
}
int full(stack *s)
{
if(s->top==MAX-1)
return(1);
else
return(0);
}
void push(stack *s,char x)
{
s->top=s->top+1;
s->data[s->top]=x;
}
char pop(stack *s)
{
int x;
x=s->data[s->top];
s->top=s->top-1;
return(x);
}
char top(stack * s)
{
return(s->data[s->top]);
}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q16.Write a Program to evaluate postfix expression using stack


#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 50
int stack[MAX];
char post[MAX];
int top=-1;
void pushstack(int tmp);
void calculator(char c);
int main()
{
int i;
printf("Insert a postfix notation :: ");
gets(post);
for(i=0;i<strlen(post);i++)
{
if(post[i]>='0' && post[i]<='9')
{
pushstack(i);
}
if(post[i]=='+' || post[i]=='-' || post[i]=='*' ||
post[i]=='/' || post[i]=='^')
{
calculator(post[i]);
}
}
printf("\n\nResult :: %d",stack[top]);
getch();
}
void pushstack(int tmp)
{
top++;
stack[top]=(int)(post[tmp]-48);
}
void calculator(char c)
{
int a,b,ans;
a=stack[top];
stack[top]='\0';
top--;
b=stack[top];
stack[top]='\0';
top--;
switch(c)
{
case '+':
ans=b+a;
break;
case '-':
ans=b-a;
break;
case '*':
ans=b*a;

IT II

Rahul Khanna
760/IT/14

break;
case '/':
ans=b/a;
break;
case '^':
ans=b^a;
break;
default:
ans=0;
}
top++;
stack[top]=ans;
}

OUTPUT

IT II

Rahul Khanna
760/IT/14

Q17.Write a Program to transverse binary trees in various manners

# include <stdio.h>
# include <conio.h>
# include <stdlib.h>
typedef struct BST {
int data;
struct BST *lchild, *rchild;
} node;
void insert(node *, node *);
void inorder(node *);
void preorder(node *);
void postorder(node *);
node *search(node *, int, node **);
int main() {
int choice;
char ans = 'N';
int key;
node *new_node, *root, *tmp, *parent;
node *get_node();
root = NULL;
printf("\nProgram For Binary Search Tree ");
do {
printf("\n1.Create");
printf("\n2.Search");
printf("\n3.Recursive Traversals");
printf("\n4.Exit");
printf("\nEnter your choice :");
scanf("%d", &choice);
switch (choice) {
case 1:
do {
new_node = get_node();
printf("\nEnter The Element ");
scanf("%d", &new_node->data);
if (root == NULL) /* Tree is not Created */
root = new_node;
else
insert(root, new_node);
printf("\nWant To enter More Elements?(y/n)");
ans = getch();
} while (ans == 'y');
break;
case 2:
printf("\nEnter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key, &parent);
printf("\nParent of node %d is %d", tmp->data, parent->data);
break;
case 3:
if (root == NULL)

IT II

Rahul Khanna
760/IT/14

printf("Tree Is Not Created");


else {
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
}
} while (choice != 4);
}
node *get_node() {
node *temp;
temp = (node *) malloc(sizeof(node));
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
void insert(node *root, node *new_node) {
if (new_node->data < root->data) {
if (root->lchild == NULL)
root->lchild = new_node;
else
insert(root->lchild, new_node);
}
if (new_node->data > root->data) {
if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
}
}
node *search(node *root, int key, node **parent) {
node *temp;
temp = root;
while (temp != NULL) {
if (temp->data == key) {
printf("\nThe %d Element is Present", temp->data);
return temp;
}
*parent = temp;
if (temp->data > key)
temp = temp->lchild;
else
temp = temp->rchild;
}
return NULL;
}
void inorder(node *temp) {
if (temp != NULL) {
inorder(temp->lchild);
printf("%d", temp->data);
inorder(temp->rchild);

IT II

Rahul Khanna
760/IT/14

}
}
void preorder(node *temp) {
if (temp != NULL) {
printf("%d", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
}
}
void postorder(node *temp) {
if (temp != NULL) {
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d", temp->data);
}
}

OUTPUT

IT II

Rahul Khanna
760/IT/14

You might also like