Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

ALPHA ARTS AND SCIENCE COLLEGE

(AFFILIATED TO THE UNIVERSITY OF MADRAS)


PORUR, CHENNAI-116

RECORD NOTE BOOK

DEPARTMENT OF COMPUTER SCIENCE

DATA STRUCTURES USING C++ LAB


SAE31

NAME :………………………………….

REG. NO. :………………………………….

YEAR : 2020 - 2021

1
ALPHA ARTS AND SCIENCE COLLEGE
(AFFILIATED TO THE UNIVERSITY OF MADRAS)
PORUR, CHENNAI-116

DATA STRUCTURES USING C++ LAB


PRACTICAL RECORD

Name............................................................................................................................

University Reg. No…………………………………………………………………..

Certified bonafide record of work done by………………………………………….

………………………………………………………………………………………

Staff-in-Charge Head of the Department

Submitted for the Data Structures using C++ practical Examination of the

University of Madras held on ………………………….

Examiners

…………………………. ……..…….. …………………..

Date………………. Date…………………….
2
INDEX

Ex.No Date Title of the Program Page.No Sign


1. Array Operations

2. Stack using Array

3. Queue using Array

4. Stack using pointer

5. Queue using Pointer

6. Addition of two Polynomials

7. Conversion of Infix Expression to


Postfix Expression
8. Evaluation of postfix expression

9. Single Linked List

10. Double Linked List

11. Binary Tree Traversal

12. Depth first search and Breath first


search using graph

3
Ex.No:1
ARRAY OPERATIONS
Date :

Aim

To create a C++ program to perform inserting and deleting operations using one Dimensional
Array

Algorithm

Step 1: Create array with some fixed size inside the class
Step 2: create functions for array initialization, insertion, deletion and traverse
Step 3: To read the input values for Array.
Step 4: For insertion get the position and Data.
Step 5: Through the loop statement find out the position where to insert the element.
Step 6: Once the position is reached then insert that element in that position.
Step 7: For deletion get the position value and then using the loop statement replace the
deleting element with the element in the next location and repeat the process until the last
location
Step 8: To traverse, set the loop from the starting location of the array to the end location and
display all the elements in the array.

4
ARRAY OPERATIONS

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Arrayop
{
int n,i,arr[10],pos,data;
public:
void get();
void view();
void insert();
void del();
};
void Arrayop :: get()
{
cout<<"Enter n:";
cin>>n;
cout<<"Enter the element of array:\n";
for(i=0;i<n;i++)
cin>>arr[i];
}
void Arrayop :: insert()
{
cout<<"Enter the position of on array:";
cin>>pos;
pos=pos-1;
cout<<"Enter the insertion element:";
cin>>data;
for(i=n-1;i>=pos;i--)
{
arr[i+1]=arr[i];
}
arr[pos]=data;
n++;
}
void Arrayop :: view()
{
cout<<"The element of array is:\n";
for(i=0;i<n;i++)
cout<<arr[i]<<"\n";
}
void Arrayop :: del()
{
cout<<"Enter the position of the element:";
cin>>pos;
pos=pos-1;
for(i=pos;i<n-1;i++)
{
arr[i]=arr[i+1];
}

5
n--;
}
void main()
{
Arrayop ob;
int ch;
clrscr();
do
{
cout<<"\t\t\tArray operations\n";
cout<<"1.Input\n2.Insertion\n3.Deletion\n4.View\n5.Exit\n";
cout<<"Enter the choice:";
cin>>ch;
switch(ch)
{
case 1:ob.get();break;
case 2:ob.insert();break;
case 3:ob.del();break;
case 4:ob.view();break;
case 5:exit(0);
}
}while(ch<=5);
getch();
}

6
Output

Array operations
1.Input
2.Insertion
3.Deletion
4.View
5.Exit
Enter the choice:1
Enter n:4
Enter the element of array:
10 20 30 40
Array operations
1.Input
2.Insertion
3.Deletion
4.View
5.Exit
Enter the choice:2
Enter the position of on array:2
Enter the insertion element:15
Array operations
1.Input
2.Insertion
3.Deletion
4.View
5.Exit
Enter the choice:4
The element of array is:
10
15
20
30
40
Array operations
1.Input
2.Insertion
3.Deletion
4.View
5.Exit
Enter the choice:5

7
Result

8
Ex.No:2
STACK USING ARRAY
Date :

Aim

To create a C++ program to implement stack operations using one dimensional array

Algorithm

Step 1: Create a stack with fixed number of elements using an array.


Step 2: Read the stack operation.
Step 3: If operation is push then
i. If stack status is overflow (top=MAX) we can’t push the element in to stack.
ii. Otherwise increment the top of the stack (top = top +1).
iii. Then push the element into the stack.
Step 4: If operation is pop then
i. If stack status is empty (top = -1) we can’t pop the element from the stack.
ii. Otherwise we can pop the element from the top of the stack.
iii. Decrement the top (top = top -1)
Step 5: If operation is traverse or view then
i. If stack status is empty (top =-1) we can’t view elements in the stack.
ii. Otherwise set the loop from top to 0th location
iii. Display all the elements in the stack

