New

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

strcpy(calendar[4].

activity, "Lab Test");


strcpy(calendar[5].name, "Saturday");
calendar[5].date = 6;
strcpy(calendar[5].activity, "Weekend getaway");
strcpy(calendar[6].name, "Sunday");
calendar[6].date = 7;
strcpy(calendar[6].activity, "Relax and recharge");
// Print the calendar
printf("Calendar for the week:\n");
for (int i = 0; i < 7; i++)
{
printf("%s (Date: %d): %s\n", calendar[i].name, calendar[i].date, calendar[i].activity);
}
return 0;
}
Output:
Calendar for the week:
Monday (Date: 1): College from 9 AM to 4.30 PM
Tuesday (Date: 2): Skill Development Lab,
Wednesday (Date: 3): Placement Training
Thursday (Date: 4): 1 Day Workshop
Friday (Date: 5): Lab Test
Saturday (Date: 6): Weekend getaway
Sunday (Date: 7): Relax and recharge

1. Develop a Program in C for the following:


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>
// Define a structure to represent a day
struct day
{
char name[20];
int date;
7
char activity[100];
};

// Function to create the calendar


void create(struct day calendar[7])
{
for (int i = 0; i < 7; i++)
{
printf("Enter details for %s:\n", calendar[i].name);
printf("Date: ");
scanf("%d", &calendar[i].date);
printf("Activity: ");
scanf(" %s", calendar[i].activity);
}
}

// Function to read data from the keyboard


void read(struct day calendar[7])
{
for (int i = 0; i < 7; i++)
{
printf("Name of calender: %s:\n", calendar[i].name);
printf("Date:%d\n", calendar[i].date);

printf("Activity:%s", calendar[i].activity);
}
}

// Function to display the calendar


void display(struct day calendar[7])
{
printf("Calendar for the week:\n");
for (int i = 0; i < 7; i++)
{
printf("%s (Date: %d): %s\n", calendar[i].name, calendar[i].date, calendar[i].activity);
}
}

int main()
{
struct day calendar[7];
int choice;
// Initialize the names of the days
strcpy(calendar[0].name, "Monday");
strcpy(calendar[1].name, "Tuesday");
strcpy(calendar[2].name, "Wednesday");
strcpy(calendar[3].name, "Thursday");
strcpy(calendar[4].name, "Friday");
strcpy(calendar[5].name, "Saturday");
strcpy(calendar[6].name, "Sunday");
for(;;)
8
{
printf("1. Create Calendar\n");
printf("2. Read Calendar \n");
printf("3. Display calendar\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
create(calendar);
break;
case 2:
read(calendar);
break;
case 3:
display(calendar);
break;
default:
printf("Invalid choice.\n");
return 1;
}
}
return 0;
}

9
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 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<string.h>
#include<stdlib.h>

char str[50], pat[50], rep[50];


int start = 0, patfound = 0;
int lasts, lastp, lastr;

void replacepattern()
{
int i, j;
lastr = strlen(rep)-2;

if(lastp != lastr)
{
printf("\nInvalid length of replace string");
exit(0);
}
else
{
i = start;
for(j=0; j<=lastr; j++)
{
str[i] = rep[j];
i++;
}
}
return;
}
void findpattern()
{
int i, j, inmatch;
lasts = (strlen(str))-2;
lastp = (strlen(pat))-2;
int endmatch;

for(endmatch = lastp; endmatch<=lasts; endmatch++, start++)


{
if(str[endmatch] == pat[lastp])
{
inmatch = start;
j=0;
while(j<lastp)
{
if(str[inmatch] == pat[j])
{
inmatch++;
j++;

10
}
else
{
break;
}
}
if(j == lastp)
{
patfound = 1;
replacepattern();
}
}
}
return;
}
void main()
{
printf("\nEnter the main string(STR): ");
fgets(str, 50, stdin);

printf("\nEnter the pattern to be matched(PAT): ");


fgets(pat, 50, stdin);

printf("\nEnter the string to be replaced(REP): ");


fgets(rep, 50, stdin);

printf("\nThe string before pattern match is:\n %s", str);

findpattern();

if(patfound == 0)
printf("\nThe pattern is not found in the main string");
else
printf("\nThe string after pattern match and replace is: \n %s ",str);
return;
}

Output:
Case 1:
Enter the main string(STR): Hello hii how are you hii
Enter the pattern to be matched(PAT): hii
Enter the string to be replaced(REP): xyz
The string before pattern match is:
Hello hii how are you hii
The string after pattern match and replace is:
Hello xyz how are you xyz

Case 2:
Enter the main string(STR): Hello hii how are you
Enter the pattern to be matched(PAT): abc
Enter the string to be replaced(REP): xyz

The string before pattern match is:


Hello hii how are you

The pattern is not found in the main string


11
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

