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

DataStructure Lab Manual

Uploaded by

Shrinivasa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

DataStructure Lab Manual

Uploaded by

Shrinivasa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

(ISO 9001:2015 Certified)

Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

20CC307: Data Structures Laboratory Evaluation

Evaluation Component Evaluation Criteria Marks

Write up 05
Program1
Execution 10
Write up 10
Program2
Execution 15
Viva-Voce 10
Total 50

1
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

Contents
1) Write a program to store and retrieve array elements using pointers.
2) Write a program to maintain the student information using structure and pointers.
3) Write a C program for the following operations on a stack of integers (use arrays).
i) Push
ii) Pop
iii) Display
4) Write a program to implement the stack using structures.
5) Write a program to convert an expression in infix to an expression in postfix form.
6) Write a program to evaluate a postfix expression
7) Write a program to find factorial of a number using recursion.
8) Write a program to search an element in the given array using binary search (Use recursion).
9) Write a program to implement tower of Hanoi problem using recursion.
10) Write a program to implement queue of integers with the following operations.
i) Insert at the rear end
ii) Delete at the front end
iii) Display the contents of the queue
11) Write a program to implement the queue of integers using structures and implement the above
operations on the queue.
12) Write a program to implement the circular queue.
13) Write a program to implement the priority queue of integers.
14) Write a program to implement singly linked list and implement the following operations on it.
i) Insert a node at the front of the list
ii) Insert a node at any given position
iii) Delete a node from the front of the list
iv) Delete any node from the list.
15) Write a program to implement stack using dynamic memory allocation.
16) Write a program to implement queue using dynamic memory allocation.
17) Write a program to implement circular linked list.
18) Write a program to implement doubly linked list.
19) Write a program to implement circular doubly linked list.
20) Write a program to create a binary tree and implement the inorder, preorder and postorder
travarsals.
21) Write a program to construct a binary search tree and construct postfix expression tree.
22) 22. Write a C program to implement a binary search tree of integers and perform the following
traversal techniques:
i) In-order traversal
ii) Find the maximum element
iii) Search an element
iv) Display "Duplication is not allowed" if same element is inserted again. Display "Key
element not found" if searched element is not present in the tree.Display "Search is
successful" if searched element is present in the tree.
v) Display "Maximum element is 15" if the maximum element is 15 in the tree.
2
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

1. Write a program to store and retrieve array elements using pointers.

#include <stdio.h>
int main()
{
int arr1[25], i,n;
printf("\n\n Pointer : Store and retrieve elements from an array :\n");
printf("------------------------------------------------------------\n");
printf(" Input the number of elements to store in the array :");
scanf("%d",&n);

printf(" Input %d number of elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf(" element - %d : ",i);
scanf("%d",arr1+i);
}
printf(" The elements you entered are : \n");
for(i=0;i<n;i++)
{
printf(" element - %d : %d \n",i,*(arr1+i));
}
return 0;
}

3
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

2. Write a program to maintain the student information using structure and pointers.

#include <stdio.h>
struct student {
char firstName[50];
int roll;
float marks;
} s[5];

int main() {
int i;
printf("Enter information of students:\n");

// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");

// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}

4
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

3. Write a C program for the following operations on a stack of integers (use arrays).
a) Push
b) Pop
c) Display

#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
int top=-1;
void push(int a[],int item)
{
top=top+1;
a[top]=item;
}
int pop(int a[])
{
int item;
item=a[top];
top=top-1;
return item;
}
void display(int a[])
{
int i;
if(top==-1)
printf("The stack is empty\n");
else if(top!=-1)
{
printf("The stack elements are\n");
for(i=top; i>=0;i--)
printf("%d ",a[i]);
printf("\n");
}
}

int main()
{
int s[10],choice,item;
while(1)
{
printf("Enter the choice\n");
printf("1 Push\n2 Pop\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: if(top==SIZE-1)
{

5
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


printf("The stack is full\n");
break;
}
else
{
printf("Enter the element to be pushed\n");
scanf("%d",&item);
push(s,item);
}
break;
case 2: if(top==-1)
{
printf("The stack is empty\n");
break;
}
item=pop(s);
printf("Popped element is %d\n",item);
break;
case 3: display(s);
break;
case 4: exit(0);
}
}
return 0;
}

6
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

4. Write a program to implement the stack using structures.