9
STACK USING ARRAY

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class stack
{
int s[5],top;
public:
stack()
{
top=-1;
}
void push();
void pop();
void view();
};
void stack::push()
{
if(top==5)
cout<<"The stack is full\n";
else
{
cout<<"Enter the element:";
cin>>s[++top];
}
}
void stack ::pop()
{
if(top==-1)
cout<<"Stack is empty\n";
else
cout<<"Deletion Element is:"<<s[top--]<<"\n";
}
void stack :: view()
{
if(top==-1)
cout<<"Stack is empty\n";
else
{
cout<<"Stack elements are:\n";
for(int i=top;i>=0;i--)
{
cout<<s[i]<<"\n";
}
}
}
void main()
{
stack ob;
int ch;

10
clrscr();
cout<<"\t\t\tStack Array Operations \n";
cout<<"1.Push 2.Pop 3.View 4.Exit\n";
do
{
cout<<"Enter the choice:";
cin>>ch;
switch(ch)
{
case 1:ob.push();break;
case 2:ob.pop();break;
case 3:ob.view();break;
case 4:exit(0);
}
}while(ch<=4);
getch();
}

11
Output

Stack Array Operations


1.Push 2.Pop 3.View 4.Exit
Enter the choice:1
Enter the element:10
Enter the choice:1
Enter the element:20
Enter the choice:1
Enter the element:30
Enter the choice:3
Stack elements are:
30
20
10
Enter the choice:2
Deletion Element is:30
Enter the choice:

12
Result

13
Ex.No:3
QUEUE USING ARRAY
Date :

Aim

To create a C++ program to implement Queue Operations using one dimensional Array

Algorithm

Step 1: Create a Queue with fixed number of elements using an array.


Step 2: Initialize front and rear with -1 to indicate the empty.
Step 3: Read the Queue operation.
Step 4: If operation is insert then
i. If queue status is overflow (rear = MAX) we can’t insert the element into the queue.
ii. Otherwise check if it’s the first element to be inserted in the queue
iii. If so, then increment both the rear and front by 1 and insert the element.
iv. Otherwise, increment the rear and then insert the element into the queue.

Step 5: If operation is delete then


i. If queue status is empty (front = -1) we can’t delete the element from queue.
ii. Otherwise we can delete the element by incrementing the front (front = front +1).
iii. If there is only one element in the queue then after deletion set rear = front = -1
Step 6: If operation is traverse or view then
i. If queue status is empty (front = -1) we can’t view elements from queue.
ii. Otherwise set the loop from front to rear location
iii. Display all the elements in the queue

14
QUEUE USING ARRAY

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class queue
{
private:
int q[5],rear,front;
public:
queue()
{
rear=front= -1;
}
void enq();
void deq();
void view();
};
void queue :: enq()
{
if(front ==0 && rear==4)
cout<<"Queue is Full\n";
else
{
if(front ==-1)
{
front=0;
}
cout<<"Enter the Data:";
cin>>q[++rear];
}
}
void queue :: deq()
{
if(front==-1)
cout<<"Queue if Empty\n";
else
{
if(rear==front)
{
rear=front=-1;
}
else
{
int x=q[front];
cout<<"dequeue element="<<x<<endl;
front++;
}
}
}
void queue :: view()

15
{
if(front==-1)
cout<<"Queue if Empty\n";
else
{
for(int i=front;i<=rear;i++)
cout<<q[i]<<"\n";
}
}
void main()
{
queue q;
clrscr();
int ch;
cout<<"\t\t\tQueue Using Array\n";
cout<<"1.Insertion 2.Deletion 3.View 4.Exit\n";
do
{
cout<<"Enter the choice:";
cin>>ch;
switch(ch)
{
case 1:q.enq();break;
case 2:q.deq();break;
case 3:q.view();break;
case 4:exit(0);
}
}while(ch<=4);
getch();
}

16
Output

Queue Using Array


1.Insertion 2.Deletion 3.View 4.Exit
Enter the choice:1
Enter the Data:10
Enter the choice:1
Enter the Data:20
Enter the choice:1
Enter the Data:30
Enter the choice:3
10
20
30
Enter the choice:2
dequeue element=10
Enter the choice:3
20
30
Enter the choice:

17
Result

18
Ex.No:4
STACK USING POINTER
Date :

Aim

To create a C++ program to implement stack operations using pointer.

Algorithm

Step 1: Create node using structure with data part and address part
Step 2: Read the stack operation

Step 3: If the queue operation is push then do the following steps


1. Create a new empty node temp.
2. Allocate memory for the node temp.
3. Read the queue’s element in temp node’s data part.
4. Assign temp node’s link part as null.
5. Assign top = temp.

Step 4: If the queue operation is pop then do the following steps.


