Anda di halaman 1dari 0

Shri Pillappa College of Engineering

(Approved by AICTE New Delhi, Affiliated to VTU Govt. of Karnataka)


#79, Kondasettihalli Road, Hesaraghatta Hobli, Bangalore North Taluk, Bangalore-89

Department of Computer Science & Engineering



III Semester
Data Structures Lab Manual
(10CSL37)




Prepared By
Jagadeesh A Y
Asst. Professor
Dept. of CSE
SPCE
Jagadeesh A Y, Dept. of CSE SPCE 2

DATA STRUCTURES WITH C/C++ LABORATORY
(Common to CSE & ISE)
Subject Code: 10CSL37 I.A. Marks : 25
Hours / Week : 03 Exam Hours: 03
Total Hours : 42 Exam Marks: 50

1. Using circular representation for a polynomial, design, develop, and execute a program in
C to accept two polynomials, add them, and then print the resulting polynomial.

2. Design, develop, and execute a program in C to convert a given valid parenthesized infix
arithmetic expression to postfix expression and then to print both the expressions. The
expression consists of single character operands and the binary operators + (plus), -
(minus), * (multiply) and / (divide).

3. Design, develop, and execute a program in C to evaluate a valid postfix expression using
stack. Assume that the postfix expression is read as a single line consisting of non-
negative single digit operands and binary arithmetic operators. The arithmetic operators
are + (add), - (subtract), * (multiply) and / (divide).

4. Design, develop, and execute a program in C to simulate the working of a queue of
integers using an array. Provide the following operations:
a. Insert b. Delete c. Display

5. Design, develop, and execute a program in C++ based on the following requirements:
An EMPLOYEE class is to contain the following data members and member functions:
Data members: Employee_Number (an integer), Employee_Name (a string of characters),
Basic_Salary (an integer) , All_Allowances (an integer), IT (an integer), Net_Salary (an
integer).
Member functions: to read the data of an employee, to calculate Net_Salary and to print the
values of all the data members.
(All_Allowances = 123% of Basic; Income Tax (IT) = 30% of the gross salary (=
basic_Salary _ All_Allowance); Net_Salary = Basic_Salary + All_Allowances IT)
Jagadeesh A Y, Dept. of CSE SPCE 3

6. Design, develop, and execute a program in C++ to create a class called STRING and
implement the following operations. Display the results after every operation by overloading
the operator <<.
i. STRING s1 = VTU
ii. STRING s2 = BELGAUM
iii. STIRNG s3 = s1 + s2; (Use copy constructor)

7. Design, develop, and execute a program in C++ to create a class called STACK using an
array of integers and to implement the following operations by overloading the operators +
and - :
i. s1=s1 + element; where s1 is an object of the class STACK and element is an integer to be
pushed on to top of the stack.
ii. s1=s1- ; where s1 is an object of the class STACK and operator pops off the top element.
Handle the STACK Empty and STACK Full conditions. Also display the contents of the
stack after each operation, by overloading the operator <<.

8. Design, develop, and execute a program in C++ to create a class called LIST (linked list)
with member functions to insert an element at the front of the list as well as to delete an
element from the front of the list. Demonstrate all the functions after creating a list object.

9. Design, develop, and execute a program in C to read a sparse matrix of integer values and
to search the sparse matrix for an element specified by the user. Print the result of the search
appropriately. Use the triple <row, column, value> to represent an element in the sparse
matrix.

10. Design, develop, and execute a program in C to create a max heap of integers by
accepting one element at a time and by inserting it immediately in to the heap. Use the array
representation for the heap. Display the array at the end of insertion phase.

11. Design, develop, and execute a program in C to implement a doubly linked list where
each node consists of integers. The program should support the following operations:
i. Create a doubly linked list by adding each node at the front.
ii. Insert a new node to the left of the node whose key value is read as an input.
iii. Delete the node of a given data if it is found, otherwise display appropriate message.
Jagadeesh A Y, Dept. of CSE SPCE 4

iv. Display the contents of the list.
(Note: Only either (a,b and d) or (a, c and d) may be asked in the examination)

12. Design, develop, and execute a program in C++ to create a class called DATE with
methods to accept two valid dates in the form dd/mm/yy and to implement the following
operations by overloading the operators + and -. After every operation the results are to be
displayed by overloading the operator <<.
i. no_of_days = d1 d2; where d1 and d2 are DATE objects, d1 >=d2 and no_of_days is an
integer.
ii. d2 = d1 + no_of_days; where d1 is a DATE object and no_of_days is an integer.