#include<stdio.h>
#include<stdlio.h>
#define MAX 10 struct stack
{
int top;
int items[MAX];
};
void push(int,struct stack *);
void pop(struct stack *);
void display(struct stack *);
int main()
{
struct stack s;
s.top=-1;
int choice,item;
for(;;)
{
printf("Enter your choice\n");
printf("1 Push\n2 Pop\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the item\n");
scanf("%d",&item); push(item,&s);
break;
case 2: pop(&s);
break;
case 3: display(&s);
break;
case 4: exit(0);
}
}
return 0;
}
void push(int item,struct stack *s)
{
if(s->top==MAX-1)
printf("The stack is full\n");
else
{
(s->top)++;
s->items[s->top]=item;
}
}
void pop(struct stack *s)
{
int item;
7
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


if(s->top==-1)
printf("The stack is empty\n");
else
{
item=s->items[s->top];
(s->top)--;
printf("%d deleted\n",item);
}
}

void display(struct stack *s)


{
int t=s->top;
if(s->top==-1)
printf("The stack is empty\n");
else
{
printf("Elements in the stack are\n");
while(t>-1)
{
printf("%d ",s->items[t--]);
}
printf("\n");
}
}

8
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

5. Write a program to convert an expression in infix to an expression in postfix form.


#include<stdio.h>
#include<ctype.h>

char stack[100];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int priority(char ch)


{
switch(ch)
{
case '(': return 0;
case '+':
case '-': return 1;
case '*':
case '/': return 2;
case '$': return 3;
default: return -1;
}
}

int main()
{
char exp[100],ch,post[100];
//char *e, x;
int i,j=0;
printf("Enter the expression : ");
scanf("%s",exp);
//e = exp;

i=0;
while(exp[i] != '\0')
{
ch=exp[i];
if(isalnum(ch))
9
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


post[j++]=ch;
//printf("%c ",exp[i]);
else if(ch == '(')
push(ch);
else if(ch == ')')
{
while((ch = pop()) != '(')
//printf("%c ", x);
post[j++]=ch;
}
else
{
while(priority(stack[top]) >= priority(ch))
post[j++]=pop();
//printf("%c ",pop());
push(ch);
}
//e++;
i++;
}
while(top != -1)
{
post[j++]=pop();
}
post[j]='\0';
printf("The corresponding postfix expression is %s\n",post);
return 0;
}

10
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

6. Write a program to evaluate a postfix expression


#include<stdio.h>
#include<ctype.h>
int stack[100];
int top = -1;

void push(char x)
{
stack[++top] = x;
}

int pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}

int main()
{
char exp[100],ch;;
//char *e, x;
int i,j=0,op1,op2,result;
printf("Enter the expression : ");
scanf("%s",exp);
//e = exp;

i=0;
while(exp[i] != '\0')
{
ch=exp[i];
if(isdigit(ch))
push(ch-'0');
switch(ch)
{
case '+': op1=pop();
op2=pop();
result=op1+op2;
push(result);
break;

case '-': op1=pop();


op2=pop();
result=op1-op2;
push(result);
break;

11
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


case '*': op1=pop();
op2=pop();
result=op1*op2;
push(result);
break;

case '/': op1=pop();


op2=pop();
result=op1/op2;
push(result);
break;
}
i++;
}
result=pop();
printf("The result of the given postfix expression is %d\n",result);
return 0;
}

12
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

7. Write a program to find factorial of a number using recursion.


#include<stdio.h>
long int factorial(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}

long int factorial(int n)


{
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}

13
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

8. Write a program to search an element in the given array using binary search (Use recursion).
#include<stdio.h>
int binsearch(int a[],int low, int high, int key)
{
int mid;
if(low>high)
return -1;
mid=(low+high)/2;
if(a[mid]==key)
return mid;
else if(key<a[mid])
return binsearch(a,low,mid-1,key);
else
return binsearch(a,mid+1,high,key);
}

int main()
{
int n,a[100],i,key,res;

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


scanf("%d",&n);

printf("Enter the elements in ascending order:\n");


for(i=0;i<n;i++)
scanf("%d",&a[i]);

printf("Enter the element to seach: ");


scanf("%d",&key);

res=binsearch(a,0,n-1,key);

if(res==-1)
printf("Element entered is not present in the list\n");
else
printf("Element entered is present at the position %d\n",res+1);

return 0;
}