1. If top equal to NULL then display stack is empty.
2. Otherwise, Assign temp=top.
3. Assign top as temp of link (i.e. top=temp->link).
4. Delete the temp node (so that the element in the top will be deleted)

Step 5: If the operation is view then do the following steps


1. Assign temp as top.
2. Repeat until temp becomes Null.
1. Display temp node’s data.
2. Assign temp as temp of link (temp = temp -> link)
Step 6: go to step 2.

19
STACK USING POINTER

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int no;
node *next;
}*top,*temp;
class stack
{
public:
stack() //constructor
{
top=NULL;
}
void push();
void pop();
void view();
};
void stack :: push()
{
temp=new node; //new-dynamic memory allocation operator
cout<<"Enter the Number:";
cin>>temp->no;
temp->next=NULL;//Null value is stored in first time
if(top==NULL)
{
top=temp;//Assign new node as a top
}
else
{
temp->next=top;
top=temp;
}
}
void stack :: pop()
{
if(top==NULL)
cout<<"Stack is Empty\n";
else
{
temp=top;
cout<<"Deleted Element is:"<<temp->no<<"\n";
top=temp->next;
delete temp;
}
}
void stack :: view()
{

20
if(top==NULL)
cout<<"stack is Empty\n";
else
{
for(temp=top;temp!=NULL;temp=temp->next)
{
cout<<temp->no<<"\n";
}
}
}
void main()
{
int ch;
clrscr();
stack m;
cout<<"\t\t\tStack Operations\n";
cout<<"1.Push 2.Pop 3.View 4.Exit\n";
do
{
cout<<"Enter the choice:";
cin>>ch;
switch(ch)
{
case 1:m.push();break;
case 2:m.pop();break;
case 3:m.view();break;
case 4:exit(0);
}
}while(ch<=4);
getch();
}

21
Output

Stack Operations
1.Push 2.Pop 3.View 4.Exit
Enter the choice:1
Enter the Number:10
Enter the choice:1
Enter the Number:20
Enter the choice:1
Enter the Number:30
Enter the choice:3
30
20
10
Enter the choice:2
Deleted Element is:30
Enter the choice:4

22
Result

23
Ex.No:5
QUEUE USING POINTER
Date :

Aim

To create a C++ program to implement queue operations using pointer.

Algorithm

Step 1: Create node using structure with data part and address part

Step 2: Read the queue operation

Step 3: If the queue operation is insert then do the following steps


 Create a new empty node temp.
 Allocate memory for the node temp.
 Read the queue’s element in temp node’s data part.
 Assign temp node’s link part as null.
 If front is NULL then Assign front = temp and rear = temp (first node in the queue)
 Otherwise assign rear -> next = temp and rear =temp (for subsequent node)
Step 4: If the queue operation delete then do the following steps.
 If front equal to NULL then display queue is empty.
 Otherwise, Assign temp= front.
 Assign front as temp of link (i.e. front = temp->link).
 Delete the temp node (so that the element in the front will be deleted)
Step 5: If the operation is view then do the following steps
1. If front equal to NULL then display queue is empty.
2. Otherwise set the loop temp = front.
3. Repeat until temp becomes Null.
1. Display temp node’s data.
2. Assign temp as temp of link (temp = temp -> link)
Step 6: go to step 2.

24
QUEUE USING POINTER

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int no;
node *next;
}*front,*rear,*temp;
class queue
{
public:
queue()
{
front=NULL;
rear=NULL;
}
void insert();
void del();
void display();
};
void queue :: insert()
{
temp=new node;
cout<<"Enter the Number:";
cin>>temp->no;
temp->next=NULL;
if(front==NULL)
front=temp;
else
{
rear->next=temp;
}
rear=temp;
}
void queue::del()
{
if(front==NULL)
cout<<"Queue is empty\n";
else
{
temp=front;
front=temp->next;
cout<<"Delete Element IS:"<<temp->no<<"\n";
delete temp;
}
}
void queue::display()
{
if(front==NULL)

25
cout<<"Queue is empty\n";
else
{
for(temp=front;temp!=NULL;temp=temp->next)
cout<<temp->no<<"\n";
}
}
void main()
{
queue q;
clrscr();
int ch;
cout<<"\t\t\tQueue Using Pointer\n";
cout<<"1.Insertion 2.Deletion 3.View 4.Exit\n";
do
{
cout<<"Enter the choice:";
cin>>ch;
switch(ch)
{
case 1: q.insert(); break;
case 2: q.del(); break;
case 3: q.display(); break;
case 4: exit(0);
}
}while(ch<=4);
getch();
}

26
Output

Queue Using Pointer


1.Insertion 2.Deletion 3.View 4.Exit
Enter the choice:1
Enter the Number:10
Enter the choice:1
Enter the Number:20
Enter the choice:1
Enter the Number:30
Enter the choice:3
10
20
30
Enter the choice:2
Delete Element IS:10
Enter the choice:4

27
Result

28
Ex.No:6
ADDITION OF TWO POLNOMIALS USING ARRAY
Date :

Aim

To create a C++ program to add two polynomials.