13. Design, develop, and execute a program in C++ to create a class called OCTAL, which
has the characteristics of an octal number.
Implement the following operations by writing an appropriate constructor and an overloaded
operator +.
i. OCTAL h = x ; where x is an integer
ii. int y = h + k ; where h is an OCTAL object and k is an integer.
Display the OCTAL result by overloading the operator <<. Also display the values of h and
y.

14. Design, develop, and execute a program in C++ to create a class called BIN_TREE that
represents a Binary Tree, with member functions to perform inorder, preorder and postorder
traversals. Create a BIN_TREE object and demonstrate the traversals.

Note: In the examination each student picks one question from a lot of all the 14
questions.

Jagadeesh A Y, Dept. of CSE SPCE 5

Program 1
Using circular representation for a polynomial, design,
develop, and execute a program in C to accept two polynomials,
add them, and then print the resulting polynomial.

#include<stdio.h>
#include<stdlib.h>

struct node
{
int coeff;
int exponent;
struct node *link;
};
typedef struct node * NODE;

NODE attach(NODE head, int coeff, int exp)
{
NODE temp, pres;
temp = (struct node *)malloc(sizeof(struct node));

temp->coeff = coeff;
temp->exponent = exp;

pres = head->link;

while(pres->link != head)
{
pres = pres->link;
}

pres->link = temp;
temp->link = head;

return head;
}

NODE read_polynomial(NODE head)
{
int i = 1;
int coeff;
int exp;

printf("Enter coefficient as -999 to end the polynomial\n");
while(1)
{
printf("Enter %d term\n", i++);
printf("Coefficient = ");
scanf("%d", &coeff);
Jagadeesh A Y, Dept. of CSE SPCE 6


if ( coeff == -999)
break;

printf("\npow x =");
scanf("%d", &exp);
head = attach(head, coeff, exp);
}
return head;
}

NODE polynomial_add(NODE head1, NODE head2, NODE head3)
{
NODE a, b;
int coeff;

a = head1->link;
b = head2->link;
while( a != head1 && b != head2)
{
if( a->exponent == b->exponent)
{
coeff = a->coeff + b->coeff;
if(coeff != 0)
{
head3 = attach(head3, coeff, a->exponent);
a = a->link;
b = b->link;
}
}
else if( a->exponent > b->exponent)
{
head3 = attach(head3, a->coeff, a->exponent);
a = a->link;
}
else
{
head3 = attach(head3, b->coeff, b->exponent);
b = b->link;
}
}
while( a != head1 )
{
head3 = attach(head3, a->coeff, a->exponent);
a = a->link;
}

while( b != head2 )
{
head3 = attach(head3, b->coeff, b->exponent);
b = b->link;
Jagadeesh A Y, Dept. of CSE SPCE 7

}

return head3;
}

void display(NODE head)
{
NODE temp;
if(head->link == head)
{
printf("Polynomial does not exist\n");
return;
}

temp = head->link;

while(temp != head)
{

printf("+%2dx^%d", temp->coeff, temp->exponent);
temp = temp->link;
}
return;
}

void main()
{
NODE head1, head2, head3;

head1 = (struct node * )malloc(sizeof(struct node));
head2 = (struct node * )malloc(sizeof(struct node));
head3 = (struct node * )malloc(sizeof(struct node));

head1->link = head1;
head2->link = head2;
head3->link = head3;

printf("\nEnter Polynomial 1:");
head1 = read_polynomial(head1);

printf("\nEnter Polynomial 2:");
head2 = read_polynomial(head2);

head3 = polynomial_add(head1, head2, head3);

printf("\nPolynomial 1 is: ");
display(head1);

printf("\nPolynomial 2 is: ");
display(head2);

Jagadeesh A Y, Dept. of CSE SPCE 8

printf("\nPolynomial 3 is: ");
display(head3);

}


Jagadeesh A Y, Dept. of CSE SPCE 9

Program 2

Write a C program to convert & print a valid parenthesized
infix expression to postfix. The expression should consist of
single character operands and binary operators.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int f(char symbol)
{
switch(symbol)
{
case '+' : return 2;
case '-' : return 2;
case '*' : return 4;
case '/' : return 4;
case '^' : return 5;
case '$' : return 5;
case '(' : return 0;
case '#' : return -1;
default : return 8;
}
}