14
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

9. Write a program to implement tower of Hanoi problem using recursion.


#include <stdio.h>

// C recursive function to solve tower of hanoi puzzle


void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod)
{
if (n == 1)
{
printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod);
return;
}
towerOfHanoi(n-1, from_rod, aux_rod, to_rod);
printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod);
towerOfHanoi(n-1, aux_rod, to_rod, from_rod);
}

int main()
{
int n = 3; // Number of disks
towerOfHanoi(n, 'A', 'C', 'B'); // A, B and C are names of rods
printf("\n");
return 0;
}

15
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

10. Write a program to implement queue of integers with the following operations.
(i) Insert at the rear end
(ii) Delete at the front end
(iii) Display the contents of the queue
#include <stdio.h>
#include<stdlib.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int main()
{
int choice;
while (1)
{
printf("1.Insert element to queue n");
printf("2.Delete element from queue n");
printf("3.Display all elements of queue n");
printf("4.Quit n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(1);
default: printf("Wrong choice n");
}
}
}

void insert()
{
int item;
if(rear == MAX - 1)
printf("Queue Overflow n");
else
{
if(front== - 1)
16
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


front = 0;
printf("Inset the element in queue : ");
scanf("%d", &item);
rear = rear + 1;
queue_array[rear] = item;
}
}
void delete()
{
if(front == - 1 || front > rear)
{
printf("Queue Underflow n");
return;
}
else
{
printf("Element deleted from queue is : %dn", queue_array[front]);
front = front + 1;
}
}
void display()
{
int i;
if(front == - 1)
printf("Queue is empty n");
else
{
printf("Queue is : n");
for(i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("n");
}
}

17
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

11. Write a program to implement the queue of integers using structures and implement the above
operations on the queue.
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
struct queue
{
int rear,front;
int q[MAX];
};
void INSERT(int,struct queue *);
void DELETE(struct queue *);
void DISPLAY(struct queue *);
int main()
{
int choice, item;
struct queue s;
s.rear=-1;
s.front=0;
for(;;)
{
printf("Enter your choice\n1 Insertion\n2 Deletion\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: if(s.rear==MAX-1)
printf("Queue is full\n");
else
{
printf("Enter the element to be inserted\n");
scanf("%d",&item);
INSERT(item,&s);
}
break;
case 2: DELETE(&s);
break;
case 3: DISPLAY(&s);
break;
case 4: exit(0);
}
}
return 0;
}
void INSERT(int item,struct queue *s)
{

18
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

s->rear=s->rear+1;
s->q[s->rear]=item;
}

void DELETE(struct queue *s)


{
int item;
if(s->front>s->rear)
printf("Queue is empty\n");
else
{
item=s->q[s->front];
printf("%d deleted\n",item);
(s->front)++;
}
}

void DISPLAY(struct queue *s)


{
int i;
if(s->front>s->rear)
printf("Queue is empty\n");
else
{
printf("Elements in the queue are\n");
for(i=s->front;i<=s->rear;i++)
printf("%d ",s->q[i]);
printf("\n");
}
}

19
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

12. Write a program to implement the circular queue.


#include<stdio.h>
#include<stdlib.h>
#define SIZE 3
int items[SIZE];
int front=-1,rear=-1;
int isFull()
{
if((front==rear+1)||(front==0 && rear==SIZE-1))
return 1;
return 0;
}
int isEmpty()
{
if(front==-1)
return 1;
return 0;
}
void insert()
{
int element;
if(isFull())
printf("Queue is full\n");
else
{
printf("Enter the element\n");
scanf("%d",&element);
if(front==-1)
front=0;
rear=(rear+1)%SIZE;
items[rear]=element;
}
}

void delete()
{
int element;
if(isEmpty())
printf("Queue is empty\n");
else
{
element=items[front];
if(front==rear)
{
front=-1; rear=-1;
}
else
front=(front+1)%SIZE;
20
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


printf("Deleted element %d \n",element);
}
}
void display()
{
int i;
if(isEmpty())
printf("Queue is empty\n");
else
{
printf("Elements in the queue\n");
for(i=front; i!=rear; i=(i+1)%SIZE)
printf("%d ",items[i]);
printf("%d \n",items[i]);
}
}

int main()
{
int choice;
for(;;)
{
printf("Enter your choice\n1 Insertion\n2 Deletion\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
}
}
}

21
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

13. Write a program to implement the priority queue of integers.


#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int q[MAX];
int front=0, rear=-1,i,j;
void insert()
{
int ele;
printf("Enter value\n");
scanf("%d",&ele);
if(rear==MAX-1)
printf("Queue overflow\n");
else
{
j=rear;
while((j>=front)&&(ele>q[j]))
{
q[j+1]=q[j];
j--;
}
q[j+1]=ele; rear++;
}
}

void delete()
{
int flag=0;
if(front>rear)
printf("There are no elements to delete\n");
else
{
int ele;
printf("Enter value to delete\n");
scanf("%d",&ele);
for(i=front;i<=rear;i++)
{
if(q[i]==ele)
{
flag=1;
for(j=i;j>front;j--)
q[j]=q[j-1];
front++;
break;
}
}
if(flag==0)
printf("%d not found\n",ele);
22
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


}
}
void display()
{
if(front>rear)
printf("Queue is empty\n");
else
{
for(i=front;i<=rear;i++)
printf("%d ",q[i]);
printf("\n");
}
}
int main()
{
int ch;
printf("1 Insert\n2 Delete\n3 Display\n4 Exit\n");
for(;;)
{
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert();
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
}
}
return 0;
}

