The document is a lab manual for a Data Structures course. It contains 14 programming assignments to be completed in C and C++ involving various data structures like stacks, queues, linked lists, trees, and graphs. The assignments include problems like polynomial addition using circular lists, converting infix to postfix expressions, evaluating postfix expressions with stacks, and operations on binary trees, linked lists, queues and other abstract data types. Students will pick one question randomly to complete for their exam.
The document is a lab manual for a Data Structures course. It contains 14 programming assignments to be completed in C and C++ involving various data structures like stacks, queues, linked lists, trees, and graphs. The assignments include problems like polynomial addition using circular lists, converting infix to postfix expressions, evaluating postfix expressions with stacks, and operations on binary trees, linked lists, queues and other abstract data types. Students will pick one question randomly to complete for their exam.
(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; }
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.
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
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); }
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
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;
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)
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 <<.
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;
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 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:";
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.
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)
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
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
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.
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.