int g(char symbol)
{
switch(symbol)
{
case '+' :
case '-' : return 1;
case '*' :
case '/' : return 3;
case '^' :
case '$' : return 6;
case '(' : return 9;
case ')' : return 0;
default : return 7;
}
}

int infixtoposfix(char infix[],char postfix[])
{
int top, i, j;
char s[30], symbol;
top = -1;
s[++top] = '#';
j=0;
for( i = 0; i<strlen(infix); i++)
{
Jagadeesh A Y, Dept. of CSE SPCE 10

symbol = infix[i];
while(f(s[top]) > g(symbol))
{
postfix[j] = s[top--];
j++;
}

if(f(s[top] != g(symbol)))
s[++top] = symbol;
else
top--;
}

while(s[top]!='#')
{
postfix[j++]=s[top--];
}
postfix[j]='\0';
return 0;
}

void main()
{
char infix[30],postfix[30];
clrscr();
printf("enter valid expression\n");
scanf("%s", infix);
infixtoposfix(infix, postfix);
printf("the infix expression is %s\n", infix);
printf("the postfix expression is %s\n", postfix);
getch();
}


Jagadeesh A Y, Dept. of CSE SPCE 11

Program 3
Design, develop, and execute a program in C to evaluate a
valid postfix expression using stack. Assume that the postfix
expression is read as a single line consisting of non-negative
single digit operands and binary arithmetic operators. The
arithmetic operators are + (add), - (subtract), * (multiply)
and / (divide).

#include<stdio.h>
#include<math.h>

double compute(char symbol, double op1, double op2)
{
switch(symbol)
{
case '+': return (op1+op2);
case '-': return (op1-op2);
case '*': return (op1*op2);
case '/': return (op1/op2);
case '$':
case '^': return pow(op1,op2);
}

}

void main()
{
double s[20];
double res;
double op1;
double op2;
int top, i;
char postfix[20];
char symbol;

printf("Enter the postfix expression\n");
scanf("%s", postfix);

top = -1;

for(i = 0; i< strlen(postfix); i++)
{
symbol = postfix[i];

if( isdigit(symbol) )
s[++top] = symbol - '0';

Jagadeesh A Y, Dept. of CSE SPCE 12

else
{
op2 = s[top--];
op1 = s[top--];

res = compute(symbol, op1, op2);

s[++top] = res;
}
}
res = s[top--];

printf("The result is %f\n", res);
}



Jagadeesh A Y, Dept. of CSE SPCE 13

Program 4
Design, develop, and execute a program in C to simulate the
working of a queue of integers using an array. Provide the
following operations:
a. Insert b. Delete c. Display

#include<stdio.h>
#define Q_SIZE 20

int choice, element, front, rear, q[Q_SIZE];

void q_insert()
{
if(rear == Q_SIZE-1)
{
printf("\tQUEUE OVERFLOW\n");
return;
}
rear = rear+1;
q[rear] = element;
return;
}

int q_delete()
{
if( front>rear)
return -1;

return q[front++];
}

void display()
{
int i;

if( front> rear )
{
printf("\t QUEUE IS EMPTY\n");
return;
}

printf("Contents of the Queue are\n");
for(i = front; i<=rear; i++)
{
printf("%d\n", q[i]);
}
}

Jagadeesh A Y, Dept. of CSE SPCE 14

void main()
{
front = 0;
rear = -1;

for( ; ; )
{
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Enter the element to insert into
Queue\n");
scanf("%d", &element);

q_insert();
break;

case 2: element = q_delete();
if(element == -1)
printf("\tQUEUE IS EMPTY\n");
else
printf("Item deleted = %d\n", element);
break;

case 3: display();
break;

default: exit(0);
}
}
}




Jagadeesh A Y, Dept. of CSE SPCE 15

Program 5
Design, develop, and execute a program in C++ based on the
following requirements:
An EMPLOYEE class is to contain the following data members and
member functions:
Data members: Employee_Number (an integer), Employee_Name (a
string of characters), Basic_Salary (an integer) ,
All_Allowances (an integer), IT (an integer), Net_Salary (an
integer).
Member functions: to read the data of an employee, to
calculate Net_Salary and to print the values of all the data
members.
(All_Allowances = 123% of Basic; Income Tax (IT) = 30% of the
gross salary (= basic_Salary _ All_Allowance); Net_Salary =
Basic_Salary + All_Allowances IT)

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

