Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
53 views107 pages

DS Lab Manual Raja

Ds lab manual

Uploaded by

shreyase991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views107 pages

DS Lab Manual Raja

Ds lab manual

Uploaded by

shreyase991
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 107

VISVESWARAYA TECHNOLOGICAL

UNIVERSITY BELAGAVI

LAB MANUAL
Of
DATA STRUCTURES AND APPLICATION LAB
Subject Code: BCSL305
For

III SEMESTER B.E


COMPUTER SCIENCE & ENGINEERING
2024-25

Compiled by
RAJAKANTH C B
ASST. PROF
Dept of CSE
AIEMS

AMRUTA INSTITUTE OF ENGINEERING AND MANAGEMENT SCIENCES


BIDADI, BANGLORE-562109
AMRUTA INSTITUTE OF ENGINEERING AND MANAGEMENT SCIENCES
Affiliated to VTU, Recognized by GOK, Approved by AICTE, New Delhi
(NAAC ‘B++ Grade’ Accredited) Bidadi Industrial Town, Ramanagaram,
Near Toyota Kirloskar Motors Road, Bengaluru, Karnataka-562109
Ph no:+91 9141201831,[email protected].

DEPARTMENT OF CSE

VISION OF THE DEPARTMENT


To produce quality Computer Science professionals by providing excellent technical knowledge,
skills, personality through education and research with moral values.

MISSION OF THE DEPARTMENT


 To provide excellent teaching learning environment to become globally competent computer.

 To involve in research activity and be committed for lifelong learning in making


positive contribution to the Students
COURSE OUTCOMES
Course Outcomes: At the end of this course, students are able to:
CO1- Analyze various linear and non-linear data structures
CO2- Demonstrate the working nature of different types of data structures and their applications
CO3- Use appropriate searching and sorting algorithms for the given scenarios
CO4- Apply the appropriate data structure for solving real-world problems

REVISED BLOOMS TAXONOMY (RBT)


PROGRAM LIST

Sl. NO. Program Description Page No.

1 Develop a Program in C for the following: a) Declare a calendar as an array of 7


elements (A dynamically Created array) to represent 7 days of a week. Each
Element of the array is a structure having three fields. The first field is the name
of the Day (A dynamically allocated String), The second field is the date of the
1-6
Day (A integer), the third field is the description of the activity for a particular
day (A dynamically allocated String). b) Write functions create(), read() and
display(); to create the calendar, to read the data from the keyboard and to print
weeks activity details report on screen.

2 Develop a Program in C for the following operations on Strings. a. Read a main


String (STR), a Pattern String (PAT) and a Replace String (REP) b. Perform
Pattern Matching Operation: Find and Replace all occurrences of PAT in STR
7-9
with REP if PAT exists in STR. Report suitable messages in case PAT does not
exist in STR Support the program with functions for each of the above operations.
Don't use Built-in functions.

3 Develop a menu driven Program in C for the following operations on STACK of


Integers (Array Implementation of Stack with maximum size MAX) a. Push an
Element on to Stack b. Pop an Element from Stack c. Demonstrate how Stack
10-16
can be used to check Palindrome d. Demonstrate Overflow and Underflow
situations on Stack e. Display the status of Stack f. Exit Support the program with
appropriate functions for each of the above operations

4 Develop a Program in C for converting an Infix Expression to Postfix Expression.


Program should support for both parenthesized and free parenthesized
17-21
expressions with the operators: +, -, *, /, % (Remainder), ^ (Power) and
alphanumeric operands.

5 Develop a Program in C for the following Stack Applications a. Evaluation of


Suffix expression with single digit operands and operators: +, -, *, /, %, ^ b. 22-25
Solving Tower of Hanoi problem with n disks

6 Develop a menu driven Program in C for the following operations on Circular


QUEUE of Characters (Array Implementation of Queue with maximum size
MAX) a. Insert an Element on to Circular QUEUE b. Delete an Element from
26-33
Circular QUEUE c. Demonstrate Overflow and Underflow situations on Circular
QUEUE d. Display the status of Circular QUEUE e. Exit Support the program
with appropriate functions for each of the above operations

7 Develop a menu driven Program in C for the following operations on Singly


Linked List (SLL) of Student Data with the fields: USN, Name, Programme, Sem, 34-49
PhNo a. Create a SLL of N Students Data by using front insertion. b.
Display the status of SLL and count the number of nodes in it c. Perform Insertion
/ Deletion at End of SLL d. Perform Insertion / Deletion at Front of
SLL(Demonstration of stack) e. Exit

8 Develop a menu driven Program in C for the following operations on Doubly


Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept,
Designation, Sal, PhNo a. Create a DLL of N Employees Data by using end
insertion. b. Display the status of DLL and count the number of nodes in it c. 50-69
Perform Insertion and Deletion at End of DLL d. Perform Insertion and Deletion
at Front of DLL e. Demonstrate how this DLL can be used as Double Ended
Queue. f. Exit

9 Develop a Program in C for the following operationson Singly Circular Linked


List (SCLL) with header nodes a. Represent and Evaluate a Polynomial P(x,y,z)
= 6x 2 y 2 z-4yz 5 +3x 3 yz+2xy 5 z-2xyz 3 b. Find the sum of two polynomials
70-80
POLY1(x,y,z) and POLY2(x,y,z) and store the result in POLYSUM(x,y,z)
Support the program with appropriate functions for each of the above operations

10 Develop a menu driven Program in C for the following operations on Binary


Search Tree (BST) of Integers . a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15,
24, 14, 7, 8, 5, 2 b. Traverse the BST in Inorder, Preorder and Post Order c. 80-88
Search the BST for a given element (KEY) and report the appropriate message
d. Exit

11 Develop a Program in C for the following operations on Graph(G) of Cities a.


Create a Graph of N cities using Adjacency Matrix. b. Print all the nodes 89-93
reachable from a given starting node in a digraph using DFS/BFS method

12 Given a File of N employee records with a set K of Keys (4-digit) which uniquely
determine the records in file F. Assume that file F is maintained in memory by a
Hash Table (HT) of m memory locations with L as the set of memory addresses
(2-digit) of locations in HT. Let the keys in K and addresses in L are Integers.
94-97
Develop a Program in C that uses Hash function H: K →L as H(K)=K mod m
(remainder method), and implement hashing technique to map a given key K to
the address space L. Resolve the collision (if any) using linear probing.

13 Sample Viva Questions 98-102

AIEMS
Program-1
Develop a Program in C for the following:
a) Declare a calendar as an array of 7 elements (A dynamically Created array) to
represent 7 days of a week. Each Element of the array is a structure having three
fields. The first field is the name of the Day (A dynamically allocated String), The
second field is the date of the Day (A integer), the third field is the description of the
activity for a particular day (A dynamically allocated String).
b) Write functions create(), read() and display(); to create the calendar, to read the data
from the keyboard and to print weeks activity details report on screen.

# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# define DAYS 7

struct activity
{
char *nday;
int d,m,y;
char *desc;
};
typedef struct activity Plan;
Plan* create();
void read(Plan *);
void display(Plan *);
void main()
{
Plan *cal = NULL;
cal = create( );

1
read(cal);
display(cal);
}
Plan* create()
{
Plan *t = (Plan *) malloc(sizeof(Plan)*7);
if (t == NULL)
{
printf("Sufficient memory not allocated\n");
return 0;
}
return t;
}
void read(Plan *p)
{
int i;
for(i=0;i<DAYS;i++)
{
p[i].nday = (char *) malloc(9);
printf("Enter name of the day :\n");
scanf(" %s",p[i].nday);
printf("Enter date of the day (DD MM YYYY)Formate:\n ");
scanf("%d%d%d",&(p[i].d),&(p[i].m),&(p[i].y));
printf("Enter description of the activity :\n");
p[i].desc = (char *) malloc(400);
scanf(" %s",p[i].desc);
p[i].desc = (char *) realloc(p[i].desc,strlen(p[i].desc)+1);
}
}