Theory:
A stack is a container of objects that are inserted and removed according to the last-in first-out(LIFO) principle. A stack is a
limited access data structure - elements can be added and removed from the stack only at the top. Push adds an item to the top
of the stack, pop removes the item from the top.

Basic Operations on Stack


In order to make manipulations in a stack, there are certain operations provided to us.
push() to insert an element into the stack
pop() to remove an element from the stack
top() Returns the top element of the stack.
isEmpty() returns true if stack is empty else false.
isFull() returns true if stack is full else false.

Program

#include<stdio.h>
#include<stdlib.h>
#define MAX 5

int s[MAX];
int top = -1;

void push(int item);


int pop();
void palindrome();
void display();

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)
{
case 1: printf("\nEnter an element to be pushed: ");
scanf("%d", &item);
12
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;
}
}
}

void push(int item)


{
if(top == MAX-1)
{
printf("\n~~~~Stack overflow~~~~");
return;
}

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;
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()

13
{
int flag=1,i;
printf("\nStack content are:\n");
for(i=top; i>=0 ; i--)
printf("| %d |\n", s[i]);

printf("\nReverse of stack content are:\n");


for(i=0; i<=top; i++)
printf("| %d |\n", s[i]);

for(i=0; i<=top/2; i++)


{
if( s[i] != s[top-i] )
{
flag = 0;
break;
}
}
if(flag == 1)
{
printf("\nIt is palindrome number");
}
else
{
printf("\nIt is not a palindrome number");
}
}

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
14
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 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: 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

Stack elements are:


| 15 |
| 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: 15

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo

15
=>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.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
~~~~Stack underflow~~~~

~~~~~~Menu~~~~~

16
=>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
=>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 |

Reverse of stack content are:


| 11 |
| 22 |
| 11 |

It is palindrome number

~~~~~~Menu~~~~~
=>1.Push an Element to Stack and Overflow demo

17
=>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~~~~~
=>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

18
=>4.Display
=>5.Exit
Enter your choice: 3
Stack content are:
| 33 |
| 22 |
| 11 |

Reverse of stack content are:


| 11 |
| 22 |
| 33 |

It is not a 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: 5

19
4. Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should
support for both parenthesized and free parenthesized
a. 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;

void 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++)


{
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 '^' :
20
case '$' : while( prec(stack[top]) >= prec(symb) )
{
temp = pop();
postfix[j] = temp;
j++;
}
push(symb);
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;
}

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 '^' :
21
case '$' : p = 3;
break;
}
return p;
}

Output:

Enter the valid infix expression: (a+b)+c/d*e

The entered infix expression is :


(a+b)+c/d*e

The corresponding postfix expression is :


ab+cd/e*+

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


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

Theory:
a) Evaluation of postfix expression with single digit operands and operators.
For each input symbol,
 If it is a digit then, push it on to the stack.
 If it is an operator then, pop out the top most two contents from the stack and apply the operator on
them. Later on, push the result on to stack.
 If the input symbol is „\0‟, empty the stack.

b) Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one rings. These rings
are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits over the larger one. There are
other variations of the puzzle where the number of disks increase, but the tower count remains the same.
A few rules to be followed for Tower of Hanoi are −
 Only one disk can be moved among the towers at any given time.
 Only the "top" disk can be removed.
22
 No large disk can sit over a small disk.

Program (a):
#include<stdio.h>
#include<math.h>
#include<ctype.h>
char s[50],ch;
int i,a,b,top=-1,stk[50],ans,num,elm;
void push(int num)
{
top=top+1;
stk[top]=num;
}
int pop()
{
elm=stk[top];
top=top-1;
return (elm);
}
void main()
{
printf("Enter the postfix expression\n");
scanf("%s",s);
for(i=0;s[i]!='\0';i++)
{
ch=s[i];
if(isdigit(ch))
push(ch-'0');
else
{
b=pop();
a=pop();
switch(ch)
{
case '+':push(a+b);
break;
case '-':push(a-b);
break;
case '*':push(a*b);
break;
case '/':push(a/b);
break;
case '%':push(a%b);
break;
case '$':push(pow(a,b));
break;
case '^':push(pow(a,b));
break;
default:printf("Invalid operator\n");
}
}
}
ans=pop();
printf("Result of given expression= %d\n",ans);
}