class EMP
{
char emp_name[20];
int emp_number;
int basic_salary;
int gross_salary;
int all_allowances;
int IT;
int net_salary;

public: void read_data();
void calculate_net_salary();
void display();
};

void EMP :: read_data()
{
cout<<"\nemployee name:\t ";
cin>>emp_name;
cout<<"employee number:\t ";
cin>>emp_number;
cout<<"employee basic salary:\t ";
cin>>basic_salary;
}
Jagadeesh A Y, Dept. of CSE SPCE 16

void EMP :: calculate_net_salary()
{
all_allowances = 1.23 * basic_salary;
gross_salary = basic_salary + all_allowances;
IT = 0.3 * gross_salary;
net_salary = basic_salary + all_allowances - IT;
}

void EMP :: display()
{
cout<<emp_name<<"\t";
cout<<emp_number<<"\t";
cout<<basic_salary<<"\t";
cout<<all_allowances<<"\t";
cout<<gross_salary<<"\t";
cout<<IT<<"\t";
cout<<net_salary<<"\n";
}

void main()
{
int i, n;
EMP emp[20];

clrscr();

cout<<"\nEnter number of employees: ";
cin>>n;

for( i = 0; i<n; i++)
{
cout<<"\nEnter details of employee "<<i<<"\n";
emp[i].read_data();
}

for( i = 0; i<n; i++)
{
emp[i].calculate_net_salary();
}

cout<<"==============================================\n";
cout<<"\nEmployee Name\t Employee Number\t Basic Salary\t
Allowances\t Gross Salary\t Income Tax\t Net Salary\n";
cout<<"==============================================\n";

for( i = 0; i<n; i++)
{
emp[i].display();
}
getch();
}
Jagadeesh A Y, Dept. of CSE SPCE 17

Program 6
Design, develop, and execute a program in C++ to create a
class called STRING and implement the following operations.
Display the results after every operation by overloading the
operator <<.
i. STRING s1 = VTU
ii. STRING s2 = BELGAUM
iii. STIRNG s3 = s1 + s2; (Use copy constructor)

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

class string
{
private: char name[20];
public: string()
{
name[20]='\0';
}

string(char s[])
{
strcpy(name, s);
}

string(string &s)
{
strcpy(name, s.name);
}

friend string operator+(string s1, string s2);
friend ostream &operator <<(ostream &print, string
&s);
};

ostream &operator <<(ostream &print, string &s)
{
print<<s.name<<endl;
return(print);
}

string operator +(string s1, string s2)
{
string temp(s1);
strcat( temp.name, s2.name);
return (temp);
Jagadeesh A Y, Dept. of CSE SPCE 18

}

void main()
{
clrscr();

string s1("VTU");
string s2("BELGAUM");
string s3=s1+s2;
cout<<"\n first string="<<s1;
cout<<"\n second string="<<s2;
cout<<"\n concatenated third string="<<s3;

getch();
}

Jagadeesh A Y, Dept. of CSE SPCE 19

Program 7
Design, develop, and execute a program in C++ to create a
class called STACK using an array of integers and to implement
the following operations by overloading the operators + and -
:

i. s1=s1 + element; where s1 is an object of the class STACK
and element is an integer to be pushed on to top of the
stack.

ii. s1=s1- ; where s1 is an object of the class STACK and
operator pops off the top element.
Handle the STACK Empty and STACK Full conditions. Also display
the contents of the stack after each operation, by overloading
the operator <<.

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

class stack
{
int a[100],top,size;
public:
stack(int n)
{
top=-1;
size=n;
}
friend stack operator+(stack,int);
friend stack operator--(stack);
friend int empty(stack);
friend int overflow(stack);
friend ostream & operator<<(ostream &,stack);

};

stack operator +(stack s1,int num)
{
s1.a[++s1.top]=num;
return s1;
}

stack operator --(stack s1)
{
cout<<"\n The deleted element is:"<<s1.a[s1.top--];
return s1;
}



Jagadeesh A Y, Dept. of CSE SPCE 20

int empty(stack s1)
{
if(s1.top == -1)
return 1;
else
return 0;
}

int overflow(stack s1)
{
if(s1.top == s1.size-1)
return 1;
else
return 0;
}