2
void display(Plan *p)
{
int i;
printf("**** Activity description for %d days ****\n",DAYS);
for(i=0;i<DAYS;i++)
{
printf("\nName of the day : %s", p[i].nday);
printf("\nDate of the day : %d %d %d", p[i].d,p[i].m,p[i].y);
printf("\nActivity description %s: ", p[i].desc);
}
}

3
Output:
Enter name of the day :
Monday
Enter date of the day (DD MM YYYY)Formate:
11 11 2024
Enter description of the activity :
Sports
Enter name of the day :
Tuesday
Enter date of the day (DD MM YYYY)Formate:
12 11 2024
Enter description of the activity :
project
Enter name of the day :
Wednesday
Enter date of the day (DD MM YYYY)Formate:
13 11 2024
Enter description of the activity :
Seminar
Enter name of the day :
Thrusday
Enter date of the day (DD MM YYYY)Formate:
14 11 2024
Enter description of the activity :
Miniproject
Enter name of the day :
Firday
Enter date of the day (DD MM YYYY)Formate:
15 11 2024

4
Enter description of the activity :
SCR
Enter name of the day :
Saturday
Enter date of the day (DD MM YYYY)Formate:
16 11 2024
Enter description of the activity :
Internals
Enter name of the day :
Sunday
Enter date of the day (DD MM YYYY)Formate:
17 11 2024
Enter description of the activity :
Holiday
**** Activity description for 7 days ****

Name of the day : Monday


Date of the day : 11 11 2024
Activity description Sports:
Name of the day : Tuesday
Date of the day : 12 11 2024
Activity description project:
Name of the day : Wednesday
Date of the day : 13 11 2024
Activity description Seminar:
Name of the day : Thrusday
Date of the day : 14 11 2024
Activity description Miniproject:
Name of the day : Firday

5
Date of the day : 15 11 2024
Activity description SCR:
Name of the day : Saturday
Date of the day : 16 11 2024
Activity description Internals:
Name of the day : Sunday
Date of the day : 17 11 2024
Activity description Holiday:
Process returned 31 (0x1F) execution time : 176.977 s
Press any key to continue.

6
Program-2
Develop a Program in C for the following operations on Strings.
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in
STR with REP if PAT exists in STR. Report suitable messages in case PAT does not
exist in STR Support the program with functions for each of the above operations.
Don't use Built-in functions.