Algorithm

Step 1: Create polynomial using structure with coefficient part and exponent part
Step 2: Before using read(), enter the number of terms in each polynomial
Step 3: If the operation is read then
 Enter the coefficient and exponent of two polynomials
 Repeat the process until it reaches the maximum number of terms (n)
Step 4: If the operation display then do the following steps.
 Print the coefficient and exponent part of both polynomials
 Add + between each terms if it’s not last before term
 Add new line after the first polynomial
 Repeat the process until it reaches the maximum number of terms (n)
Step 5: If the operation is operator+ then do the following steps
 If p1.exponent > p2.exponent then
P3.coefficent = p1.coefficient
P3.exponent = p1.exponent
 Else if p1.exponent < p2.exponent then
P3.coefficent = p2.coefficient
P3.exponent = p2.exponent
 Else if p1.exponent = p2.exponent then
P3.coeffcient = p1.coefficient + p2.coefficient
P3. Exponent = either p1.exponent or p2.exponent
 Repeat the above process until last term is reached in both polynomials and return P3

29
ADDITION OF TWO POLYNOMIALS USING ARRAY

#include<iostream.h>
#include<conio.h>
struct poly
{
int coeff;
int exp;
};
class polynomial
{
struct poly p[20];
public:
int n;
void read();
void display();
polynomial operator+(polynomial);
};
void polynomial::read()
{
int i;
for(i=0;i<n;i++)
{
cout<<"\n Enter the coefficient & exponents:";
cin>>p[i].coeff>>p[i].exp;
}
}
void polynomial::display()
{
for(int i=0;i<n;i++)
{
cout<<p[i].coeff<<" x^ "<<p[i].exp;
if(i!=n-1)
cout<<" + ";
}
cout<<"\n";
}
polynomial polynomial::operator +(polynomial p2)
{
polynomial p3;
int i=0,j=0,k=0;
while((i<n)&&(j<p2.n))
{
if(p[i].exp>p2.p[j].exp)
{
p3.p[k]=p[i];
i++;
}
else if(p[i].exp<p2.p[j].exp)
{
p3.p[k]=p2.p[j];

30
j++;
}
else
{
p3.p[k].coeff=p[i].coeff+p2.p[j].coeff;
p3.p[k].exp=p[i].exp;
i++;
j++;
}
k++;
}
while(i<n)
{
p3.p[k]=p[i];
i++;
k++;
}
while(j<p2.n)
{
p3.p[k]=p2.p[j];
j++;
k++;
}
p3.n=k;
return p3;
}
void main()
{
polynomial p1,p2,p3;
clrscr();
cout<<"\n Enter the no of terms in 1st polynomial:";
cin>>p1.n;
p1.read();
cout<<"\n Enter the no of terms in 2nd polynomial:";
cin>>p2.n;
p2.read();
cout<<"\n 1st polynomial is:\n";
p1.display();
cout<<"\n 2nd polynomial is:\n";
p2.display();
p3=p1+p2;
cout<<"\n Resultant polynomial is:";
p3.display();
getch();
}

31
Output

Enter the no of terms in 1st polynomial:3


Enter the coefficient & exponents:5 4
Enter the coefficient & exponents:3 3
Enter the coefficient & exponents:6 2
Enter the no of terms in 2nd polynomial:3
Enter the coefficient & exponents:8 4
Enter the coefficient & exponents:2 3
Enter the coefficient & exponents:8 1
1st polynomial is:
5 x^ 4 + 3 x^ 3 + 6 x^ 2
2nd polynomial is:
8 x^ 4 + 2 x^ 3 + 8 x^ 1
Resultant polynomial is:13 x^ 4 + 5 x^ 3 + 6 x^ 2 + 8 x^ 1

32
Result

33
Ex.No:7 CONVERSION OF INFIX EXPRESSION TO POSTFIX
Date : EXPRESSION

Aim

To Create a C++ program that converts a given Infix Expression into Postfix Expression

Algorithm

Step 1: Create a class named Postfix with Character Array Expression of fixed size.

Step 2: If the operation is read then

 Enter the infix expression


 Add “ )” to the given expression using strcat() function

