Ilovepdf Merged
Ilovepdf Merged
a) Program code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Day {
};
int main() {
int i;
1
PROGRAM -1
switch (i) {
case 0:
calendar[i]->name = strdup("Monday");
calendar[i]->date = 1;
calendar[i]->activity = strdup("Work");
break;
case 1:
calendar[i]->name = strdup("Tuesday");
calendar[i]->date = 2;
calendar[i]->activity = strdup("Meeting");
break;
case 2:
calendar[i]->name = strdup("Wednesday");
calendar[i]->date = 3;
calendar[i]->activity = strdup("Gym");
break;
case 3:
calendar[i]->name = strdup("Thursday");
calendar[i]->date = 4;
calendar[i]->activity = strdup("Study");
break;
case 4:
2
PROGRAM -1
calendar[i]->name = strdup("Friday");
calendar[i]->date = 5;
break;
case 5:
calendar[i]->name = strdup("Saturday");
calendar[i]->date = 6;
break;
case 6:
calendar[i]->name = strdup("Sunday");
calendar[i]->date = 7;
calendar[i]->activity = strdup("Relax");
break;
printf("Calendar:\n");
3
PROGRAM -1
free(calendar[i]->name);
free(calendar[i]->activity);
free(calendar[i]);
return 0;
Expected Output:
Calendar:
4
PROGRAM -1
Program Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Day {
};
int i;
calendar[i]->activity = NULL;
5
PROGRAM -1
*calendar[]) {
int i;
char temp[100];
scanf("%s", temp);
calendar[i]->name = strdup(temp);
scanf("%d", &calendar[i]->date);
scanf("%s", temp);
calendar[i]->activity = strdup(temp);
int i;
printf("Calendar:\n");
int main() {
create(calendar);
read(calendar);
display(calendar);
free(calendar[i]->name);
free(calendar[i]->activity);
free(calendar[i]);
return 0;
7
PROGRAM -1
Expected Output:
8
PROGRAM -1
Calendar:
9
PROGRAM-
1
PROGRAM-
{
i++;
m++;
if (PAT[i] == '\0')
{
for (k = 0; REP[k] != '\0'; k++, j++)
{
ANS[j] = REP[k];
}
i = 0;
c = m;
flag = 1;
}
}
else
{
ANS[j] = STR[c];
j++;
c++;
m = c;
i = 0;
}
}
if (flag == 0)
printf("Pattern not found!!!\n");
else
{
2
PROGRAM-
ANS[j] = '\0';
printf("\nThe RESULTANT string is:\n%s\n", ANS);
}
}
int main()
{
read();
replace();
return 0;
}
Sample Output 1
Enter the MAIN string:
good morning
Enter a PATTERN string: morning
Enter a REPLACE string: Evening
The RESULTANT string is: good evening
Sample Output 2
Enter the MAIN string: hi vcet
Enter a PATTERN string: bye
Enter a REPLACE string: hello
Pattern doesn't found!!
3
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
Program :-
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int s[MAX];
int top = -1;
void main()
{
int choice, item;
while(1)
{
printf("\n\n\n\n~~~~~~Menu~~~~~~ : ");
printf("\n=>1.Push an Element to Stack and Overflow demo ");
printf("\n=>2.Pop an Element from Stack and Underflow demo");
printf("\n=>3.Palindrome demo ");
printf("\n=>4.Display ");
printf("\n=>5.Exit"); printf("\
nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
1
Program
case 1: printf("\nEnter an element to be pushed:
");
scanf("%d", &item);
push(item);
break;
case 2: item = pop();
if(item != -1)
printf("\nElement popped is: %d",
item);
break;
case 3: palindrome();
break;
case 4: display();
break;
case 5: exit(1);
default: printf("\nPlease enter valid choice ") ;
break;
}
}
}
top = top + 1 ;
s[top] = item;
}
int pop()
{
int item;
if(top == -1)
{
printf("\n~~~~Stack underflow~~~~");
return -1;
}
item = s[top];
top = top - 1;
2
Program
return item;
}
void display()
{
int i;
if(top == -1)
{
printf("\n~~~~Stack is empty~~~~");
return;
}
printf("\nStack elements are:\n ");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);
}
void palindrome()
{
int flag=1,i;
printf("\nStack content are:\n");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);
3
Program
}
Output:
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 11
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 12
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 13
~~~~~~Menu~~~~~~ :
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 14
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow
4
Program
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 15
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 16
~~~~Stack overflow~~~~
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 4
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 15
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow
5
Program
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 4
Stack elements are:
| 14 |
| 13 |
| 12 |
| 11 |
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 14
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 13
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 12
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Displ
ay
6
Program
Enter your choice: 2
Element popped is: 11
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
~~~~Stack underflow~~~~
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 4
~~~~Stack is empty~~~~
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 11
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 22
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
7
Program
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 11
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 3
Stack content are:
| 11 |
| 22 |
| 11 |
It is palindrome number
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 11
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 22
~~~~~~Menu~~~~~
8
Program
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 2
Element popped is: 11
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 11
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 22
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 1
Enter an element to be pushed: 33
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 3
Stack content are:
9
Program
| 33 |
| 22 |
| 11 |
~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo
=>2.Pop an Element from Stack and Underflow demo
=>3.Palindrome demo
=>4.Display
=>5.Exit
Enter your choice: 5
1
Program
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.
Algorithm:
Step 1: Start.
Step 4: Stop
Program:-
#include <ctype.h>
#include <stdio.h>
char s[SIZE];
s[++top] = elem;
return (s[top--]);
1
Program
int pr(char elem) /* Function for precedence */
switch (elem)
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
case '%':
return 3;
case '^':
return 4;
int i = 0, k = 0;
2
Program
scanf("%s", infx);
push('#');
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
pofx[k++] = pop();
else /* Operator */
pofx[k++] = pop();
push(ch);
pofx[k++] = pop();
3
Program
printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);
Output 1:
Output 2:
4
Program -
5. Develop a Program in C for the following Stack Applications
Algorithm:
Step 1: Start.
Step 2: Read the postfix/suffix expression.
Step 3: Evaluate the postfix expression based on the precedence of the operator.
Step 4: Stop.
Program code:
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
int main()
{
double s[20],res,op1,op2;
int top=-1,i;
char postfix[20],symbol;
printf("Enter the postfix expression : \n");
1
Program -
gets(postfix);
for(i=0;i<strlen(postfix); i++)
{
symbol=postfix[i];
if(isdigit(symbol))
{
s[++top]=symbol-'0';
}
else
{
op2=s[top--];
op1=s[top--];
res= compute(symbol,op1,op2);
s[++top]=res;
}
}
res=s[top--];
printf("\n The result is : %f\n",res);
return 0;
}
Output 1:
Insert a postfix notation: 22^32*+
Result: 10
Output 2:
Insert a postfix notation: 45^98*+
Result: 1096
Output 3:
Insert a postfix notation: 23+
Result: 5
Output 4:
Insert a postfix notation: 13-
Result:
2
Program -
5b. Solving Tower of Hanoi problem with n disks.
Algorithm:
Step 1: Start.
Step 2: Read N number of discs.
Step 3: Move all the discs from source to destination by using temp rod.
Step 4: Stop.
Program code:
#include<stdio.h>
#include<math.h>
int main()
{
int n;
printf("Enter the number of discs : \n ");
scanf("%d",&n);
tower(n,'A','B','C');
printf("Total number of disc moves are %d ", (int)pow(2,n)-1);
return 0;
}
Output 1:
Enter the number of disks: 2
Move disk 1 from peg A to peg B
Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C
Total numbers of disc moves are 3
3
Program -
Output 2:
Enter the number of disks: 3
Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C
Total numbers of disc moves are 7
Output 3:
Enter the number of disks: 4
Move disk 1 from peg A to peg B
Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C
Move disk 3 from peg A to peg B
Move disk 1 from peg C to peg A
Move disk 2 from peg C to peg B
Move disk 1 from peg A to peg B
Move disk 4 from peg A to peg C
Move disk 1 from peg B to peg C
Move disk 2 from peg B to peg A
Move disk 1 from peg C to peg A
Move disk 3 from peg B to peg C
Move disk 1 from peg A to peg B
Move disk 2 from peg A to peg C
Move disk 1 from peg B to peg C
Total numbers of disc moves are 15
4
Program
e. Exit
Support the program with appropriate functions for each of the above operations
Algorithm:-
Step 1: Start.
Step 3: Insert the elements into circular queue. If queue is full give a message as
“queue is overflow”
Step 4: Delete an element from the circular queue. If queue is empty give a
message as “queue is underflow”
Step 6: Stop.
Program:-
#include <stdio.h>
#include<stdlib.h>
#include<stdio_ext.h>
#define MAX 3
1
Program
char cq[MAX];
void insert(char);
void delete();
void display();
void main()
int ch;
char item;
while(1)
printf("\n\n~~Main Menu~~");
printf("\n==> 3. Display");
scanf("%d", &ch);
fpurge(stdin);
switch(ch)
2
Program
scanf("%c", &item);
insert(item);
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
if(front == (rear+1)%MAX)
else
if(front == -1)
front = rear = 0;
else
3
Program
rear = (rear+1)%MAX;
cq[rear] = item;
void delete()
char item;
if(front == -1)
else
item = cq[front];
else
front = (front+1)%MAX;
}
4
Program
void display ()
int i ;
if(front == -1)
else
5
Program
Output:
~~Main Menu~~
==> 3. Display
==> 4. Exit
~~Main Menu~~
==> 3. Display
==> 4. Exit
~~Main Menu~~
==> 3. Display
==> 4. Exit
~~Main Menu~~
6
Program
==> 1. Insertion and Overflow Demo
==> 3. Display
==> 4. Exit
~~Main Menu~~
==> 3. Display
==> 4. Exit
Front[0]-> A B C <-[2]Rear
~~Main Menu~~
==> 3. Display
==> 4. Exit
7
Program
~~Main Menu~~
==> 3. Display
==> 4. Exit
Front[1]-> B C <-[2]Rear
~~Main Menu~~
==> 3. Display
==> 4. Exit
~~Main Menu~~
==> 3. Display
==> 4. Exit
8
Program
Circular Queue contents are:
Front[2]-> C <-[2]Rear
~~Main Menu~~
==> 3. Display
==> 4. Exit
~~Main Menu~~
==> 3. Display
==> 4. Exit
~~Main Menu~~
==> 3. Display
==> 4. Exit
9
Program
Enter Your Choice: 4
1
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, Ph.No
e. Exit
Algorithm:-
Step 1: Start.
Step 10: Demonstrate how singly linked list can be used as stack.
Step 11: Demonstrate how singly linked list can be used as queue.
1
Program
Program:-
#include<stdio.h>
#include<stdlib.h>
struct node
char usn[25],name[25],branch[25];
int sem;
};
int count=0;
NODE create()
NODE snode;
if(snode == NULL)
exit(1);
2
Program
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)
return NULL;
if(start->link == NULL)
3
Program
{
count--;
free(start);
return NULL;
temp = start;
start = start->link;
count--;
free(temp);
return start;
NODE insertend()
NODE cur,temp;
temp = create();
if(start == NULL)
return temp;
cur = start;
while(cur->link !=NULL)
4
Program
cur = cur->link;
cur->link = temp;
return start;
NODE deleteend()
NODE cur,prev;
if(start == NULL)
return NULL;
if(start->link == NULL)
free(start);
count--;
return NULL;
prev = NULL;
cur = start;
while(cur->link!=NULL)
5
Program
prev = cur;
cur = cur->link;
free(cur);
prev->link = NULL;
count--;
return start;
void display()
NODE cur;
int num=1;
if(start == NULL)
return;
cur = start;
while(cur!=NULL)
cur = cur->link;
6
Program
num++;
void stackdemo()
int ch;
while(1)
scanf("%d",&ch);
switch(ch)
break;
break;
case 3: display();
break;
default : return;
7
Program
return;
int main()
int ch,i,n;
while(1)
printf("\n~~~Menu~~~");
printf("\n2:DisplayStatus"); printf("\
n3:InsertAtEnd"); printf("\n4:DeleteAtEnd");
printf("\n6:Exit \n");
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
for(i=1;i<=n;i++)
start = insertfront();
break;
8
Program
case 2: display();
break;
break;
break;
case 5: stackdemo();
break;
case 6: exit(0);
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)
9
Program
6:Exit
Enter your choice:1
Enter the no of students: 3
Enter the usn,Name,Branch, sem,PhoneNo of the student:
111
aaa
cs
1
111111
Enter the usn,Name,Branch, sem,PhoneNo of the student:
222
bbb
ec
2
222222
Enter the usn,Name,Branch, sem,PhoneNo of the student:
333
ccc
ec
3
333333
~~~Menu~~~
1
Program
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:333| Name:ccc| Branch:ec| Sem:3| Ph:333333|
||2|| USN:222| Name:bbb| Branch:ec| Sem:2| Ph:222222|
||3|| USN:111| Name:aaa| Branch:cs| Sem:1| Ph:111111|
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
Enter your choice:3
1
Program
Enter the usn,Name,Branch, sem,PhoneNo of the student:
444
ddd
ec
4
444444
~~~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:333| Name:ccc| Branch:ec| Sem:3| Ph:333333|
||2|| USN:222| Name:bbb| Branch:ec| Sem:2| Ph:222222|
||3|| USN:111| Name:aaa| Branch:cs| Sem:1| Ph:111111|
||4|| USN:444| Name:ddd| Branch:ec| Sem:4| Ph:444444|
No of student nodes is 4
~~~Menu~~~
1
Program
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:4
The student node with the usn: 444 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:333| Name:ccc| Branch:ec| Sem:3| Ph:333333|
||2|| USN:222| Name:bbb| Branch:ec| Sem:2| Ph:222222|
||3|| USN:111| Name:aaa| Branch:cs| Sem:1| Ph:111111|
1
Program
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
Enter your choice:4
The student node with the usn: 111 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:5
~~~Stack Demo using SLL~~~
1:Push operation
1
Program
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo: 1
Enter the usn,Name,Branch, sem,PhoneNo of the student:
555
eee
cs
1
555555
~~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo:3
The contents of SLL:
||1|| USN:555| Name:eee| Branch:cs| Sem:1| Ph:555555|
||2|| USN:333| Name:ccc| Branch:ec| Sem:3| Ph:333333|
||3|| USN:222| Name:bbb| Branch:ec| Sem:2| Ph:222222|
No of student nodes is 3
~~~Stack Demo using SLL~~~
1
Program
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo: 1
Enter the usn,Name,Branch, sem,PhoneNo of the student:
666
fff
cs
6
666666
~~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo: 3
The contents of SLL:
||1|| USN:666| Name:fff| Branch:cs| Sem:6| Ph:666666|
||2|| USN:555| Name:eee| Branch:cs| Sem:1| Ph:555555|
||3|| USN:333| Name:ccc| Branch:ec| Sem:3| Ph:333333|
||4|| USN:222| Name:bbb| Branch:ec| Sem:2| Ph:222222|
1
Program
No of student nodes is 4
~~~Stack Demo using SLL~~~
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo: 2
The Student node with usn: 666 is deleted
1
Program
1:Push operation
2: Pop operation
3: Display
4:Exit
Enter your choice for stack demo: 4
~~~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
1
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, Ph.No
f. Exit
Algorithm:-
Step 1: Start.
Step 10: Demonstrate how doubly linked list can be used as double ended
queue.
Program:-
#include<stdio.h>
#include<stdlib.h>
1
Program
struct node
char ssn[25],name[25],dept[10],designation[25];
int sal;
};
int count=0;
NODE create()
NODE enode;
exit(0);
2
Program
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;
cur = first;
if(cur == NULL)
3
Program
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++;
NODE deletefront()
NODE temp;
if(first == NULL)
return NULL;
if(first->rlink== NULL)
free(first);
count--;
return NULL;
4
Program
}
temp = first;
first = first->rlink;
temp->rlink = NULL;
first->llink = NULL;
free(temp);
count--;
return first;
NODE insertend()
temp = create();
if(first == NULL)
return temp;
cur= first;
while(cur->rlink!=NULL)
cur = cur->rlink;
5
Program
cur->rlink = temp;
temp->llink = cur;
return first;
NODE deleteend()
NODE prev,cur;
if(first == NULL)
return NULL;
if(first->rlink == NULL)
free(first);
count--;
return NULL;
prev=NULL;
cur=first;
while(cur->rlink!=NULL)
6
Program
prev=cur;
cur = cur->rlink;
cur->llink = NULL;
free(cur);
prev->rlink = NULL;
count--;
return first;
void deqdemo()
int ch;
while(1)
scanf("%d", &ch);
switch(ch)
case 1: first=insertfront();
break;
case 2: first=deletefront();
break;
7
Program
case 3: first=insertend();
break;
case 4: first=deleteend();
break;
case 5: display();
break;
default : return;
void main()
int ch,i,n;
while(1)
printf("\n\n~~~Menu~~~");
printf("\n2:DisplayStatus"); printf("\
n3:InsertAtEnd"); printf("\n4:DeleteAtEnd");
printf("\n5:InsertAtFront"); printf("\
n6:DeleteAtFront");
printf("\n8:Exit \n");
8
Program
printf("\nPlease enter your choice: ");
scanf("%d",&ch);
switch(ch)
scanf("%d",&n);
for(i=1;i<=n;i++)
first = insertend();
break;
case 2: display();
break;
break;
break;
break;
break;
case 7: deqdemo();
break;
case 8 : exit(0);
9
Program
}
Output:-
~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
111
aaa
dept1
des1
1000
11111
1
Program
Enter the ssn,Name,Department,Designation,Salary,PhoneNo of the employee:
222
bbb
dept2
des2
2000
22222
~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
ENode:1||SSN:111|Name:aaa|Department:dept1|Designation:des1|Salary:1000|P
hone no:11111
ENode:2||SSN:222|Name:bbb|Department:dept2|Designation:des2|Salary:2000|
Phone no:22222
No of employee nodes is 2
~~~Menu~~~
1
Program
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
333
ccc
dept3
des3
3000
33333
~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
1
Program
Please enter your choice: 2
ENode:1||SSN:111|Name:aaa|Department:dept1|Designation:des1|Salary:1000|P
hone no:11111
ENode:2||SSN:222|Name:bbb|Department:dept2|Designation:des2|Salary:2000|
Phone no:22222
ENode:3||SSN:333|Name:ccc|Department:dept3|Designation:des3|Salary:3000|P
hone no:33333
No of employee nodes is 3
~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
444
ddd
dept4
des4
4000
44444
1
Program
~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
ENode:1||SSN:444|Name:ddd|Department:dept4|Designation:des4|Salary:4000|
Phone no:44444
ENode:2||SSN:111|Name:aaa|Department:dept1|Designation:des1|Salary:1000|P
hone no:11111
ENode:3||SSN:222|Name:bbb|Department:dept2|Designation:des2|Salary:2000|
Phone no:22222
ENode:4||SSN:333|Name:ccc|Department:dept3|Designation:des3|Salary:3000|P
hone no:33333
No of employee nodes is 4
~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
1
Program
6:DeleteAtFront
8:Exit
~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
1
Program
8:Exit
ENode:1||SSN:111|Name:aaa|Department:dept1|Designation:des1|Salary:1000|P
hone no:11111
ENode:2||SSN:222|Name:bbb|Department:dept2|Designation:des2|Salary:2000|
Phone no:22222
No of employee nodes is 2
~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
1
Program
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
4:DeleteQueueRear
5:DisplayStatus
6: Exit
1:InsertQueueFront
2: DeleteQueueFront
3:InsertQueueRear
1
Program
4:DeleteQueueRear
5:DisplayStatus
6: Exit
~~~Menu~~~
2:DisplayStatus
3:InsertAtEnd
4:DeleteAtEnd
5:InsertAtFront
6:DeleteAtFront
8:Exit
1
Program
9. Develop a Program in C for the following operations on Singly Circular
Linked List (SCLL)
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
Algorithm:-
Step 1: Start.
Step 4: Read two polynomials and find the sum of the polynomials.
Step 5: Stop
Program:-
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node
int coef;
1
Program
};
NODE getnode()
NODE x;
if(x == NULL)
return NULL;
return x;
NODE attach(int coef, int xexp, int yexp, int zexp, NODE head)
temp = getnode();
temp->coef = coef;
temp->xexp = xexp;
temp->yexp = yexp;
temp->zexp = zexp;
cur = head->link;
while(cur->link != head)
2
Program
cur = cur->link;
cur->link = temp;
temp->link = head;
return head;
scanf("%d", &n);
printf("\n\t\tCoef = ");
scanf("%d", &coef);
scanf("%d", &xexp);
scanf("%d", &yexp);
scanf("%d", &zexp);
return head;
3
Program
void display(NODE head)
NODE temp;
if(head->link == head)
return;
temp = head->link;
while(temp != head)
temp = temp->link;
if(temp != head)
printf(" + ");
int x, y, z, sum = 0;
NODE poly;
4
Program
scanf("%d %d %d", &x, &y, &z);
poly = head->link;
while(poly != head)
poly = poly->link;
return sum;
NODE a, b;
int coef;
a = head1->link;
b = head2->link;
while(1)
5
Program
head3 = attach(coef, a->xexp, a->yexp, a->zexp, head3);
a = a->link;
b = b->link;
break;
if(a->xexp!=0 || b->xexp!=0)
switch(COMPARE(a->xexp, b->xexp))
b = b->link;
break;
a = a->link;
break;
6
Program
b = b->link;
break;
a = a->link;
break;
b = b->link;
break;
a = a->link;
break;
break;
if(a->yexp!=0 || b->yexp!=0)
7
Program
{
switch(COMPARE(a->yexp, b->yexp))
b = b->link;
break;
break;
b = b->link;
break;
a = a->link;
break;
8
Program
break;
if(a->zexp!=0 || b->zexp!=0)
switch(COMPARE(a->zexp,b->zexp))
b = b->link;
break;
a = a->link;
break;
break;
while(a!= head1)
head3 = attach(a->coef,a->xexp,a->yexp,a->zexp,head3);
a = a->link;
while(b!= head2)
9
Program
{
head3 = attach(b->coef,b->xexp,b->yexp,b->zexp,head3);
b = b->link;
return head3;
void main()
head->link=head;
head1->link=head1;
head2->link=head2;
1
Program
head3->link= head3;
while(1)
printf("\n~~~Menu~~~");
scanf("%d",&ch);
switch(ch)
head = read_poly(head);
display(head);
res = poly_evaluate(head);
break;
1
Program
head3 = poly_sum(head1,head2,head3);
display(head3);
break;
case 3: exit(0);
Output:-
~~~Menu~~~
Coef = 6
1
Program
Enter Pow(x) Pow(y) and Pow(z): 2 2 1
Coef = -4
Coef = 3
Coef = 2
Coef = -2
~~~Menu~~~
1
Program
Coef = 6
Coef = 3
Coef = 5
Coef = 10
Coef = 5
Enter Pow(x) Pow(y) and Pow(z): 0 0 0
Polynomial 1 is:
Coef = 8
Coef = 4
1
Program
Enter the 3 term:
Coef = 30
Coef = 20
Coef = 3
Polynomial 2 is:
~~~Menu~~~
1
Program
10. Develop a menu driven Program in C for the following operations on Binary
Search Tree
(BST) of Integers .
c. Search the BST for a given element (KEY) and report the appropriate
message
d. Exit
Algorithm:-
Step 1: Start.
Step 9: Stop
Program:-
#include<stdio.h>
#include<stdlib.h>
struct BST
int data;
1
Program
};
NODE create()
NODE temp;
scanf("%d", &temp->data);
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
if (root->lchild == NULL)
2
Program
root->lchild = newnode;
else
insert(root->lchild, newnode);
if (root->rchild == NULL)
root->rchild = newnode;
else
insert(root->rchild, newnode);
}
int key;
NODE cur;
if(root == NULL)
printf("\nBST is empty.");
return;
scanf("%d", &key);
cur = root;
3
Program
while (cur != NULL)
if (cur->data == key)
return;
cur = cur->lchild;
else
cur = cur->rchild;
}
if(root != NULL)
inorder(root->lchild);
inorder(root->rchild);
4
Program
void preorder(NODE root)
if (root != NULL)
preorder(root->lchild);
preorder(root->rchild);
if (root != NULL)
postorder(root->lchild);
postorder(root->rchild);
void main()
while(1)
5
Program
printf("\n~~~~BST MENU~~~~");
printf("\n1.Create a BST");
printf("\n2.Search"); printf("\
n4.Exit");
scanf("%d", &ch);
switch(ch)
scanf("%d", &n);
for(i=1;i<=n;i++)
newnode = create();
if (root == NULL)
root = newnode;
else
insert(root, newnode);
break;
else
6
Program
printf("\nThe Preorder display : ");
preorder(root);
inorder(root);
postorder(root);
break;
case 3: search(root);
break;
case 4: exit(0);
Output:-
~~~~BST MENU~~~~
1.Create a BST
2.Search
3. BST Traversals:
4. Exit
Enter your choice: 1
Enter the number of elements: 12
Enter The value: 6
Enter The value: 9
Enter The value: 5
Enter The value: 2
Enter The value: 8
Enter The value: 15
7
Program
Enter The value: 24
Enter The value: 14
Enter The value: 7
Enter The value: 8
Enter The value: 5
Enter The value: 2
~~~~BST MENU~~~~
1.Create a BST
2.Search
3. BST Traversals:
4. Exit
Enter your choice: 3
2 5 7 8 14 24 15 9 6
~~~~BST MENU~~~~
1.Create a BST
2.Search
3. BST Traversals:
4. Exit
~~~~BST MENU~~~~
1.Create a BST
2.Search
8
Program
3. BST Traversals:
4. Exit
~~~~BST MENU~~~~
1.Create a BST
2.Search
3.BST Traversals:
4.Exit
9
Program
11. Develop a Program in C for the following operations on Graph(G) of Cities
b. Print all the nodes reachable from a given starting node in a digraph using
DFS/BFS method.
Algorithm:-
Step 1: Start.
Step 3: Print the nodes reachable from the starting node using BFS.
Step 5: Stop.
Program:-
#include<stdio.h>
#include<stdlib.h>
void bfs(int v)
int i, cur;
visited[v] = 1; q[+
+rear] = v;
while(front!=rear)
cur = q[++front];
1
Program
for(i=1;i<=n;i++)
if((a[cur][i]==1)&&(visited[i]==0))
q[++rear] = i;
visited[i] = 1;
void dfs(int v)
int i;
visited[v]=1; s[+
+top] = v;
for(i=1;i<=n;i++)
dfs(i);
2
Program
}
int main()
scanf("%d",&n);
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for(i=1;i<=n;i++)
visited[i]=0;
scanf("%d",&start);
printf("\n==>3:Exit"); printf("\
scanf("%d", &ch);
switch(ch)
3
Program
{
bfs(start);
for(i=1;i<=n;i++)
if(visited[i]==0)
break;
dfs(start);
break;
case 3: exit(0);
Output:-
Case 1:
4
Program
Enter the adjacency matrix:
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0
~~~Menu~~~~
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Case 2:
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0
~~~Menu~~~~
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
5
Program
Enter the starting vertex: 2
Case 3:
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0
~~~Menu~~~~
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
Case 4:
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0
6
Program
~~~Menu~~~~
==>1. BFS: Print all nodes reachable from a given starting node
==>2. DFS: Print all nodes reachable from a given starting node
==>3:Exit
7
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.
Algorithm:-
Step 1: Start.
Step 2: Given a File of N employee records with a set K of Keys (4-digit) which
uniquely determine the records in file F.
Step 6: Hashing as to map a given key K to the address space L, Resolve the
collision (if any) is using linear probing.
Step 7: Stop.
Program:-
#include<stdio.h>
#include<stdlib.h>
int key[20],n,m;
int *ht,index;
int count = 0;
1
Program
void insert(int key)
index = key % m;
while(ht[index] != -1)
index = (index+1)%m;
ht[index] = key;
count++;
void display()
int i;
if(count == 0)
return;
2
Program
void main()
int i;
scanf("%d", &n);
printf("\nEnter the two digit memory locations (m) for hash table: ");
scanf("%d", &m);
ht = (int *)malloc(m*sizeof(int));
ht[i] = -1;
printf("\nEnter the four digit key values (K) for N Employee Records:\n
");
scanf("%d", &key[i]);
for(i=0;i<n;i++)
if(count == m)
break;
insert(key[i]);
3
Program
}
display();
Output:-
Enter the number of employee records (N) : 12
Enter the two digit memory locations (m) for hash table: 15
Enter the four digit key values (K) of 'N' Employee Records:
1234
5678
3456
2345
6799
1235
7890
3214
3456
1235
5679
2346
T[1] --> -1
4
Program
T[2] --> -1
T[3] --> -1