#include <stdio.h>
#include <stdlib.h>
int main()
{
char STR[100],PAT[100],REP[100],ans[100];

int i,j,c,m,k,flag=0;
printf("\nEnter the MAIN string: \n");
gets(STR);

printf("\nEnter a PATTERN string: \n");


gets(PAT);

printf("\nEnter a REPLACE string: \n");


gets(REP);

i = m = c = j = 0;
while ( STR[c] != '\0')

{
if ( STR[m] == PAT[i])

{
i++;
m++;

flag=1;

7
if ( PAT[i] == '\0')
{

for(k=0; REP[k] !='\0';k++,j++)


ans[j] = REP[k];

i=0;
c=m;

}
}
else

{
ans[j] = STR[c];

j++;
c++;

m = c; i=0;
}

if(flag==0)
{
printf("Pattern doesn't found!!!");

}
else

{
ans[j] = '\0';

printf("\nThe RESULTANT string is:%s\n" ,ans);


}

8
Output:
Enter the MAIN string:
Raja
Enter a PATTERN string:
Ra
Enter a REPLACE string:
Ga
The RESULTANT string is:Gaja

Enter the MAIN string:


Raja
Enter a PATTERN string:
Mn
Enter a REPLACE string:
Ga
Pattern doesn't found!!!
Process returned 0 (0x0) execution time : 10.944 s
Press any key to continue.

9
Program-3
Develop a menu driven Program in C for the following operations on STACK of Integers
(Array Implementation of Stack with maximum size MAX)
a. Push an Element on to Stack
b. Pop an Element from Stack
c. Demonstrate how Stack can be used to check Palindrome
d. Demonstrate Overflow and Underflow situations on Stack
e. Display the status of Stack
f. Exit Support the program with appropriate functions for each of the above operations

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#define max_size 5
int stack[max_size], top=-1, flag=1;
int i, temp, item, rev[max_size], num[max_size];
void push();
void pop();
void display();
void pali();
int main()
{
int choice;
printf("\n\n--------STACK OPERATIONS--------");
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Palindrome\n");
printf("4.Display\n");
printf("5.Exit\n");
printf(" ");

10
while(1)
{
printf("\nEnter your choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
if(flag)
printf("\nThe poped element: %d\t",item);
temp=top;
break;
case 3: pali();
top=temp;
break;
case 4: display();
break;
case 5: exit(0);
break;
default: printf("\nInvalid choice:\n");
break;
}
}
//return 0;
}

11
void push() //Inserting element into the stack
{
if(top==(max_size-1))
{
printf("\nStack Overflow:");
}
else
{
printf("Enter the element to be inserted:\t");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
temp=top;
}
void pop() //deleting an element from the stack
{
if(top==-1)
{
printf("Stack Underflow:");
flag=0;
}
else
{
item=stack[top];
top=top-1;
}
}

12
void pali()
{
i=0;
if(top==-1)
{
printf("Push some elements into the stack first\n");
}
else
{
while(top!=-1)
{
rev[top]=stack[top]; pop();
}
top=temp;
for(i=0;i<=temp;i++)
{
if(stack[top--]==rev[i])
{
if(i==temp)
{
printf("Palindrome\n"); return;
}
}
}
printf("Not Palindrome\n");
}
}

13
void display()
{
int i; top=temp;
if(top==-1)
{
printf("\nStack is Empty:");
}
else
{
printf("\nThe stack elements are:\n" );
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}
}

14
Output:
--------STACK OPERATIONS--------
1.Push
2.Pop
3.Palindrome
4.Display
5.Exit
Enter your choice: 1
Enter the element to be inserted: 3
Enter your choice: 1
Enter the element to be inserted: 6
Enter your choice: 1
Enter the element to be inserted: 5
Enter your choice: 1
Enter the element to be inserted: 6
Enter your choice: 1
Enter the element to be inserted: 3
Enter your choice: 1
Stack Overflow:

Enter your choice: 3


Palindrome
Enter your choice: 4
The stack elements are:
3
6
5
6
3

15
Enter your choice: 2
The poped element: 3
Enter your choice: 3
Not Palindrome
Enter your choice: 2
The poped element: 6
Enter your choice: 2
The poped element: 5
Enter your choice: 2
The poped element: 6
Enter your choice: 2
The poped element: 3
Enter your choice: 2
Stack Underflow:
Enter your choice: 4
Stack is Empty:
Enter your choice: 5
Process returned 0 (0x0) execution time : 99.348 s
Press any key to continue.

16
Program-4
Develop a Program in C for converting an Infix Expression to Postfix Expression. Program
should support for both parenthesized and free parenthesized expressions with the
operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric operands.

#include<stdio.h>
#include<stdlib.h>
void evaluate();
void push(char);
char pop();
int prec(char);
char infix[30], postfix[30], stack[30];
int top = -1;
int main()
{
printf("\nEnter the valid infix expression:\t");
scanf("%s", infix);
evaluate();
printf("\nThe entered infix expression is :\n %s \n", infix);
printf("\nThe corresponding postfix expression is :\n %s \n", postfix);
}
void evaluate()
{
int i = 0, j = 0;
char symb, temp;

push('#');

for(i=0; infix[i] != '\0'; i++)

17
{
symb = infix[i];
switch(symb)
{
case '(' :push(symb);
break;

case ')' : temp = pop();


while(temp != '(' )
{
postfix[j] = temp;
j++;
temp = pop();
}
break;

case '+' :
case '-' :
case '*' :
case '/' :
case '%' :
case '^' :
case '$' : while( prec(stack[top]) >= prec(symb) )
{
temp = pop();
postfix[j] = temp;
j++;
}
push(symb);

18
break;
default:postfix[j] = symb;
j++;
}
}
while(top > 0)
{
temp = pop();
postfix[j] = temp;
j++;
}
postfix[j] = '\0';
}
void push(char item)
{
top = top+1;
stack[top] = item;
}
char pop()
{
char item;
item = stack[top];
top = top-1;
return item;
}

19
int prec(char symb)
{
int p;
switch(symb)
{
case '#' : p = -1;
break;

case '(' :
case ')' : p = 0;
break;

case '+' :
case '-' : p = 1;
break;

case '*' :
case '/' :
case '%' : p = 2;
break;
case '^' :
case '$' : p = 3;
break;
}
return p;
}

20
Output:
Enter the valid infix expression: (A+B)*C+(D-E)^F
The entered infix expression is :
(A+B)*C+(D-E)^F
The corresponding postfix expression is :
AB+C*DE-F^+
Process returned 0 (0x0) execution time : 25.388 s
Press any key to continue.

21
Program-5
Develop a Program in C for the following Stack Applications
a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /,
%,^.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int i, top = -1;
int op1, op2, res, s[20];
char postfix[90], symb;

void push(int item)


{
top = top+1;

s[top] = item;
}

int pop()
{
int item;
item = s[top];
top = top-1;
return item;
}

22
int main()
{

printf("\nEnter a valid postfix expression:\n");


scanf("%s", postfix);
for(i=0; postfix[i]!='\0'; i++)
{
symb = postfix[i];
if(isdigit(symb))
{
push(symb - '0');
}
else
{
op2 = pop();
op1 = pop();
switch(symb)
{
case '+':push(op1+op2);
break;
case '-':push(op1-op2);
break;
case '*':push(op1*op2);
break;
case '/':push(op1/op2);
break;
case '%':push(op1%op2);
break;

23
case '$':
case '^':push(pow(op1, op2));
break;
default :push(0);
}
}
}
res = pop();
printf("\n Result = %d", res);
}

Output:
Enter a valid postfix expression:
23*7+38
Result = 8
Process returned 0 (0x0) execution time : 25.913 s
Press any key to continue.

24
b. Solving Tower of Hanoi problem with n disks
#include<stdio.h>
#include<math.h>
void tower(int n, char from_peg, char aux_peg, char to_peg);
void main(){
int n;
printf("\nEnter the number of disks: ");
scanf("%d", &n);
tower(n, 'A', 'B', 'C');
printf("\nTotal number of moves = %0.0f", pow(2,n)-1 );
}
void tower(int n, char from_peg, char aux_peg, char to_peg){
if(n == 1) {
printf("\nMove disk %d from %c peg to %c peg", n, from_peg, to_peg);
return; }
tower(n-1, from_peg, to_peg, aux_peg);
printf("\nMove disk %d from peg %c to %c peg", n, from_peg, to_peg);
tower(n-1, aux_peg, from_peg, to_peg);
}
Output:-
Enter the number of disks: 3
Move disk 1 from A peg to C peg
Move disk 2 from peg A to B peg
Move disk 1 from C peg to B peg
Move disk 3 from peg A to C peg
Move disk 1 from B peg to A peg
Move disk 2 from peg B to C peg
Move disk 1 from A peg to C peg
Total number of moves = 7

25
Program-6
Develop a menu driven Program in C for the following operations on Circular QUEUE of
Characters (Array Implementation of Queue with maximum size MAX)
a. Insert an Element on to Circular QUEUE
b. Delete an Element from Circular QUEUE
c. Demonstrate Overflow and Underflow situations on Circular QUEUE
d. Display the status of Circular QUEUE
e. Exit Support the program with appropriate functions for each of the above operations

#include <stdio.h>
#include <stdlib.h>
#define Max 3
void insert(char [], int *, int *);
void del(char [], int *, int *);
void display(char [], int, int);
int main()
{
char q[Max];
int r=-1,f=0,cnt=0; int ch;
while(1)
{
printf("1: Insert\n2: Delete\n3: Display\n4: Exit\n");
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: insert(q,&r,&cnt);
break;
case 2: del(q,&f,&cnt);

26
break;
case 3: display(q,f,cnt);
break;
default: exit(0);
}
}
}

void insert(char q[],int *r,int *cnt)


{
int ele;
if((*cnt) == Max)
{
printf("C Q overflow\n");
return;
}
(*r) = ((*r)+ 1 ) % Max;
printf("enter the element\n");
scanf(" %d", &ele);
q[*r]=ele;
(*cnt)++;
}

void del(char q[],int *f,int *cnt)


{
if((*cnt) == 0)
{
printf("C Queue is empty\n");

27
return;
}
printf("Element deleted from circular queue is %d\n",q[(*f)]);
(*f) = ((*f) + 1) % Max;
(*cnt)--;
}

void display(char q[], int f, int cnt)


{
int i,j;
if (cnt==0)
{
printf("Circular Queue is empty\n");
return;
}
printf("Circular Queue contents are\n");
for(i=f,j=0;j<cnt;j++)
{
printf("%d : %d\n",i,q[i]); i = (i + 1) % Max;
}
}

28
Output:
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
1
enter the element
6
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
1
enter the element
2
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
1
enter the element
5
1: Insert
2: Delete
3: Display
4: Exit

29
Enter choice
1
C Q overflow
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
2
Element deleted from circular queue is 6
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
1
enter the element
8
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
1
C Q overflow
1: Insert
2: Delete
3: Display
4: Exit

30
Enter choice
3
Circular Queue contents are
1:2
2:5
0:8
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
2
Element deleted from circular queue is 2
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
1
enter the element
89
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
3
Circular Queue contents are
2:5

31
0:8
1 : 89
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
2
Element deleted from circular queue is 5
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
2
Element deleted from circular queue is 8
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
2
Element deleted from circular queue is 89
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
2

32
C Queue is empty
1: Insert
2: Delete
3: Display
4: Exit
Enter choice
4

Process returned 0 (0x0) execution time : 133.458 s


Press any key to continue.

33
Program-7
Develop a menu driven Program in C for the following operations on Singly Linked List
(SLL) of Student Data with the fields: USN, Name, Programme, Sem, PhNo
a. Create a SLL of N Students Data by using front insertion.
b. Display the status of SLL and count the number of nodes in it
c. Perform Insertion / Deletion at End of SLL
d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack)
e. Exit

#include<stdio.h>
#include<stdlib.h>
struct node
{
char usn[50],name[50],branch[50];
int sem;
long int phone;
struct node *link;
};
typedef struct node * NODE;
NODE start = NULL;
int count=0;

NODE create()
{
NODE snode;
snode = (NODE)malloc(sizeof(struct node));

if(snode == NULL)
{

34
printf("\nMemory is not available");
exit(1);
}
printf("\nEnter the usn,Name,Branch, sem,PhoneNo of the student:");
scanf("%s %s %s %d %ld",snode->usn, snode->name, snode->branch, &snode->sem, &snode->phone);
snode->link=NULL;
count++;
return snode;
}

NODE insertfront()
{
NODE temp;
temp = create();
if(start == NULL)
{
return temp;
}
temp->link = start;
return temp;
}

NODE deletefront()
{
NODE temp;
if(start == NULL)
{
printf("\nLinked list is empty");
return NULL;

35
}
if(start->link == NULL)
{
printf("\nThe Student node with usn:%s is deleted ",start->usn);
count--;
free(start);
return NULL;
}
temp = start;
start = start->link;
printf("\nThe Student node with usn:%s is deleted",temp->usn);
count--;
free(temp);
return start;
}

NODE insertend()
{
NODE cur,temp;
temp = create();

if(start == NULL)
{
return temp;
}
cur = start;
while(cur->link !=NULL)
{
cur = cur->link;

36
}
cur->link = temp;
return start;
}

NODE deleteend()
{
NODE cur,prev;
if(start == NULL)
{
printf("\nLinked List is empty");
return NULL;
}
if(start->link == NULL)
{
printf("\nThe student node with the usn:%s is deleted",start->usn);
free(start);
count--;
return NULL;
}
prev = NULL;
cur = start;
while(cur->link!=NULL)
{
prev = cur;
cur = cur->link;
}

printf("\nThe student node with the usn:%s is deleted",cur->usn);

37
free(cur);
prev->link = NULL;
count--;
return start;
}
void display()
{
NODE cur;
int num=1;
if(start == NULL)
{

printf("\nNo Contents to display in SLL \n");


return;
}
printf("\nThe contents of SLL: \n");
cur = start;
while(cur!=NULL)
{
printf("\n||%d|| USN:%s| Name:%s| Branch:%s| Sem:%d| Ph:%ld|",num,cur->usn, cur->name,cur->branch,
cur->sem,cur->phone);
cur = cur->link;
num++;
}
printf("\n No of student nodes is %d \n",count);
}

38
void stackdemo()
{
int ch;
while(1)
{
printf("\n~~~Stack Demo using SLL~~~\n");
printf("\n 1:Push operation \n 2: Pop operation \n 3: Display \n 4:Exit \n");
printf("\nEnter your choice for stack demo");
scanf("%d",&ch);

switch(ch)
{
case 1: start = insertfront();
break;
case 2: start = deletefront();
break;
case 3: display();
break;
default : return;
}
}
return;
}

int main()
{
int ch,i,n;
while(1)
{

39
printf("\n~~~Menu~~~");
printf("\nEnter your choice for SLL operation \n");
printf("\n 1:Create SLL of Student Nodes");
printf("\n 2:DisplayStatus");
printf("\n 3:InsertAtEnd");
printf("\n 4:DeleteAtEnd");
printf("\n 5:Stack Demo using SLL(Insertion and Deletion at Front)");
printf("\n 6:Exit \n");
printf("\nEnter your choice:");
scanf("%d",&ch);

switch(ch)
{
case 1 : printf("\nEnter the no of students: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
start = insertfront();
break;

case 2: display();
break;

case 3: start = insertend();


break;

case 4: start = deleteend();


break;

case 5: stackdemo();

40
break;

case 6: exit(0);

default: printf("\nPlease enter the valid choice");

}
}
}

41
Output:

~~~Menu~~~
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:1


Enter the no of students: 2
Enter the usn,Name,Branch, sem,PhoneNo of the student:1jb23
raja
cse
3
98745612
Enter the usn,Name,Branch, sem,PhoneNo of the student:1jb24
yuvi
ise
4
58974613

~~~Menu~~~
Enter your choice for SLL operation

42
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:2


The contents of SLL:
||1|| USN:1jb24| Name:yuvi| Branch:ise| Sem:4| Ph:58974613|
||2|| USN:1jb23| Name:raja| Branch:cse| Sem:3| Ph:98745612|
No of student nodes is 2

~~~Menu~~~
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:3


Enter the usn,Name,Branch, sem,PhoneNo of the student:1jb25
virat
Cv
5
459764326

43
~~~Menu~~~
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:2


The contents of SLL:
||1|| USN:1jb24| Name:yuvi| Branch:ise| Sem:4| Ph:58974613|
||2|| USN:1jb23| Name:raja| Branch:cse| Sem:3| Ph:98745612|
||3|| USN:1jb25| Name:virat| Branch:Cv| Sem:5| Ph:459764326|
No of student nodes is 3

~~~Menu~~~
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

44
Enter your choice:4
The student node with the usn:1jb25 is deleted
~~~Menu~~~
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:2


The contents of SLL:
||1|| USN:1jb24| Name:yuvi| Branch:ise| Sem:4| Ph:58974613|
||2|| USN:1jb23| Name:raja| Branch:cse| Sem:3| Ph:98745612|
No of student nodes is 2

~~~Menu~~~
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

45
Enter your choice:1
Enter the no of students: 1
Enter the usn,Name,Branch, sem,PhoneNo of the student:1jb45
Abd
cse
6
16494669415

~~~Menu~~~
Enter your choice for SLL operation

1:Create SLL of Student Nodes


2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:2


The contents of SLL:
||1|| USN:1jb45| Name:Abd| Branch:cse| Sem:6| Ph:-685199769|
||2|| USN:1jb24| Name:yuvi| Branch:ise| Sem:4| Ph:58974613|
||3|| USN:1jb23| Name:raja| Branch:cse| Sem:3| Ph:98745612|
No of student nodes is 3

~~~Menu~~~
Enter your choice for SLL operation

1:Create SLL of Student Nodes

46
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit

Enter your choice:5

~~~Stack Demo using SLL~~~

1:Push operation
2: Pop operation
3: Display
4:Exit

Enter your choice for stack demo1

Enter the usn,Name,Branch, sem,PhoneNo of the student:1jb33


rai
cse
4
4569763165

~~~Stack Demo using SLL~~~

1:Push operation
2: Pop operation
3: Display
4:Exit

47
Enter your choice for stack demo1
Enter the usn,Name,Branch, sem,PhoneNo of the student:bumra
bumra
cse
5
45794642

~~~Stack Demo using SLL~~~

1:Push operation
2: Pop operation
3: Display
4:Exit

Enter your choice for stack demo3


The contents of SLL:
||1|| USN:bumra| Name:bumra| Branch:cse| Sem:5| Ph:45794642|
||2|| USN:1jb33| Name:rai| Branch:cse| Sem:4| Ph:274795869|
No of student nodes is 2

~~~Stack Demo using SLL~~~

1:Push operation
2: Pop operation
3: Display
4:Exit

Enter your choice for stack demo2

48
The Student node with usn:bumra is deleted
~~~Stack Demo using SLL~~~

1:Push operation
2: Pop operation
3: Display
4:Exit

Enter your choice for stack demo


Enter your choice for stack demo2

The Student node with usn:bumra is deleted


~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo4
~~~Menu~~~
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
Enter your choice:6
Process returned 0 (0x0) execution time : 152.287

49
Program-8
Develop a menu driven Program in C for the following operations on Doubly Linked List
(DLL) of Employee Data with the fields: SSN, Name, Dept, Designation,
Sal, PhNo
a. Create a DLL of N Employees Data by using end insertion.
b. Display the status of DLL and count the number of nodes in it
c. Perform Insertion and Deletion at End of DLL
d. Perform Insertion and Deletion at Front of DLL
e. Demonstrate how this DLL can be used as Double Ended Queue.
f. Exit

#include<stdio.h>
#include<stdlib.h>
struct node
{
char ssn[25],name[25],dept[10],designation[25];
int sal;
long int phone;
struct node *llink;
struct node *rlink;
};
typedef struct node* NODE;
NODE first = NULL;
int count=0;
NODE create()
{
NODE enode;
enode = (NODE)malloc(sizeof(struct node));
if( enode== NULL)

50
{
printf("\nRunning out of memory");
exit(0);
}
printf("\nEnter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee: \n");
scanf("%s %s %s %s %d %ld", enode->ssn, enode->name, enode->dept, enode->designation, &enode->sal,
&enode->phone);
enode->llink=NULL;
enode->rlink=NULL;
count++;
return enode;
}
NODE insertfront()
{
NODE temp;
temp = create();
if(first == NULL)
{
return temp;
}
temp->rlink = first;
first->llink = temp;
return temp;
}

void display()
{
NODE cur;
int nodeno=1;

51
cur = first;
if(cur == NULL)
printf("\nNo Contents to display in DLL");
while(cur!=NULL)
{
printf("\nENode:%d||SSN:%s|Name:%s|Department:%s|Designation:%s|Salary:%d|Phone no:%ld",
nodeno, cur->ssn, cur->name,cur->dept, cur->designation, cur->sal, cur->phone);
cur = cur->rlink;
nodeno++;
}
printf("\nNo of employee nodes is %d",count);
}
NODE deletefront()
{
NODE temp;
if(first == NULL)
{
printf("\nDoubly Linked List is empty");
return NULL;
}
if(first->rlink== NULL)
{
printf("\nThe employee node with the ssn:%s is deleted", first->ssn);
free(first);
count--;
return NULL;
}
temp = first;
first = first->rlink;

52
temp->rlink = NULL;
first->llink = NULL;
printf("\nThe employee node with the ssn:%s is deleted",temp->ssn);
free(temp);
count--;
return first;
}

NODE insertend()
{
NODE cur, temp;
temp = create();

if(first == NULL)
{
return temp;
}
cur= first;
while(cur->rlink!=NULL)
{
cur = cur->rlink;
}

cur->rlink = temp;
temp->llink = cur;
return first;
}
NODE deleteend()
{

53
NODE prev,cur;
if(first == NULL)
{
printf("\nDoubly Linked List is empty");
return NULL;
}

if(first->rlink == NULL)
{
printf("\nThe employee node with the ssn:%s is deleted",first->ssn);
free(first);
count--;
return NULL;
}

prev=NULL;
cur=first;

while(cur->rlink!=NULL)
{
prev=cur;
cur = cur->rlink;
}

cur->llink = NULL;
printf("\nThe employee node with the ssn:%s is deleted",cur->ssn);
free(cur);
prev->rlink = NULL;
count--;

54
return first;
}

void deqdemo()
{
int ch;
while(1)
{
printf("\nDemo Double Ended Queue Operation");
printf("\n1:InsertQueueFront\n 2: DeleteQueueFront\n 3:InsertQueueRear\n 4:DeleteQueueRear\n
5:DisplayStatus\n 6: Exit \n");
scanf("%d", &ch);

switch(ch)
{
case 1: first=insertfront();
break;
case 2: first=deletefront();
break;
case 3: first=insertend();
break;
case 4: first=deleteend();
break;
case 5: display();
break;
default : return;
}

55
}
}
int main()
{
int ch,i,n;
while(1)
{
printf("\n\n~~~Menu~~~");
printf("\n1:Create DLL of Employee Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:InsertAtFront");
printf("\n6:DeleteAtFront");
printf("\n7:Double Ended Queue Demo using DLL");
printf("\n8:Exit \n");
printf("\nPlease enter your choice: ");
scanf("%d",&ch);

switch(ch)
{
case 1 : printf("\nEnter the no of Employees: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
first = insertend();
break;

case 2: display();
break;

56
case 3: first = insertend();
break;

case 4: first = deleteend();


break;

case 5: first = insertfront();


break;

case 6: first = deletefront();


break;

case 7: deqdemo();
break;

case 8 : exit(0);
default: printf("\nPlease Enter the valid choice");
}
}

57
Output:
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit

Please enter your choice: 1

Enter the no of Employees: 2

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:


123
Raja
Cse
Asst.prof
50000
489657123

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:


234
kiran
ise
asst.prf
50000

58
7894569874

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit

Please enter your choice: 2

ENode:1||SSN:123|Name:Raja|Department:Cse|Designation:Asst.prof|Salary:50000|Phone no:489657123
ENode:2||SSN:234|Name:kiran|Department:ise|Designation:asst.prf|Salary:50000|Phone no:-695364718
No of employee nodes is 2

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit

59
Please enter your choice: 3

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:


456
Manju
mech
astprf
50000
78564891645

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit

Please enter your choice: 2

ENode:1||SSN:123|Name:Raja|Department:Cse|Designation:Asst.prof|Salary:50000|Phone no:489657123
ENode:2||SSN:234|Name:kiran|Department:ise|Designation:asst.prf|Salary:50000|Phone no:-695364718
ENode:3||SSN:456|Name:Manju|Department:mech|Designation:astprf|Salary:50000|Phone no:1255480317
No of employee nodes is 3

~~~Menu~~~

60
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit

Please enter your choice: 4

The employee node with the ssn:456 is deleted

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit

Please enter your choice: 2

ENode:1||SSN:123|Name:Raja|Department:Cse|Designation:Asst.prof|Salary:50000|Phone no:489657123
ENode:2||SSN:234|Name:kiran|Department:ise|Designation:asst.prf|Salary:50000|Phone no:-695364718
No of employee nodes is 2

61
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit

Please enter your choice: 5

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:


576
yuvi
cv
proff
456203
74589612547

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL

62
8:Exit

Please enter your choice: 2

ENode:1||SSN:576|Name:yuvi|Department:cv|Designation:proff|Salary:456203|Phone no:1575168515
ENode:2||SSN:123|Name:Raja|Department:Cse|Designation:Asst.prof|Salary:50000|Phone no:489657123
ENode:3||SSN:234|Name:kiran|Department:ise|Designation:asst.prf|Salary:50000|Phone no:-695364718
No of employee nodes is 3

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit

Please enter your choice: 6

The employee node with the ssn:576 is deleted

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront

63
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit

Please enter your choice: 2

ENode:1||SSN:123|Name:Raja|Department:Cse|Designation:Asst.prof|Salary:50000|Phone no:489657123
ENode:2||SSN:234|Name:kiran|Department:ise|Designation:asst.prf|Salary:50000|Phone no:-695364718
No of employee nodes is 2

~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit

Please enter your choice: 7

Demo Double Ended Queue Operation


1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit

64
1

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:


213
raki
cse
prof
45689
789654741

Demo Double Ended Queue Operation


1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
1

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:


5432
ramu
cse
prf
458745
85274196

Demo Double Ended Queue Operation


1:InsertQueueFront

65
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
5

ENode:1||SSN:5432|Name:ramu|Department:cse|Designation:prf|Salary:458745|Phone no:85274196
ENode:2||SSN:213|Name:raki|Department:cse|Designation:prof|Salary:45689|Phone no:789654741
ENode:3||SSN:123|Name:Raja|Department:Cse|Designation:Asst.prof|Salary:50000|Phone no:489657123
ENode:4||SSN:234|Name:kiran|Department:ise|Designation:asst.prf|Salary:50000|Phone no:-695364718
No of employee nodes is 4
Demo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
3

Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:


789
abd
cse
principal
7894561
854796258

66
Demo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
5

ENode:1||SSN:5432|Name:ramu|Department:cse|Designation:prf|Salary:458745|Phone no:85274196
ENode:2||SSN:213|Name:raki|Department:cse|Designation:prof|Salary:45689|Phone no:789654741
ENode:3||SSN:123|Name:Raja|Department:Cse|Designation:Asst.prof|Salary:50000|Phone no:489657123
ENode:4||SSN:234|Name:kiran|Department:ise|Designation:asst.prf|Salary:50000|Phone no:-695364718
ENode:5||SSN:789|Name:abd|Department:cse|Designation:principal|Salary:7894561|Phone no:854796258
No of employee nodes is 5
Demo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
2

The employee node with the ssn:5432 is deleted


Demo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear

67
4:DeleteQueueRear
5:DisplayStatus
6: Exit
4

The employee node with the ssn:789 is deleted


Demo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
5

ENode:1||SSN:213|Name:raki|Department:cse|Designation:prof|Salary:45689|Phone no:789654741
ENode:2||SSN:123|Name:Raja|Department:Cse|Designation:Asst.prof|Salary:50000|Phone no:489657123
ENode:3||SSN:234|Name:kiran|Department:ise|Designation:asst.prf|Salary:50000|Phone no:-695364718
No of employee nodes is 3
Demo Double Ended Queue Operation
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
6

68
~~~Menu~~~
1:Create DLL of Employee Nodes
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
7:Double Ended Queue Demo using DLL
8:Exit

Please enter your choice: 8

Process returned 0 (0x0) execution time : 418.265 s


Press any key to continue.

69
Program-9
Develop a Program in C for the following operationson Singly Circular Linked List (SCLL)
with header nodes
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x 2 y 2 z-4yz 5 +3x 3 yz+2xy 5 z-2xyz
3
b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result
in POLYSUM(x,y,z) Support the program with appropriate functions for each of the
above operations.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define COMPARE(x, y) ( (x == y) ? 0 : (x > y) ? 1 : -1)
struct node
{
int coef;
int xexp, yexp, zexp;
struct node *link;
};
typedef struct node *NODE;

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

70
}
return x;
}

NODE attach(int coef, int xexp, int yexp, int zexp, NODE head)
{
NODE temp, cur;
temp = getnode();
temp->coef = coef;
temp->xexp = xexp;
temp->yexp = yexp;
temp->zexp = zexp;
cur = head->link;
while(cur->link != head)
{
cur = cur->link;
}
cur->link = temp;
temp->link = head;
return head;
}

NODE read_poly(NODE head)


{
int i, j, coef, xexp, yexp, zexp, n;
printf("\nEnter the no of terms in the polynomial: ");
scanf("%d", &n);
for(i=1; i<=n; i++)
{

71
printf("\n\tEnter the %d term: ",i);
printf("\n\t\tCoef = ");
scanf("%d", &coef);
printf("\n\t\tEnter Pow(x) Pow(y) and Pow(z): ");
scanf("%d", &xexp);
scanf("%d", &yexp);
scanf("%d", &zexp);
head = attach(coef, xexp, yexp, zexp, head);
}
return head;
}
void display(NODE head)
{
NODE temp;
if(head->link == head)
{
printf("\nPolynomial does not exist.");
return;
}
temp = head->link;

while(temp != head)
{
printf("%dx^%dy^%dz^%d", temp->coef, temp->xexp, temp->yexp, temp->zexp);
temp = temp->link;
if(temp != head)
printf(" + ");
}
}

72
int poly_evaluate(NODE head)
{
int x, y, z, sum = 0;
NODE poly;

printf("\nEnter the value of x,y and z: ");


scanf("%d %d %d", &x, &y, &z);

poly = head->link;
while(poly != head)
{
sum += poly->coef * pow(x,poly->xexp)* pow(y,poly->yexp) * pow(z,poly->zexp);
poly = poly->link;
}
return sum;
}
NODE poly_sum(NODE head1, NODE head2, NODE head3)
{
NODE a, b;
int coef;
a = head1->link;
b = head2->link;
while(a!=head1 && b!=head2)
{
while(1)
{
if(a->xexp == b->xexp && a->yexp == b->yexp && a->zexp == b->zexp)
{
coef = a->coef + b->coef;

73
head3 = attach(coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
b = b->link;
break;
} //if ends here
if(a->xexp!=0 || b->xexp!=0)
{
switch(COMPARE(a->xexp, b->xexp))
{
case -1 : head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
break;

case 0 : if(a->yexp > b->yexp)


{
head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
break;
}
else if(a->yexp < b->yexp)
{
head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
break;
}
else if(a->zexp > b->zexp)
{
head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;

74
break;
}
else if(a->zexp < b->zexp)
{
head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
break;
}
case 1 : head3 = attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a = a->link;
break;
} //switch ends here
break;
} //if ends here
if(a->yexp!=0 || b->yexp!=0)
{
switch(COMPARE(a->yexp, b->yexp))
{
case -1 : head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
break;
case 0 : if(a->zexp > b->zexp)
{
head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
break;
}
else if(a->zexp < b->zexp)
{

75
head3 = attach(b->coef, b->xexp, b->yexp, b->zexp, head3);
b = b->link;
break;
}
case 1 : head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
break;
}
break;
}
if(a->zexp!=0 || b->zexp!=0)
{
switch(COMPARE(a->zexp,b->zexp))
{
case -1 : head3 = attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b = b->link;
break;
case 1 : head3 = attach(a->coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
break;
}
break;
}
}
}
while(a!= head1)
{
head3 = attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a = a->link;

76
}
while(b!= head2)
{
head3 = attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b = b->link;
}
return head3;
}
int main()
{
NODE head, head1, head2, head3;
int res, ch;
head = getnode(); /* For polynomial evalaution */
head1 = getnode(); /* To hold POLY1 */
head2 = getnode(); /* To hold POLY2 */
head3 = getnode(); /* To hold POLYSUM */
head->link=head;
head1->link=head1;
head2->link=head2;
head3->link= head3;

while(1)
{
printf("\n~~Menu~~");
printf("\n1.Represent and Evaluate a Polynomial P(x,y,z)");
printf("\n2.Find the sum of two polynomials POLY1(x,y,z)");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)

77
{
case 1: printf("\n~~~Polynomial evaluation P(x,y,z)~~\n");
head = read_poly(head);
printf("\nRepresentation of Polynomial for evaluation: \n");
display(head);
res = poly_evaluate(head);
printf("\nResult of polynomial evaluation is : %d \n", res);
break;

case 2: printf("\nEnter the POLY1(x,y,z): \n");


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

printf("\nEnter the POLY2(x,y,z): \n");


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

printf("\nPolynomial addition result: \n");


head3 = poly_sum(head1,head2,head3);
display(head3);
break;
case 3: exit(0);
}
}
}

78
Output:
~~Menu~~
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z)
Enter your choice:1
~~~Polynomial evaluation P(x,y,z)~~
Enter the no of terms in the polynomial: 3
Enter the 1 term:
Coef = 6
Enter Pow(x) Pow(y) and Pow(z): 2 2 1
Enter the 2 term:
Coef = -4
Enter Pow(x) Pow(y) and Pow(z): 0 1 5
Enter the 3 term:
Coef = 3
Enter Pow(x) Pow(y) and Pow(z): 1 1 1
Representation of Polynomial for evaluation:
6x^2y^2z^1 + -4x^0y^1z^5 + 3x^1y^1z^1
Enter the value of x,y and z: 1 1 1
Result of polynomial evaluation is : 5

~~Menu~~
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z)
Enter your choice:2
Enter the POLY1(x,y,z):
Enter the no of terms in the polynomial: 3
Enter the 1 term:

79
Coef = 8
Enter Pow(x) Pow(y) and Pow(z): 2 3 6
Enter the 2 term:
Coef = -3
Enter Pow(x) Pow(y) and Pow(z): 5 6 1
Enter the 3 term:
Coef = -2
Enter Pow(x) Pow(y) and Pow(z): 0 1 5
Polynomial 1 is:
8x^2y^3z^6 + -3x^5y^6z^1 + -2x^0y^1z^5
Enter the POLY2(x,y,z):
Enter the no of terms in the polynomial: 3
Enter the 1 term:
Coef = 6
Enter Pow(x) Pow(y) and Pow(z): 1 2 3
Enter the 2 term:
Coef = 2
Enter Pow(x) Pow(y) and Pow(z): 1 3 2
Enter the 3 term:
Coef = -2
Enter Pow(x) Pow(y) and Pow(z): 6 5 3
Polynomial 2 is:
6x^1y^2z^3 + 2x^1y^3z^2 + -2x^6y^5z^3
Polynomial addition result:
8x^2y^3z^6 + -3x^5y^6z^1 + 6x^1y^2z^3 + 2x^1y^3z^2 + -2x^6y^5z^3 + -2x^0y^1z^5
~~Menu~~
1.Represent and Evaluate a Polynomial P(x,y,z)
2.Find the sum of two polynomials POLY1(x,y,z)
Enter your choice:

80
Program-10
Develop a menu driven Program in C for the following operations on Binary Search Tree
(BST) of Integers .
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit

#include<stdio.h>
#include<stdlib.h>
struct BST
{
int data;
struct BST *lchild;
struct BST *rchild;
};
typedef struct BST * NODE;

NODE create()
{
NODE temp;
temp = (NODE) malloc(sizeof(struct BST));
printf("\nEnter The value: ");
scanf("%d", &temp->data);
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}

81
void insert(NODE root, NODE newnode);
void inorder(NODE root);
void preorder(NODE root);
void postorder(NODE root);
void search(NODE root);

void insert(NODE root, NODE newnode)


{
/*Note: if newnode->data == root->data it will be skipped. No duplicate nodes are allowed */
if (newnode->data < root->data)
{
if (root->lchild == NULL)
root->lchild = newnode;
else
insert(root->lchild, newnode);
}
if (newnode->data > root->data)
{
if (root->rchild == NULL)
root->rchild = newnode;
else
insert(root->rchild, newnode);
}
}
void search(NODE root)
{
int key;
NODE cur;

82
if(root == NULL)
{
printf("\nBST is empty.");
return;
}
printf("\nEnter Element to be searched: ");
scanf("%d", &key);
cur = root;
while (cur != NULL)
{
if (cur->data == key)
{
printf("\nKey element is present in BST");
return;
}
if (key < cur->data)
cur = cur->lchild;
else
cur = cur->rchild;
}
printf("\nKey element is not found in the BST");
}
void inorder(NODE root)
{
if(root != NULL)
{
inorder(root->lchild);
printf("%d ", root->data);
inorder(root->rchild);

83
}
}

void preorder(NODE root)


{
if (root != NULL)
{
printf("%d ", root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}

void postorder(NODE root)


{
if (root != NULL)
{
postorder(root->lchild);
postorder(root->rchild);
printf("%d ", root->data);
}
}

int main()
{
int ch, key, val, i, n;
NODE root = NULL, newnode;
while(1)
{

84
printf("\n~~~~BST MENU~~~~");
printf("\n1.Create a BST");
printf("\n2.BST Traversals:");
printf("\n3.Search ");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter the number of elements: ");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
newnode = create();
if (root == NULL)
root = newnode;
else
insert(root, newnode);
}
break;
case 2: if (root == NULL)
printf("\nTree Is Not Created");
else
{
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Inorder display : ");
inorder(root);
printf("\nThe Postorder display : ");

85
postorder(root);
}

break;
case 3: search(root);
break;

case 4: exit(0);
}
}

86
Output:

~~~~BST MENU~~~~
1.Create a BST
2.BST Traversals:
3.Search
4.Exit
Enter your choice: 1
Enter the number of elements: 10
Enter The value: 50
Enter The value: 23
Enter The value: 63
Enter The value: 15
Enter The value: 89
Enter The value: 95
Enter The value: 76
Enter The value: 45
Enter The value: 12
Enter The value: 13
~~~~BST MENU~~~~
1.Create a BST
2.BST Traversals:
3.Search
4.Exit
Enter your choice: 2
The Preorder display : 50 23 15 12 13 45 63 89 76 95
The Inorder display : 12 13 15 23 45 50 63 76 89 95
The Postorder display : 13 12 15 45 23 76 95 89 63 50
~~~~BST MENU~~~~

87
1.Create a BST
2.BST Traversals:
3.Search
4.Exit
Enter your choice: 3
Enter Element to be searched: 89
Key element is present in BST
~~~~BST MENU~~~~
1.Create a BST
2.BST Traversals:
3.Search
4.Exit
Enter your choice: 3
Enter Element to be searched: 68
Key element is not found in the BST
~~~~BST MENU~~~~
1.Create a BST
2.BST Traversals:
3.Search
4.Exit
Enter your choice: 4
Process returned 0 (0x0) execution time : 53.079 s
Press any key to continue.

88
Program-11
Develop a Program in C for the following operations on Graph(G) of Cities
a. Create a Graph of N cities using Adjacency Matrix.
b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS
method

#include<stdio.h>
#include<stdlib.h>
int a[50][50], n, visited[50];
int q[20], front = -1,rear = -1;
int s[20], top = -1, count=0;
void bfs(int v)
{
int i, cur;
visited[v] = 1;
q[++rear] = v;
while(front!=rear)
{
cur = q[++front];
for(i=1;i<=n;i++)
{
if((a[cur][i]==1)&&(visited[i]==0))
{
q[++rear] = i;
visited[i] = 1;
printf("%d ", i);
}
}
}

89
}

void dfs(int v)
{
int i;
visited[v]=1;
s[++top] = v;
for(i=1;i<=n;i++)
{
if(a[v][i] == 1&& visited[i] == 0 )
{
printf("%d ", i);
dfs(i);
}
}
}
int main()
{
int ch, start, i,j;
printf("\nEnter the number of vertices in graph: ");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=1; i<=n; i++)
{
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
}

for(i=1;i<=n;i++)

90
visited[i]=0;
printf("\nEnter the starting vertex: ");
scanf("%d",&start);

printf("\n==>1. BFS: Print all nodes reachable from a given starting node");
printf("\n==>2. DFS: Print all nodes reachable from a given starting node");
printf("\n==>3:Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nNodes reachable from starting vertex %d are: ", start);
bfs(start);
for(i=1;i<=n;i++)
{
if(visited[i]==0)
printf("\nThe vertex that is not reachable is %d" ,i);
}
break;

case 2: printf("\nNodes reachable from starting vertex %d are:\n",start);


dfs(start);
break;
case 3: exit(0);
default: printf("\n Please enter valid choice:");
}
}

91
Output:
Enter the number of vertices in graph: 4
Enter the adjacency matrix:
0010
0000
1010
0101
Enter the starting vertex: 1
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Enter your choice: 1
Nodes reachable from starting vertex 1 are: 3
The vertex that is not reachable is 2
The vertex that is not reachable is 4

Enter the number of vertices in graph: 4


Enter the adjacency matrix:
0010
0000
1010
0101
Enter the starting vertex: 1
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Enter your choice: 2
Nodes reachable from starting vertex 1 are:
3

92
Enter the number of vertices in graph: 6
Enter the adjacency matrix:
1011
0111
0010
0001
1001
0011
1000
0111
1000
Enter the starting vertex: 6
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Enter your choice: 1
Nodes reachable from starting vertex 6 are: 1 2 3 4 5

93
Program-12
Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine
the records in file F.
Assume that file F is maintained in memory by a Hash Table (HT) of m memory locations
with L as the set of memory addresses (2-digit) of locations in HT.
Let the keys in K and addresses in L are Integers. Develop a Program in C that uses Hash
function H: K →L as H(K)=K mod m (remainder method), and implement hashing
technique to map a given key K to the address space L. Resolve the collision (if any) using
linear probing.

#include<stdio.h>
#include<stdlib.h>
int key[20],n,m;
int *ht,index;
int count = 0;

void insert(int key)


{
index = key % m;
while(ht[index] != -1)
{
index = (index+1)%m;
}
ht[index] = key;
count++;
}

94
void display()
{
int i;
if(count == 0)
{
printf("\nHash Table is empty");
return;
}
printf("\nHash Table contents are:\n ");
for(i=0; i<m; i++)
printf("\n T[%d] --> %d ", i, ht[i]);
}
int main()
{
int i;
printf("\nEnter the number of employee records (N) : ");
scanf("%d", &n);
printf("\nEnter the two digit memory locations (m) for hash table: ");
scanf("%d", &m);
ht = (int *)malloc(m*sizeof(int));
for(i=0; i<m; i++)
ht[i] = -1;
printf("\nEnter the four digit key values (K) for N Employee Records:\n ");
for(i=0; i<n; i++)
scanf("%d", &key[i]);
for(i=0;i<n;i++)
{
if(count == m)
{

95
printf("\n~~~Hash table is full. Cannot insert the record %d key~~~",i+1);
break;
}
insert(key[i]);
}
display();
}

96
Output:
Enter the number of employee records (N) : 5
Enter the two digit memory locations (m) for hash table: 2
Enter the four digit key values (K) for N Employee Records:
4587
4568
7894
1235
4687
~~~Hash table is full. Cannot insert the record 3 key~~~
Hash Table contents are:
T[0] --> 4568
T[1] --> 4587
Enter the number of employee records (N) : 5
Enter the two digit memory locations (m) for hash table: 05
Enter the four digit key values (K) for N Employee Records:
4789
4568
1254
6325
7894
Hash Table contents are:
T[0] --> 1254
T[1] --> 6325
T[2] --> 7894
T[3] --> 4568
T[4] --> 4789
Process returned 0 (0x0) execution time : 13.739 s
Press any key to continue.

97
Sample Viva Questions:
1. What is a data structure?
A data structure is a way of organizing and storing data to perform operations efficiently.
2. Differentiate between an array and a linked list.
An array is a fixed-size, contiguous memory structure, while a linked list is a dynamic data
structure where elements are linked using pointers.
3. Explain the concept of time complexity.
Time complexity represents the amount of time an algorithm takes to complete as a function of
the input size.
4. What is the difference between a stack and a queue?
A stack follows the Last In, First Out (LIFO) principle, while a queue follows the First In, First
Out (FIFO) principle.
5. How does a binary search work?
Binary search is a divide-and-conquer algorithm that efficiently finds the position of a target
value within a sorted array.
6. What is recursion, and how is it used in data structures?
Recursion is a programming technique where a function calls itself. It is often used in data
structures like trees and graphs.
7. Explain the concept of a hash table.
A hash table is a data structure that maps keys to values using a hash function, allowing for
efficient retrieval and storage of data.
8. What is the difference between a linked list and an array?
Arrays have a fixed size, and their elements are stored in contiguous memory locations, while
linked lists can dynamically change in size and store elements with non-contiguous memory.
9. Define a tree in terms of data structures.
A tree is a hierarchical data structure composed of nodes, each having a value and zero or more
child nodes.
10. Explain the process of tree traversal.
Tree traversal involves visiting all the nodes of a tree in a specific order. Common methods are
in-order, pre-order, and post-order traversals.
11. What is a priority queue?
A priority queue is a data structure that stores elements with associated priorities and allows for
efficient retrieval of the element with the highest priority.
12. Describe the concept of a graph.
A graph is a collection of nodes (vertices) and edges that connect pairs of nodes. It can be
directed or undirected.

98
13. What is dynamic programming, and how is it related to data structures?
Dynamic programming is a technique for solving complex problems by breaking them down into
simpler overlapping subproblems. Data structures like arrays or tables are often used to store
solutions to subproblems to avoid redundant computations.
14. What is the purpose of a doubly linked list?
In a doubly linked list, each node contains a data element and two pointers, one pointing to the
next node and another pointing to the previous node. This allows for easy traversal in both
directions.
15. How does a bubble sort algorithm work?
Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order.
16. What is a stack?
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle, where the
last element added is the first one to be removed.
17. What are main operations of a stack.
The main operations are:
Push: Adds an element to the top of the stack.
Pop: Removes the element from the top of the stack.
Peek (or Top): Returns the element at the top of the stack without removing it.
18. How is a stack implemented?
A stack can be implemented using an array or a linked list. In the array implementation, a
fixed-size array is used to store elements, and a variable keeps track of the top of the stack.
19. What is the significance of the top of the stack?
The top of the stack is the position where the next push or pop operation will occur. It keeps
track of the most recently added element.
20. Explain the concept of stack overflow.
Stack overflow occurs when the stack reaches its maximum capacity, and a push operation tries
to add an element beyond the available space, leading to a runtime error.
21. How is stack different from a queue?
A stack follows the Last In, First Out (LIFO) principle, while a queue follows the First In, First
Out (FIFO) principle.
22. What is the purpose of a call stack in programming?
The call stack is used to manage function calls in a program. When a function is called, its local
variables and the return address are pushed onto the stack. When the function completes, its stack
frame is popped.

99
23. Can you give an example of a real-world scenario where a stack is useful?
One example is the Undo feature in software applications. Each action is pushed onto a stack,
and the Undo operation pops the last action to revert the changes.
24. How does a stack help in reversing a string?
By pushing each character of the string onto the stack and then popping them in reverse order,
the string can be effectively reversed.
25. Explain the use of parentheses matching using a stack.
A stack can be used to check if parentheses in an expression are balanced. When an opening
parenthesis is encountered, it is pushed onto the stack. When a closing parenthesis is encountered,
it is checked against the top of the stack.
26. What is the significance of the stack in recursive function calls?
The call stack is used to manage the execution of recursive functions by keeping track of each
function call's local variables and return addresses.
27. How is an expression evaluation carried out using a stack?
In expression evaluation, a stack can be used to store operands and operators. The stack helps in
maintaining the order of operations and ensures proper evaluation.
28. Explain the concept of a stack frame.
A stack frame is a block of memory on the call stack that stores local variables, function
parameters, and the return address for a specific function call.
29. Can a stack be implemented using queues? How?
Yes, a stack can be implemented using two queues. Push operation is simulated by enqueueing
elements into one queue, and pop operation is simulated by dequeuing elements from the other
queue.
30. What is the role of the stack in managing recursive algorithms?
The stack is crucial in managing recursive algorithms as it keeps track of the function calls and
their associated local variables, allowing for proper execution and backtracking.
31. What is a queue?
A queue is a linear data structure that follows the First In, First Out (FIFO) principle, where the
first element added is the first one to be removed.
32. Explain the main operations of a queue.
The main operations are:
Enqueue: Adds an element to the rear (end) of the queue.
Dequeue: Removes the element from the front (beginning) of the queue.
Peek (or Front): Returns the element at the front of the queue without removing it.

100
33. How is a queue implemented?
A queue can be implemented using an array or a linked list. In the array implementation, a fixed-
size array is used to store elements, and two pointers (front and rear) keep track of the queue's
boundaries.
34. Differentiate between a queue and a stack.
A queue follows the First In, First Out (FIFO) principle, while a stack follows the Last In, First
Out (LIFO) principle.
35. Explain the concept of circular queues.
In a circular queue, the front and rear pointers wrap around to the beginning of the queue when
they reach the end, forming a circle. This allows for better space utilization.
36. What is the significance of the front and rear pointers in a queue?
The front pointer points to the first element in the queue, and the rear pointer points to the last
element. They help in maintaining the order of elements and perform enqueue and dequeue
operations efficiently.
37. What is a priority queue? How is it different from a regular queue?
A priority queue is a variation of a queue where each element has an associated priority. Elements
with higher priority are dequeued before elements with lower priority.
38. Explain the concept of double-ended queues (deque).
A deque is a queue that allows insertion and deletion at both the front and rear ends. It supports
the operations of both stacks and queues.
38. How can a queue be implemented using two stacks?
Two stacks can be used to implement a queue by using one stack for enqueue operations and the
other for dequeue operations.
39. Explain the concept of a priority queue with a real-world example.
A real-world example of a priority queue is the scheduling of tasks in an operating system, where
tasks with higher priority are scheduled to run before those with lower priority.
40. How can a queue be implemented using a linked list?
In a linked list implementation of a queue, each node contains data and a pointer to the next node.
Enqueue and dequeue operations involve adding and removing nodes, respectively, at the front
and rear of the linked list.
41. What is a linked list?
A linked list is a data structure that consists of a sequence of elements, where each element points
to the next one in the sequence.
42. Explain the advantages of a linked list over an array.
Linked lists can dynamically change in size, require less memory allocation, and support efficient
insertions and deletions compared to arrays.

101
43. What are the types of linked lists?
Types of linked lists include singly linked lists, doubly linked lists, and circular
linked lists. Differentiate between a singly linked list and a doubly linked list.
In a singly linked list, each node points to the next node, while in a doubly
linked list, each node points to both the next and the previous nodes.
44. How is a circular linked list different from a linear linked list?
In a circular linked list, the last node points back to the first node, forming a
circle, while a linear linked list has a distinct end.
45. Explain the concept of a node in a linked list.
A node is a fundamental building block of a linked list, containing data and a
reference (or link) to the next node (and, in the case of a doubly linked list, to the
previous node).
46. What is the significance of a head pointer in a linked list?
The head pointer points to the first node in the linked list, serving as the entry
point for accessing and traversing the list.
47. Explain the process of inserting a node in the middle of a linked list.
To insert a node in the middle, the pointers of the preceding node are adjusted to
point to the new node, and the new node is linked to the next node.
48. How is a node removed from a linked list?
To remove a node, the links of the preceding node are adjusted to bypass the
node to be removed, and the memory for the removed node is deallocated.
49. What is a NULL pointer, and how is it used in linked lists?
A NULL pointer is a pointer that does not point to any memory location. In
linked lists, it is used to signify the end of the list, as the last node's next (or
previous) pointer points to NULL.
50. Explain the concept of a tail pointer in a linked list.
The tail pointer points to the last node in a linked list, making it faster to perform
operations at the end of the list, such as insertions and deletions.
51. How does a linked list facilitate dynamic memory allocation?
Linked lists allow for dynamic memory allocation because nodes can be added
or removed without the need for contiguous memory blocks.
52. How can a linked list be reversed?
To reverse a linked list, each node's next pointer is adjusted to point to the
preceding node iteratively or recursively.

102

You might also like