23
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

14. Write a program to implement singly linked list and implement the following operations on it.
(i) Insert a node at the front of the list
(ii) Insert a node at any given position
(iii) Delete a node from the front of the list
(iv) Delete any node from the list.

#include<stdio.h>
#include<stdlib.h>
int count=0;
struct node
{
int student_id;
int semester;
char student_name[20];
struct node *link;
};
void insert(struct node **s,int pos)
{
int i;
if(pos>count+1)
{
printf("Invalid position\n");
return;
}
struct node *newnode;
newnode=(struct node *)malloc(sizeof(struct node));
printf("Enter id\n");
scanf("%d",&(newnode->student_id));
printf("Enter name\n");
scanf("%s",newnode->student_name);
printf("Enter semester\n");
scanf("%d",&(newnode->semester));
struct node *temp=*s; if((*s)==NULL)
{
(*s)=newnode; count++;
}
else if(pos==0 || pos==1)
{
for(i=2; i<pos; i++)
{
temp=temp->link;
}
newnode->link=temp->link; temp->link=newnode; count++;
}
else

24
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


{
count++; newnode->link=*s;
*s=newnode;
}
}

void delete_front(struct node **s)


{
if((*s)==NULL)
{
printf("List is empty\n");
return;
}
struct node *temp=*s;
struct node *cur=temp->link;
*s=cur;
int item=temp->student_id;
free(temp);
printf("Deleted ID is %d\n",item);
}

void display(struct node *s)


{
if(s==NULL)
{
printf("List is empty\n"); return;
}
struct node *temp=s;
printf("Contents are\n");
while(temp!=NULL)
{
printf("ID %d\n",temp->student_id);
printf("Name %s\n",temp->student_name);
printf("Semester %d\n",temp->semester);
temp=temp->link;
}
}

int main()
{
int choice,pos;
struct node *s=NULL;
for(;;)
{
printf("Enter your choice\n1 Insert at position\n2 Delete front\n3 Display\n4
Exit\n");
scanf("%d",&choice);
switch(choice)

25
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


{
case 1: printf("Enter the position\n");
scanf("%d",&pos);
insert(&s,pos);
break;
case 2: delete_front(&s);
break;
case 3: display(s);
break;
case 4: exit(0);
}
}
return 0;
}

26
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

15. Write a program to implement stack using dynamic memory allocation.


#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
typedef struct node * NODE;
NODE getnode()
{
NODE X;
X=(NODE)malloc(sizeof(struct node));
return(X);
}
void freenode(NODE X)
{
free(X);
}
NODE insert_front(NODE first,int item)
{
NODE temp;
temp=getnode();
temp->info=item;
temp->link=first;
return(temp);
}
NODE delete_front(NODE first)
{
NODE temp;
if(first==NULL)
{
printf("List is empty\n");
return first;
}
temp=first;
temp=temp->link;
printf("Deleted data is %d\n",first->info);
freenode(first);
return(temp);
}
void display(NODE first)
{
NODE temp;
if(first==NULL)
{
printf("List is empty\n");
return;
27
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


}
printf("Contents of the Linked list are\n");
temp=first;
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->link;
}
printf("\n");
}
int main()
{
int choice,item;
NODE first=NULL;
for(;;)
{
printf("Enter your choice\n1 Insert rear\n2 Delete rear\n3 Display\n4
Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the item\n");
scanf("%d",&item);
first=insert_front(first,item);
break;
case 2: first=delete_front(first);
break;
case 3: display(first);
break;
case 4: exit(0);
}
}
}