Output:
23
Enter the postfix expression
53+62/*35*+
Result of given expression= 39

Program (b):
#include<stdio.h>
#include<math.h>
int n,x;
void tower(int n,char a,char b,char c)
{
if(n==1)
{
printf("Move disk from %c to %c\n",a,c);
}
tower(n-1,a,c,b);
printf("Move disk from %c to %c\n",a,c);
tower(n-1,b,a,c);
}
void main()
{
printf("Enter number of disks\n");
scanf("%d",&n);
tower(n,'A','B','C');
x=pow(2,n)-1;
printf("Number of moves = %d\n",x);
}

Output:
Enter the number of disks
3
24
Move disk from A to C
Move disk from A to B
Move disk from C to B
Move disk from A to C
Move disk from B to A
Move disk from B to C
Move disk from A to C
Number of moves= 7

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>
#define MAX 5
char cq[MAX];
int front = -1, rear = -1;

void main()
{
int ch;
char item;
while(1)
{
printf("\n\nMain Menu");
printf("\n1. Insertion and Overflow ");
printf("\n 2. Deletion and Underflow ");
printf("\n 3. Display");
printf("\n 4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\n\nEnter the element to be inserted: ");
scanf("%c", &item);
25
insert(item);
break;
case 2: delete();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\n\nPlease enter a valid choice");
}
}
}
void insert(char item)
{
if(front == (rear+1)%MAX)
{
printf("\n\nCircular Queue Overflow");
}
else
{
if(front == -1)
front = rear = 0;
else
rear = (rear+1)%MAX;
cq[rear] = item;
}
}
void delete()
{
char item;
if(front == -1)
{
printf("\n\nCircular Queue Underflow");
}
else
{
item = cq[front];
printf("\n\nDeleted element from the queue is: %c ",item );

if(front == rear) //only one element


front = rear = -1;
else
front = (front+1)%MAX;
}
}
void display ()
{
int i ;
if(front == -1)
{
printf("\n\nCircular Queue Empty");
}
else
{
printf("\nCircular Queue contents are:\n");

for(i = front; i != rear ; i = (i+1)%MAX)


{
26
printf(" %c", cq[i]);
}
printf(" %c", cq[i]);

}
}

Output:
Main Menu
1. Insertion and Overflow
2. Deletion and Underflow
3. Display
4. Exit
Enter Your Choice: 1

Enter the element to be inserted: A

Main Menu
1. Insertion and Overflow
2. Deletion and Underflow
3. Display
4. Exit
Enter Your Choice: 1
Enter the element to be inserted: B

Main Menu
1. Insertion and Overflow
2. Deletion and Underflow
3. Display
4. Exit
Enter Your Choice: 1
Enter the element to be inserted: C

Main Menu
1. Insertion and Overflow
2. Deletion and Underflow
3. Display
4. Exit
Enter Your Choice: 1

Enter the element to be inserted: D


Circular Queue Overflow

Main Menu
1. Insertion and Overflow
2. Deletion and Underflow
3. Display
4. Exit
Enter Your Choice: 3

Circular Queue contents are:


ABC

Main Menu
1. Insertion and Overflow
2. Deletion and Underflow
3. Display
27
4. Exit
Enter Your Choice: 2

Deleted element from the queue is: A

Main Menu
1. Insertion and Overflow
2. Deletion and Underflow
3. Display
4. Exit
Enter Your Choice: 3
Circular Queue contents are:
BC

Main Menu
1. Insertion and Overflow
2. Deletion and Underflow
3. Display
4. Exit
Enter Your Choice: 1

Enter the element to be inserted: E

Main Menu
1. Insertion and Overflow
2. Deletion and Underflow
3. Display
4. Exit
Enter Your Choice: 3

Circular Queue contents are:


BCE

Main Menu
1. Insertion and Overflow
2. Deletion and Underflow
3. Display
4. Exit
Enter Your Choice: 4

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.

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

struct node
{
char usn[25],name[25],branch[25];
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)
{
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;
}
if(start->link == NULL)
29
{
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;
}
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;
}

30
printf("\nThe student node with the usn:%s is deleted",cur->usn);
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);
}

void stackdemo()
{
int ch;
while(1)
{
printf("\nStack Demo using SLL\n");
printf("\n1:Push operation \n2: Pop operation \n3: Display \n4: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()
{
31
int ch,i,n;
while(1)
{
printf("\nMenu");
printf("\nEnter your choice for SLL operation \n");
printf("\n1:Create SLL of Student Nodes");
printf("\n2:DisplayStatus");
printf("\n3:InsertAtEnd");
printf("\n4:DeleteAtEnd");
printf("\n5:Stack Demo using SLL(Insertion and Deletion at Front)");
printf("\n6: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();
break;

case 6: exit(0);

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

}
}
}

Output:

Menu
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:Display Status
3:Insert At End
4:Delete At End
5:Stack Demo using SLL(Insertion and Deletion at Front)
6:Exit
32
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
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:Display Status
3:Insert At End
4:Delete At End
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:Display Status
3:Insert At End
4:Delete At End
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:


444
ddd
33
ec
4
444444

Menu
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:Display Status
3:Insert At End
4:Delete At End
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
Enter your choice for SLL operation
1:Create SLL of Student Nodes
2:Display Status
3:Insert At End
4:Delete At End
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:Display Status
3:Insert At End
4:Delete At End
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
34
2:Display Status
3:Insert At End
4:Delete At End
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:Display Status
3:Insert At End
4:Delete At End
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 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: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
35
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|
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

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: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:Display Status
3:Insert At End
4:Delete At End
36

You might also like