ostream& operator <<(ostream &print,stack s1)
{
if(empty(s1))
print<<"\n The stack is empty";
else
{
print<<"\n The element in stack are:\n";
for(int i=s1.top;i>=0;i--)
{
print<<s1.a[i]<<"\n";
}
}
return print;
}

void main()
{
int num, ch=1, n;
clrscr();
cout<<"\nEnter the size of stack:";
cin>>n;
stack s1(n);
while(ch)
{
cout<<"\n-------- Menu ---------";
cout<<"\n Enter 1 to push ";
cout<<"\n Enter 2 to pop ";
cout<<"\n Enter 3 to display ";
cout<<"\n Enter 4 to exit ";
cout<<"\n Enter your choice: ";
cin>>ch;




Jagadeesh A Y, Dept. of CSE SPCE 21

switch(ch)
{
case 1: clrscr();
if(overflow(s1))
cout<<"\n Stack overflow";
else
{
cout<<"\n Enter the number to be
inserted ";
cin>>num;
s1=s1+num;
}
cout<<s1;
break;

case 2: clrscr();
if(empty(s1))
cout<<"\n Stack empty";
else
s1 = --s1;
cout<<s1;
break;

case 3: clrscr();
cout<<s1;
break;

case 4: exit(0);

default: cout<<"\nInvalid choice";
}
}
getch();
}


Jagadeesh A Y, Dept. of CSE SPCE 22

Program 8
Design, develop, and execute a program in C++ to create a
class called LIST (linked list) with member functions to
insert an element at the front of the list as well as to
delete an element from the front of the list. Demonstrate all
the functions after creating a list object.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
node *link;
};

class LIST
{
node *first;
public:
list()
{
first = NULL;
}

void insert_node(int);
void delete_node();
void display();
};

void LIST::insert_node(int item)
{
node *temp=new node;
temp->info=item;
temp->link=first;
first=temp;
}

void LIST::delete_node()
{
if(first==NULL)
{
cout<<"the list is empty"<<endl;
return;
}
node *temp = first;
first = first->link;
cout<<"the deleted element is ="<<temp->info<<endl;
}
Jagadeesh A Y, Dept. of CSE SPCE 23

void LIST ::display()
{
if(first == NULL)
{
cout<<"\nlist is empty"<<endl;
return;
}

node *temp=first;
cout<<"\nthe contents of list are:";

while(temp != NULL)
{
cout<<"\n"<<temp->info<<endl;
temp=temp->link;
}
}

void main()
{
LIST object;
int choice;
int item;
clrscr();
for(;;)
{
cout<<"enter the choice" <<endl;
cout<<"1. Insert Node\n";
cout<<"2. Delete Node\n";
cout<<"3. Display\n";
cout<<"4. Exit\n";

cout<<"Enter your choice\n";
cin>>choice;
switch(choice)
{
case 1: cout<<"enter an item:";
cin>>item;
object.insert_node(item);
break;

case 2: object.delete_node();
break;

case 3: object.display();
break;

case 4: exit(0);
}
}
}
Jagadeesh A Y, Dept. of CSE SPCE 24

Program 9
Design, develop, and execute a program in C to read a sparse
matrix of integer values and to search the sparse matrix for
an element specified by the user. Print the result of the
search appropriately. Use the triple <row, column, value> to
represent an element in the sparse matrix.
#include<stdio.h>

struct MATRIX
{
int row;
int col;
int val;
};
struct MATRIX a[100];