28
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

16. Write a program to implement queue using dynamic memory allocation.


#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
typedef struct node * NODE;
NODE getnode()
{
NODE X;
X=(NODE)malloc(sizeof(struct node));
if(X==NULL)
{
printf("Memeory not available\n");
}
return(X);
}
void freenode(NODE X)
{
free(X);
}
NODE insert_rear(int item,NODE first)
{
NODE temp,cur;
temp=getnode();
temp->info=item;
temp->link=NULL;
if(first==NULL)
return (temp);
cur=first;
while(cur->link!=NULL)
{
cur=cur->link;
}
cur->link=temp;
return (first);
}
NODE delete_front(NODE first)
{
NODE temp;
if(first==NULL)
{
printf("List is empty\n");
return(first);
}
temp=first;
29
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


temp=temp->link;
printf("Deleted data is %d\n",first->info);
freenode(first);
return(temp);
}

void display(NODE first)


{
NODE temp;
if(first==NULL)
{
printf("List is empty\n");
return;
}
printf("Contents of the Linked list are\n");
temp=first;
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->link;
}
printf("\n");
}
int main()
{
int choice,item;
NODE first=NULL;
for(;;)
{
printf("Enter your choice");
printf("\n1 Insert rear\n2 Delete front\n3 Display\n4 Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the data\n");
scanf("%d",&item);
first=insert_rear(item,first);
break;
case 2: first=delete_front(first);
break;
case 3: display(first);
break;
case 4: exit(0);
}
}
return 0;
}

30
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

17. Write a program to implement circular linked list.


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
typedef struct node * NODE;
NODE insert_rear(NODE first,int item)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->data=item;
if(first==NULL)
{
first=newnode;
first->link=newnode;
return first;
}
else
{
NODE temp=first;
while(temp->link!=first)
temp=temp->link;
temp->link=newnode;
newnode->link=first;
return first;
}
}

NODE delete_rear(NODE first)


{
if(first==NULL)
{
printf("List is empty\n");
return first;
}
else
{
int itm;
NODE temp=first;
if(first->link==first)
{
itm=first->data;
free(temp);
first=NULL;
}
31
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


else
{
NODE prev=NULL;
while(temp->link!=first)
{
prev=temp;
temp=temp->link;
}
prev->link=first;
itm=temp->data;
free(temp);
}
printf("Deleted data is %d\n",itm);
return first;
}
}
void display(NODE first)
{
if(first==NULL)
{
printf("List is empty");
return;
}
else
{
NODE temp;
temp=first;
printf("Contents of the Circular Linked list are\n");
while(temp->link!=first)
{
printf("%d ",temp->data);
temp=temp->link;
}
printf("%d",temp->data);
}
}
int main()
{
int choice,val;
NODE first=NULL;
for(;;)
{
printf("Enter your choice\n1 Insert rear\n2 Delete rear\n3 Display\n4
Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the data\n");

32
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


scanf("%d",&val);
first=insert_rear(first,val);
break;
case 2: first=delete_rear(first);
break;
case 3: display(first);
printf("\n");
break;
case 4: exit(0);
}
}
return 0;
}

33
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

18. Write a program to implement doubly linked list.


#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink, *rlink;
};
typedef struct node * NODE;
NODE insert_front(NODE first,int val)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->info=val;
newnode->llink=NULL;
if(first==NULL)
{
first=newnode;
newnode->rlink=NULL;
return first;
}
else
{
newnode->rlink=first;
first->llink=newnode;
return newnode;
}
}
NODE delete_rear(NODE first)
{
if(first==NULL)
{
printf("List is Empty\n");
return first;
}
int del;
NODE cur=first;
if(first->rlink==NULL)
{
del=first->info;
free(first);
first=NULL;
}
else
{
NODE prev=NULL;
while(cur->rlink!=NULL)
{
34
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


cur=cur->rlink;
}
prev=cur->llink;
prev->rlink=NULL;
del=cur->info;
free(cur);
}
printf("Deleted data is %d\n",del);
return first;
}
void display(NODE first)
{
if(first==NULL)
{
printf("List is Empty\n");
return;
}
else
{
NODE temp=first;
while(temp!=NULL)
{
printf("%d ",temp->info);
temp=temp->rlink;
}
printf("\n");
}
}