Step 3: If the operation convert then do the following steps.

 Create a stack op for operators with the help of an array


 Initialize top=0
 Add “(“ to the top of the stack op before conversion
 Repeat the process until top>0
1. Assign char a=s[i]; i.e. assigning each character element to a
2. If the given character is an Alphabet then
 Print a (alphabet)
3. Else if a == “(” then
 Push a into the stack (op[++top]=a)
4. Else if a == “)” then
 Until op[top]!=”(” pop all the elements from the stack op[t--]
 Decrement top--;

Step 4: If the operation is prior then do the following steps

 Repeat the process until prior(op[top]) >= prior(a) && op[top]!=”(“


 print the operator at the top of stack (op[top])
 decrement the top of the stack top=top-1
 Push the operator in variable a to the stack top (op[++top]=a)

34
INFIX TO POSTFIX CONVERSION

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
class postfix
{
char s[20];
public:
void get();
void convert();
int prior(char c);
};
void postfix :: get()
{
cout<<"\t\t\tInfix to Postfix Expression Evaluation\n";
cout<<"Enter Infix Expression:";
cin>>s;
strcat(s,")");
}
int postfix :: prior(char c)
{
int pr;
switch(c)
{
case '^':pr=3;break;
case '*':
case '/':
case '%':pr=2;break;
case '+':
case '-':pr=1;break;
}
return pr;
}
void postfix :: convert()
{
char op[20],a;
int t=0,i=0;
op[++t]='(';
cout<<"Postfix:";
while(t>0)
{
a=s[i];
if(isalpha(a))
cout<<a;
else if(a=='(')
op[++t]=a;
else if(a==')')
{

35
while(op[t]!='(')
cout<<op[t--];
t--;
}
else
{
while(prior(op[t])>=prior(a)&&op[t]!='(')
cout<<op[t--];
op[++t]=a;
}
i++;
}
}
void main()
{
postfix p;
clrscr();
p.get();
p.convert();
getch();
}

36
Output

Infix to Postfix Expression Evaluation


Enter Infix Expression:(a+b)*(c+d)/(g+f)
Postfix:ab+cd+*gf+/

37
Result

38
Ex.No:8
EVALUATION OF POSTFIX EXPRESSION
Date :

Aim

To Create a C++ program that evaluates a given Postfix Expression

Algorithm

Step 1: Create a class named Postfix


Step 2: Declare the following variables
 char s[20] for postfix expression and int a[20] for stack to store the operands
 int top, op1 and op2 for the stack
 Initialize the top of the stack top = 0
Step 2: If the operation is read then
 Enter the postfix expression
 Add “ )” to the given expression using strcat() function
Step 3: If the operation evaluate then do the following steps.
 Set a loop for I as 0 to strlen(s)
 If s[i] = alphabet then
 Enter the value of s[i]
 Read the value after increment the top a[++top]
 Else
 Set Op1= a[top--]
 Set Op2= a[top]
 Set a[top]=cal(op2,op1,s[i])
 The cal() function performs operation based on the operator at s[i]
and returns the desired value
 Increment I after each iteration i=i+1
 After the evaluation, print the output value which is at a[1]

39
EVALUATION OF POSTFIX EXPRESSION

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
class postfix
{
char s[30];
int a[30],top,op1,op2;
public:
postfix()
{
top=0;
}
void get();
int cal(int,int,char);
};
void postfix :: get()
{
cout<<"\t\t\t\t\tPostfix Evaluation\n\n";
cout<<"Enter the postfix Expression:";
cin>>s;
for(int i=0;i<strlen(s);i++)
{
if(isalpha(s[i]))
{
cout<<"Enter the Value of "<<s[i]<<":";
cin>>a[++top];
}
else
{
op1=a[top--];
op2=a[top];
a[top]=cal(op2,op1,s[i]);
}
}
cout<<"The Output is:"<<a[1];
}
int postfix :: cal(int a,int b,char c)
{
switch(c)
{
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
case '%':return a%b;
case '^':return (pow(a,b));

40
}
}
void main()
{
postfix p;
clrscr();
p.get();
getch();
}

41
Output

Postfix Evaluation

Enter the postfix Expression:ab*cd+*


Enter the Value of a:2
Enter the Value of b:3
Enter the Value of c:4
Enter the Value of d:2
The Output is:36

42
Result

43
Ex.No:9
SINGLE LINKED LIST
Date :

Aim

To perform creation, insertion, deletion and view in Single Linked List using C++

Algorithm

Step 1: Create node using structure with data part and address part

Step 2: select the operation

Step 3: If the operation is create then do the following steps

 Create a new empty node temp.


 Allocate memory for the node temp.
 Read the element in temp node’s data part.
 Assign temp node’s link part as null.
 If head is NULL then Assign head = temp and tail = temp(first node in the single
linked list)
 Otherwise assign tail -> next = temp and tail =temp (for subsequent node)

Step 4: If the operation insert then do the following steps.

 Assign temp = head


 Read the position value
 Create a new node temp1
 Read the element in temp1 node’s data part
 Assign temp1 node’s link part as NULL
 If the position =1 then
1. Assign temp1 -> link = head and head = temp1

Else

2. Set a loop for I < position -1


1. temp = temp -> link to move to the desired position
2. increment I for each iteration i=i+1
3. after reaching the desired position
1. set temp1->link = temp ->link
2. set temp ->link = temp1

44
Step 5: If the operation delete then do the following steps.

 Assign temp = head


 Read the position value
 If the position =1 then
1. Assign head = head -> link
 Else
1. Set a loop for I < position
1. Set temp1 = temp
2. temp = temp->link
3. increment I for each iteration i=i+1
2. after reaching the desired position
1. set temp1->link = temp ->link
2. delete temp node

Step 6: If the operation is view then do the following steps

 Assign temp as head.


 Repeat until temp becomes NULL.
 Display temp node’s data.
 Assign temp as temp of link (temp = temp -> link)
Step 7: go to step 2.

45
SINGLE LINKED LIST

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class sll
{
struct node
{
int no;
node *next;
}*head,*tail,*temp,*temp1;
public:
sll()
{
head=NULL;
}
void create();
void insert();
void del();
void view();
~sll(){ }
};
void sll :: create()
{
temp= new node;
cout<<"Enter the Number:";
cin>>temp->no;
temp->next=NULL;
if(head==NULL)
{
head=temp;
}
else
{
tail->next=temp;
}
tail=temp;
}
void sll :: view()
{
for(temp=head;temp!=NULL;temp=temp->next)
cout<<temp->no<<"\t";
cout<<"\n";
}
void sll :: insert()
{
temp=head;
int pos,i=1;
cout<<"Enter the Position:";
cin>>pos;

46
temp1=new node;
cout<<"Enter the data:";
cin>>temp1->no;
if(pos==1)
{
temp1->next=head;
head=temp1;
}
else
{
while(i<pos-1)
{
temp=temp->next;
i++;
}
temp1->next=temp->next;
temp->next=temp1;
}
}
void sll :: del()
{
temp=head;
int pos,i=1;
cout<<"Enter the position:";
cin>>pos;
if(pos==1)
head=head->next;
else
{
while(i<pos)
{
temp1=temp;
temp=temp->next;
i++;
}
temp1->next=temp->next;
delete(temp);
}
}
void main()
{
sll m;
clrscr();
int ch1;
cout<<"\t\t\tLinked List Program\n";
cout<<"1.Create 2.Insert 3.Delete 4.view 5.Exit\n";
do{
cout<<"Enter the choice:";
cin>>ch1;
switch(ch1)
{
case 1:m.create();break;
47
case 2:m.insert();break;
case 3:m.del();break;
case 4:m.view();break;
case 5:exit(0);
}
}while(ch1<6);
getch();
}

48
Output

Linked List Program


1.Create 2.Insert 3.Delete 4.view 5.Exit
Enter the choice:1
Enter the Number:10
Enter the choice:1
Enter the Number:20
Enter the choice:1
Enter the Number:30
Enter the choice:4
10 20 30
Enter the choice:2
Enter the Position:2
Enter the data:15
Enter the choice:4
10 15 20 30
Enter the choice:3
Enter the position:3
Enter the choice:4
10 15 30
Enter the choice:

49
Result

50
Ex.No:10
DOUBLE LINKED LIST
Date :

Aim

To perform creation, insertion, deletion and view in Double Linked List using C++

Algorithm

Step 1: Create node using structure with data part, next address part and previous address part

Step 2: select the operation

Step 3: If the operation is create then do the following steps

 Create a new empty node temp.


 Allocate memory for the node temp.
 Read the element in temp node’s data part.
 Assign temp node’s next link part and previous link part as NULL.
 If head is NULL then Assign head = temp and tail = temp(first node in the single
linked list)
 Otherwise assign tail -> next = temp, temp -> prev = tail and tail =temp (for
subsequent node)

Step 4: If the operation insert then do the following steps.

 Assign temp = head


 Read the position value
 Create a new node temp1
 Read the element in temp1 node’s data part
 Assign temp1 node’s next link part and prev link part as NULL
 If the position =1 then
1. Assign temp1 -> link = head and head->prev=temp1
2. Assign head = temp1
 Else
1. Set a loop for I < position -1
1. temp = temp -> link to move to the desired position
2. increment I for each iteration i=i+1
2. after reaching the desired position
1. set temp1->link = temp ->link
2. set temp->next->prev=temp1
3. set temp ->link = temp1
4. set temp1->prev = temp
51
Step 5: If the operation delete then do the following steps.

 Assign temp = head


 Read the position value
 If the position =1 then
1. Assign head = head -> link and head->prev = NULL
 Else
1. Set a loop for I < position
1. temp = temp->link
2. increment I for each iteration i=i+1
2. after reaching the desired position
1. set temp->prev->link = temp ->link
2. set temp->link->prev = temp->prev
3. delete temp node

Step 6: If the operation is fview then do the following steps


 Assign temp as head.
 Repeat until temp becomes NULL.
 Display temp node’s data.
 Assign temp as temp of link (temp = temp -> link)
 if the operation is bview then do the following steps
 assign temp as tail
 repeat until temp becomes NULL
 Display temp node’s data.
 Assign temp as temp of link (temp = temp -> prev)
Step 7: go to step 2.

52
DOUBLE LINKED LIST

#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
struct node
{
int no;
node *next,*prev;
}*temp,*temp1,*head,*tail;
class dll
{
public:
dll()
{
head=tail=NULL;
}
void create();
void del();
void fview();
void bview();
void insert();
};
void dll::create()
{
char ch;
x:temp=new node;
cout<<"enter data:";
cin>>temp->no;
temp->next=temp->prev=NULL;
if(head==NULL)
head=temp;
else
{
tail->next=temp;
temp->prev=tail;
}
tail=temp;
}
void dll::fview()
{
for(temp=head;temp!=NULL;temp=temp->next)
{
cout<<temp->no<<"\t";
}
}
void dll::bview()
{
for(temp=tail;temp!=NULL;temp=temp->prev)
{
cout<<temp->no<<"\t";

53
}
}
void dll::insert()
{
temp=head;
int i=1,pos;
temp1=new node;
cout<<"\nenter data:";
cin>>temp1->no;
temp1->next=temp->prev=NULL;
cout<<"\nenter the pos:";
cin>>pos;
if(pos==1)
{
temp1->next=head;
head->prev=temp1;
head=temp1;
}
else
{
while(i<pos-1)
{
temp=temp->next;
i++;
}
temp1->next=temp->next;
temp->next->prev=temp1;
temp->next=temp1;
temp1->prev=temp;
}
}
void dll::del()
{
int i=1,pos;
temp=head;
cout<<"enter pos:";
cin>>pos;
if(pos==1)
{
head=head->next;
head->prev=NULL;
}
else
{
while(i<pos)
{
temp1=temp;
temp=temp->next;
i++;
}
temp1->next=temp->next;
temp->next->prev=temp->prev;
54
delete(temp);
}
}
void main()
{
int c;
clrscr();
dll s;
cout<<"double link list operations\n";
cout<<"1.create\n2.fview\n3.insert\n4.del\n5.bview\n6.exit\n";
do
{
cout<<"\nenter your choice:";
cin>>c;
switch(c)
{
case 1:s.create();break;
case 2:s.fview();break;
case 3:s.insert();break;
case 4:s.del();break;
case 5:s.bview();break;
case 6:exit(0);
}
}while(c<=6);
getch();
}

55
Output

double link list operations


1.create
2.fview
3.insert
4.del
5.bview
6.exit

enter your choice:1


enter data:10

enter your choice:1


enter data:20

enter your choice:1


enter data:30

enter your choice:2


10 20 30
enter your choice:5
30 20 10
enter your choice:3

enter data:5

enter the pos:1

enter your choice:2


5 10 20 30
enter your choice:4
enter pos:1

enter your choice:2


10 20 30
enter your choice:

56
Result

57
Ex.No:11
BINARY TREE TRAVERSALS
Date :

Aim

To perform Binary Tree Traversals (pre-order, in-order and post-order) using C++ program

Algorithm

Step 1: Create node using structure with data part, left child part and right child part

Step 2: set Root = NULL in the constructor of the class

Step 3: create a root node and enter the Root value

Step 4: select the operation

Step 5: If the operation is create then do the following steps

 Set p->left = p->right = NULL (initially left and right of the root are NULL)
 Enter value (0/1) for creating left child :
 If 1 means it creates a node to the left of the root
 Enter the value for the left child node
 If 0 means it won’t create a node to the left of the root
 Enter value (0/1) for creating right child :
 If 1 means it creates a node to the right of the root
 Enter the value for the right child node
 If 0 means it won’t create a node to the right of the root

Step 6: If preorder is selected then

 If p! = NULL then
 Print p->no
 Call preorder (p->left)
 Call preorder (p->left)

Step 7: If inorder is selected then

 If p! = NULL then
 Call preorder (p->left)
58
 print p->no
 Call preorder (p->right)

Step 8: If postorder is selected then

 If p! = NULL then
 Call preorder (p->left)
 Call preorder (p->right)
 print p->no
 print p->no

Step 9: If exit is selected then

 The program ends by the function exit(0)

59
TREE TRAVERSALS

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int no;
node *left,*right;
}*root;
class tree
{
public:
tree()
{
root=NULL;
}
void create(node *);
void inorder(node *);
void postorder(node *);
void preorder(node *);
~tree(){}
};
void tree :: create(node *p)
{
int t;
p->left=p->right=NULL;
cout<<"Enter 1 to add one to left of"<<p->no<<":";
cin>>t;
if(t==1)
{
p->left=new node;
cout<<"Enter value:";
cin>>p->left->no;
create(p->left);
}
cout<<"Enter 1 to add one node to right of"<<p->no<<":";
cin>>t;
if(t==1)
{
p->right=new node;
cout<<"Enter the value:";
cin>>p->right->no;
create(p->right);
}
}
void tree :: inorder(node *p)
{
if(p!=NULL)
{
inorder(p->left);

60
cout<<p->no<<" ";
inorder(p->right);
}
}
void tree :: preorder(node *p)
{
if(p!=NULL)
{
cout<<p->no<<" ";
preorder(p->left);
preorder(p->right);
}
}
void tree :: postorder(node *p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
cout<<p->no<<" ";
}
}
void main()
{
tree t;
int ch;
clrscr();
root=new node;
cout<<"\t\t\tTREE TRAVERSALS";
cout<<"\n\tEnter the root value:";
cin>>root->no;
t.create(root);
cout<<"\n1.INORDER 2.PREORDER 3.POSTORDER 4.EXIT";
do{
cout<<"\nEnter the Choice:";
cin>>ch;
switch(ch)
{
case 1:t.inorder(root);break;
case 2:t.preorder(root);break;
case 3:t.postorder(root);break;
case 4:exit(0);
}
}while(ch<=4);
getch();
}