read_sparse_matrix()
{
int m;
int n;
int k=1;
int element;
int i, j;
printf("Enter the number of ROWS, COLUMNS of Sparse
Matrix\n");
scanf("%d%d", &m, &n);

a[0].row = m;
a[0].col = n;

printf("Enter elements of Sparse Matrix\n");
for( i = 0; i<m; i++)
{
for(j = 0; j<n; j++)
{
scanf("%d", &element);

if( element == 0)
continue;
else
{
a[k].row = i;
a[k].col = j;
a[k].val = element;
k++;
}
}
}
Jagadeesh A Y, Dept. of CSE SPCE 25

a[0].val = k-1;
return;
}

search()
{
int element, i;

printf("Enter the element to search\n");
scanf("%d", &element);

for( i = 1; i <= a[0].val; i++ )
{
if( element == a[i].val )
{
printf("Search Element is present in the
Matrix\n");
return;
}
}
printf("Search Element is not present in the Sparse
Matrix\n");
return;
}

void main()
{
read_sparse_matrix();
search();
}






Jagadeesh A Y, Dept. of CSE SPCE 26

10. Design, develop, and execute a program in C to create a
max heap of integers by accepting one element at a time and by
inserting it immediately in to the heap. Use the array
representation for the heap. Display the array at the end of
insertion phase.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX_SIZE 10

int insert_heap(int item, int a[], int n)
{
int c, p;
if( n == MAX_SIZE)
{
printf("Heap is full\n");
return n;
}

c = n;
p = c/2;

while( c != 1 && item > a[p])
{
a[c] = a[p];
c = p;
p = c/2;
}

a[c] = item;
return (n+1);
}

void display(int a[], int n)
{
int i;
if( n == 0 )
{
printf("Heap is empty\n");
return;
}
printf("The priority queue contents are\n");
for( i = 1; i<n; i++)
printf("%3d", a[i]);
return;
}

Jagadeesh A Y, Dept. of CSE SPCE 27

void main()
{
int a[MAX_SIZE], n=1, choice, item;
clrscr();

for(;;)
{
printf("\n1. Insert");
printf("\n2. Display");
printf("\n3. Quit");

printf("\nEnter U R choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("\nEnter item to be inserted:");
scanf("%d", &item);
n = insert_heap(item, a, n);
break;
case 2: display(a, n);
break;
default: exit(0);
}
}
}



Jagadeesh A Y, Dept. of CSE SPCE 28

Program 11
Design, develop, and execute a program in C to implement a
doubly linked list where each node consists of integers. The
program should support the following operations:
i. Create a doubly linked list by adding each node at the
front.
ii. Insert a new node to the left of the node whose key value
is read as an input.
iii. Delete the node of a given data if it is found, otherwise
display appropriate message.
iv. Display the contents of the list.
(Note: Only either (a,b and d) or (a, c and d) may be asked in
the examination)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node* NODE;

NODE getnode(void)
{
NODE x;
x =(NODE)malloc(sizeof(struct node));
if(x == NULL)
{
printf("out of memory\n");
exit(0);
}
return x;
}

void free_node(NODE x)
{
free(x);
}



Jagadeesh A Y, Dept. of CSE SPCE 29

NODE insert_front (int item, NODE head)
{
NODE temp, cur;
temp = getnode();
temp->info =item;
cur = head->rlink;
head->rlink = temp;
temp->llink =head;
temp->rlink =cur;
cur->llink = temp;
return head;
}

NODE insert_left(int item,NODE head)
{
NODE temp,cur,prev;
if(head->rlink==head)
{
printf("list is empty\n");
return head;
}
cur=head->rlink;
while(cur!=head&&item!=cur->info)
{
cur=cur->rlink;
}
if(cur==head)
{
printf("key not found\n");
return head;
}
prev=cur->llink;
printf("\n enter the item to be inserted to the left of
%d",item);
temp=getnode();
scanf("%d",&temp->info);
prev->rlink=temp;
temp->llink=prev;
cur->llink=temp;
temp->rlink=cur;
return head;
}

NODE delete_item(int item,NODE head)
{
NODE cur ,prev, next;
if(head->rlink==head)
{
printf("list is empty cannot delete\n");
return head;
}
Jagadeesh A Y, Dept. of CSE SPCE 30

cur=head->rlink;
while(cur!=head&&item!=cur->info)
{
cur=cur->rlink;
}
if(cur==head)
{
printf("\n item not found\n");
return head;
}
prev=cur->llink;
next=cur->rlink;
prev->rlink=next;
next->llink=prev;
free_node(cur);
return head;
}

void display(NODE head)
{
NODE temp;
if(head->rlink == head)
{
printf("List is empty\n");
return;
}
printf("contents of the list is\n");
for(temp = head->rlink; temp != head; temp=temp->rlink)
{
printf("%d\n", temp->info);
}
printf("\n");
}

void main()
{
NODE head;
int choice, item;
head=getnode();
head->rlink=head;
clrscr();
for(;;)
{
printf("1:insert front\n2:insert left\n3:delete
key\n4:display\n5:exit");
printf("\nenter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("enter the item to be
inserted\n");
Jagadeesh A Y, Dept. of CSE SPCE 31

scanf("%d",&item);
head=insert_front(item,head);
break;

case 2: printf("enter the key value of the
node\n");
scanf("%d",&item);
head=insert_left(item,head);
break;

case 3: printf("enter the item to be deleted");
scanf("%d",&item);
head=delete_item(item,head);
break;

case 4: display(head);
break;
default: exit(0);
}
}
getch();
}




Jagadeesh A Y, Dept. of CSE SPCE 32

Program 13
Design, develop, and execute a program in C++ to create a
class called OCTAL, which has the characteristics of an octal
number.
Implement the following operations by writing an appropriate
constructor and an overloaded operator +.
i. OCTAL h = x ; where x is an integer
ii. int y = h + k ; where h is an OCTAL object and k is an
integer.
Display the OCTAL result by overloading the operator <<. Also
display the values of h and y.

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

class octal
{
int octnum;
int conv_octal(int x);
public:
octal(int x)
{
octnum = conv_octal(x);
}
int operator+(int k);
friend ostream & operator<<(ostream &print,octal o);
};

int octal::conv_octal(int x)
{
int i = 0, sum = 0;
while(x != 0)
{
int rem = x%8;
sum = sum + rem * pow(10,i);
i++;
x = x/8;
}
return sum;
}




Jagadeesh A Y, Dept. of CSE SPCE 33

int octal::operator +(int x)
{
x = conv_octal(x);
return (octnum + x);
}

ostream & operator <<(ostream &print, octal oct)
{
print<<oct.octnum;
return print;
}

void main()
{
int x;
clrscr();
cout<<"enter the decimal number to be converted:"<<endl;
cin>>x;
octal h = x;
cout<<"\noctal value is ="<<h;
cout<<"\nenter value of k to be added:";
cin>>x;
int y = h + x;
cout<<"\nvalue of y is ="<<y<<endl;
getch();
}



Jagadeesh A Y, Dept. of CSE SPCE 34

Program 14
Design, develop, and execute a program in C++ to create a
class called BIN_TREE that represents a Binary Tree, with
member functions to perform inorder, preorder and postorder
traversals. Create a BIN_TREE object and demonstrate the
traversals.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int num;
struct node *left, *right;
};

class BIN_TREE
{
node *root;
public:
BIN_TREE()
{
root = NULL;
}
void create();
void inorder(node *);
void preorder(node *);
void postorder(node *);
void display();

};

void BIN_TREE :: create()
{
node *prev, *pres, *temp;
temp = new node;
temp->left = temp->right = NULL;
cout<<"\n\nEnter a number : ";
cin>>temp->num;

if( root == NULL )
{
root=temp;
return;
}

pres = root;
prev = NULL;
Jagadeesh A Y, Dept. of CSE SPCE 35


while( pres != NULL )
{
prev = pres;

if( temp->num > pres->num )
pres = pres->right;
else
pres = pres->left;
}

if( temp->num > prev->num )
prev->right = temp;
else
prev->left = temp;
}

void BIN_TREE :: inorder( node *r )
{
if( r != NULL )
{
inorder( r->left );
cout<<r->num<<" ";
inorder( r->right );
}
}

void BIN_TREE :: preorder( node *r )
{
if( r != NULL )
{
cout<<r->num<<" ";
preorder( r->left );
preorder( r->right );
}
}

void BIN_TREE :: postorder( node *r )
{
if( r != NULL )
{
postorder( r->left );
postorder( r->right );
cout<<r->num<<" ";

}
}




Jagadeesh A Y, Dept. of CSE SPCE 36

void BIN_TREE :: display()
{
if( root == NULL )
{
cout<<"\n\nBinary tree is empty !!";
getch();
}

cout<<"\n\n\nInorder display is..\n\n";
inorder( root );

cout<<"\n\n\npreorder display is..\n\n";
preorder( root );

cout<<"\n\n\npostorder display is..\n\n";
postorder( root );

getch();
}

void main()
{
int ch;
BIN_TREE b1;

for(; ;)
{
clrscr();

cout<<"\n1.Create\n\n";
cout<<"2.Display\n\n\n";
cout<<"3.Exit\n\n";
cout<<"Enter your chocie : ";
cin>>ch;

switch( ch )
{
case 1: b1.create();
break;

case 2: b1.display();
break;

default: exit(0);
}
}
}

Anda mungkin juga menyukai