int main()
{
NODE first=NULL;
int choice,val;
while(1)
{
printf("Enter your choice\n1 Insert front\n2 Delete rear\n3 Display\n4
Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the data\n");
scanf("%d",&val);
first=insert_front(first,val);
break;
case 2: first=delete_rear(first);
break;
case 3: display(first);

35
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


break;
case 4: exit(0);
}
}
return 0;
}

36
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

19. Write a program to implement circular doubly linked list.

#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink, *rlink;
};
typedef struct node * NODE;
NODE insert_front(NODE first,int val)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->info=val;
if(first==NULL)
{
first=newnode;
newnode->llink=newnode;
newnode->rlink=newnode;
return first;
}
else
{
NODE last=first->llink;
newnode->rlink=first;
newnode->llink=last;
last->rlink=newnode;
first->llink=newnode;
return newnode;
}
}

NODE delete_rear(NODE first)


{
if(first==NULL)
{
printf("List is Empty\n");
return first;
}
int del;
NODE cur=first;
if(first->rlink==first)
{
del=first->info;
free(first);
first=NULL;
}
37
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


else
{
NODE prev=NULL;
cur=first->llink;
prev=cur->llink;
prev->rlink=first;
first->llink=prev;
del=cur->info;
free(cur);
}
printf("Deleted data is %d\n",del);
return first;
}
void display(NODE first)
{
if(first==NULL)
{
printf("List is Empty\n");
return;
}
else
{
NODE temp=first;
while(temp->rlink!=first)
{
printf("%d ",temp->info);
temp=temp->rlink;
}
printf("%d \n",temp->info);
}
}
int main()
{
NODE first=NULL;
int choice,val;
while(1)
{
printf("Enter your choice\n1 Insert front\n2 Delete rear\n3 Display\n4
Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the data\n");
scanf("%d",&val);
first=insert_front(first,val);
break;
case 2: first=delete_rear(first);
break;

38
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


case 3: display(first);
break;
case 4: exit(0);
}
}
return 0;
}

39
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

20. Write a program to create a binary tree and implement the inorder, preorder and postorder
travarsals.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *lchild;
struct node *rchild;
};
typedef struct node * NODE;
NODE create_B_Tree()
{
NODE newnode; int data=0;
newnode=NULL;
printf("Enter data ('0' if no data)");
scanf("%d",&data);
if(data)
{
newnode=(NODE)malloc(sizeof(struct node));
newnode->info=data;
printf("\nLeft child of %d\n",newnode->info);
newnode->lchild=create_B_Tree();
printf("\nRight child of %d\n",newnode->info);
newnode->rchild=create_B_Tree();
}
return newnode;
}
void pre_order(NODE root)
{
if(root!=NULL)
{
printf("%d\n",root->info);
pre_order(root->lchild);
pre_order(root->rchild);
}
}
void post_order(NODE root)
{
if(root!=NULL)
{
post_order(root->lchild);
post_order(root->rchild);
printf("%d\n",root->info);
}
}
void in_order(NODE root)
{
40
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


if(root!=NULL)
{
in_order(root->lchild);
printf("%d\n",root->info);
in_order(root->rchild);
}
}
int main()
{
printf("Create binary tree, start from root\n");
NODE root;
root=create_B_Tree();
while(1)
{
printf("Select mode of traversal for displaying the binary tree\n");
printf("1 Pre-order\n2 Post-order\n3 In-order\n4 Exit\nChoice \n");
int choice;
scanf("%d",&choice);
switch(choice)
{
case 1: pre_order(root);
printf("\n");
break;
case 2: post_order(root);
printf("\n");
break;
case 3: in_order(root);
printf("\n");
break;
case 4: exit(0);
}
}
return 0;
}

41
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