61
Output

Enter the root value:3


Enter 1 to add one to left of3:1
Enter value:1
Enter 1 to add one to left of1:1
Enter value:7
Enter 1 to add one to left of7:0
Enter 1 to add one node to right of7:0
Enter 1 to add one node to right of1:1
Enter the value:8
Enter 1 to add one to left of8:0
Enter 1 to add one node to right of8:0
Enter 1 to add one node to right of3:1
Enter the value:2
Enter 1 to add one to left of2:1
Enter value:4
Enter 1 to add one to left of4:0
Enter 1 to add one node to right of4:0
Enter 1 to add one node to right of2:1
Enter the value:5
Enter 1 to add one to left of5:0
Enter 1 to add one node to right of5:0
1.INORDER 2.PREORDER 3.POSTORDER 4.EXIT
Enter the Choice:1
7183425
Enter the Choice:2
3178245
Enter the Choice:3
7814523
Enter the Choice:4

62
Result

63
Ex.No:12 GRAPH TRAVERSALS: BREADTH FIRST SEARCH AND
Date : DEPTH FIRST SEARCH

Aim

To perform Graph traversals (BFS & DFS) using C++ program

Algorithm

Step 1: create a class graph with variables vertex[10], int visited[10], static int adj[10][10]

Step 2: create functions like create(), breadth() and depth() inside the class