21. Write a program to construct a binary search tree and construct postfix expression tree.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#define max 100
struct node
{
int data;
struct node *llink,*rlink;
};
typedef struct node * NODE;
NODE construct_tree(char postfix[max])
{
if(postfix[0]==’\0’)
return NULL;
char sym;
NODE newnode,stack[max];
int i,top=-1;
for(i=0;postfix[i]!='\0';i++)
{
sym=postfix[i];
newnode=(NODE)malloc(sizeof(struct node));
newnode->data=sym;
newnode->llink=NULL;
newnode->rlink=NULL;
if(isdigit(sym))
{
stack[++top]=newnode;
continue;
}
switch(sym)
{
case '+':
case '-':
case '*':
case '/': newnode->rlink=stack[top--];
newnode->llink=stack[top--];
stack[++top]=newnode;
break;
default: return NULL;
}
}
return stack[top--];
}
float evaluate(NODE root)
{
switch(root->data)
{
42
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


case '+': return(evaluate(root->llink)+evaluate(root->rlink));
case '-': return(evaluate(root->llink)-evaluate(root->rlink));
case '*': return(evaluate(root->llink)*evaluate(root->rlink));
case '/': return(evaluate(root->llink)/evaluate(root->rlink));
default: return(root->data - '0');
}
}
int main()
{
float res;
char postfix[max];
NODE root=NULL;
printf("Enter the postfix expression\n");
gets(postfix);
root=construct_tree(postfix);
if(root==NULL)
{
printf("Invalid input\n");
return 0;
}
res=evaluate(root);
printf("Result after evaluation is %.2f\n",res);
return 0;
}

43
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering

22. Write a C program to implement a binary search tree of integers and perform the following
traversal techniques:
i) In-order traversal
ii )Find the maximum element
iii) Search an element
Display "Duplication is not allowed" if same element is inserted
again. Display "Key element not found" if searched element is not
present in the tree. Display "Search is successful" if searched
element is present in the tree.
Displpay "Maximum element is 15" if the maximum element is 15 in the tree.

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *llink,*rlink;
};
typedef struct node * NODE;
NODE create_node(int val)
{
NODE newnode;
newnode=(NODE)malloc(sizeof(struct node));
newnode->data=val;
newnode->llink=NULL;
newnode->rlink=NULL;
return newnode;
}
void setleft(NODE p,int val)
{
if(p==NULL)
printf("Insertion is not possible\n");
else if(p->llink!=NULL)
printf("Invalid insertion\n");
else
p->llink=create_node(val);
}

void setright(NODE p,int val)


{
if(p==NULL)
printf("Insertion is not possible\n");
else if(p->rlink!=NULL)
printf("Invalid insertion\n");
else
p->rlink=create_node(val);
}

44
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


NODE create_BS_Tree(NODE root,int val)
{
if(root==NULL)
{
root=create_node(val);
}
else
{
NODE p,q;
p=q=root;
while(val!=p->data&&q!=NULL)
{
p=q;
if(val<p->data)
q=p->llink;
else
q=p->rlink;
}
if(val==p->data)
{
printf("Duplication is not allowed\n");
}
else if(val<p->data)
setleft(p,val);
else
setright(p,val);
}
return root;
}
void inorder(NODE root)
{
if(root!=NULL)
{
inorder(root->llink);
printf("%d ",root->data);
inorder(root->rlink);
}
}
void search(NODE root,int key)
{
NODE p,q;
p=q=root;
while(key!=p->data&&q!=NULL)
{
p=q;
if(key<p->data)
q=p->llink;
else

45
(ISO 9001:2015 Certified)
Accredited with ‘A’ Grade by NAAC

Department of Computer and Communication Engineering


q=p->rlink;
}
if(key==p->data)
printf("Search is successful\n");
else
printf("Key element not found\n");
}
int maximum(NODE root)
{
while(root->rlink!=NULL)
{
root=root->rlink;
}
return root->data;
}
int main()
{
int choice, val,key,max;
NODE root=NULL;
printf("1 Insert\n2 In-order\n3 Search\n4 Maximum\n5 Exit\n");
while(1)
{
printf("Enter your Choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the element\n");
scanf("%d",&val);
root=create_BS_Tree(root,val);
break;
case 2: inorder(root);
printf("\n");
break;
case 3: printf("Enter the element to be searched\n");
scanf("%d",&key);
search(root,key);
break;
case 4: max=maximum(root);
printf("Maximum element is %d\n",max);
break;
case 5: exit(0);
break;
default:printf("Invalid choice\n");
}
}
return 0;
}

46

You might also like