Step 3: If the operation is create(), then

 To read input value for the No of Nodes in the graph.


 To read the value for No of Adjacency vertices for each node
 To read the value for Adjacency Vertices of each node

Step 4: Enter your choice to perform the necessary operations

Step 5: If the case is one perform the DFS to search the element in column wise.

Step 6: If the case is two perform the BFS to search the element in depth (row) wise.

Step 7: If the case is three then exit;

64
GRAPH TRAVERSAL – BFS & DFS

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class graph
{
int vertex[10];
int visited[10];
static int adj[10][10];
public:
void create(int);
void depth(int,int);
void breath(int);
};
void graph :: create(int v)
{
int i,j,k,m,n;
for(i=0;i<v;i++)
{
cout<<"Enter the node value";
cin>>vertex[i];
visited[i]=0;
}
for(i=0;i<v;i++)
{
cout<<"Enter the no.adjacent vertices fo"<<vertex[i]<<":";
cin>>n;
for(j=0;j<n;j++)
{
cout<<"Enter the adjacent vertex....";
cin>>m;
for(k=0;k<v;k++)
{
if(vertex[k]==m)
adj[i][k]=1;
}
}
adj[0][0]=1;
}
}
void graph :: depth(int a,int v)
{
int j;
for(j=0;j<v;j++)
if(adj[a][j]==1)
if(visited[j]==0)
{
visited[j]=1;
cout<<vertex[j]<<"--->";
depth(j,v);

65
}
}
void graph :: breath(int v)
{
int i,j;
for(i=0;i<v;i++)
visited[i]=0;
for(i=0;i<v;i++)
for(j=0;j<v;j++)
if(adj[i][j]==1)
if(visited[j]==0)
{
visited[j]=1;
cout<<vertex[j]<<"--->";
}
cout<<"\n";
}
int graph :: adj[10][10];
void main()
{
int n,ch;
graph g;
clrscr();
cout<<"Enter the no of vertices in the graph:";
cin>>n;
g.create(n);
do{
cout<<"\n1.DFS 2.BFS 3.EXIT";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n\nDFS Traversals method\n";
g.depth(0,n);
cout<<"end";
cout<<"\n"<<"\n";
break;
case 2:
cout<<"\n\nBFS Traversals method\n";
g.breath(n);
cout<<"\n"<<"\n";
break;
case 3:
exit(0);
}
}while(ch<=3);
getch();
}

66
Output

Enter the no of vertices in the graph:4


Enter the node value1
Enter the node value2
Enter the node value3
Enter the node value4
Enter the no.adjacent vertices fo1:2
Enter the adjacent vertex....
2
Enter the adjacent vertex....3
Enter the no.adjacent vertices fo2:2
Enter the adjacent vertex....1
Enter the adjacent vertex....4
Enter the no.adjacent vertices fo3:2
Enter the adjacent vertex....1
Enter the adjacent vertex....4
Enter the no.adjacent vertices fo4:2
Enter the adjacent vertex....2
Enter the adjacent vertex....3
1.DFS 2.BFS 3.EXIT
Enter your choice:1
DFS Traversals method
1--->2--->4--->3--->end
1.DFS 2.BFS 3.EXIT
Enter your choice:2
BFS Traversals method
1--->2--->3--->4--->
1.DFS 2.BFS 3.EXIT

67
Result

68

You might also like