0% found this document useful (0 votes)
15 views27 pages

DS Lab Program EC 2022batch 1 5

The document contains multiple C programs demonstrating various data structures and algorithms, including student record management, stack operations, infix to postfix conversion, queue simulation, and stack implementation using linked lists. Each program includes code snippets, function prototypes, and sample outputs for user interaction. The programs cover dynamic memory allocation, pointer usage, and basic error handling for operations like push, pop, and search.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views27 pages

DS Lab Program EC 2022batch 1 5

The document contains multiple C programs demonstrating various data structures and algorithms, including student record management, stack operations, infix to postfix conversion, queue simulation, and stack implementation using linked lists. Each program includes code snippets, function prototypes, and sample outputs for user interaction. The programs cover dynamic memory allocation, pointer usage, and basic error handling for operations like push, pop, and search.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 27

1.

Write a C Program to create a Student record structure to store, N


records, each record having the structure shown below: USN,
Student Name and Semester. Write necessary functions a. To display
all the records in the file. b. To search for a specific record based on
the USN. In case the record is not found, suitable message should be
displayed. Both the options in this case must be demonstrated. (Use
pointer to structure for dynamic memory allocation)

Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define the structure for student record
struct Student {
char usn[20];
char name[50];
int semester;
};
// Function prototypes
void displayRecords(struct Student *records, int n);
void searchRecord(struct Student *records, int n, char *searchUSN);

int main() {
int n;
printf("Enter the number of records: ");
scanf("%d", &n);

// Dynamically allocate memory for the array of structures


struct Student *records = (struct Student *)malloc(n * sizeof(struct Student));

// Input student records


for (int i = 0; i < n; i++) {
printf("\nEnter details for student %d:\n", i + 1);
printf("USN: ");
scanf("%s", records[i].usn);
printf("Name: ");
scanf("%s", records[i].name); // Assuming no spaces in name
printf("Semester: ");
scanf("%d", &records[i].semester);
}

// Display all records


printf("\nStudent records:\n");
displayRecords(records, n);

// Search for a record


char searchUSN[20];
printf("\nEnter USN to search: ");
scanf("%s", searchUSN);
searchRecord(records, n, searchUSN);

// Free the dynamically allocated memory


free(records);
return 0;
}
void displayRecords(struct Student *records, int n) {
for (int i = 0; i < n; i++) {
printf("\nRecord %d:\n", i + 1);
printf("USN: %s\n", records[i].usn);
printf("Name: %s\n", records[i].name);
printf("Semester: %d\n", records[i].semester);
}
}
void searchRecord(struct Student *records, int n, char *searchUSN) {
int found = 0;
for (int i = 0; i < n; i++) {
if (strcmp(records[i].usn, searchUSN) == 0) {
printf("\nRecord found:\n");
printf("USN: %s\n", records[i].usn);
printf("Name: %s\n", records[i].name);
printf("Semester: %d\n", records[i].semester);
found = 1;
break;
}
}
if (!found) {
printf("\nRecord with USN %s not found\n", searchUSN);
}
}
OUTPUT:
Test case 1:
Enter the number of records: 3

Enter details for student 1:


USN: 1bi22ec001
Name: Ram
Semester: 4

Enter details for student 2:


USN: 1bi22ec002
Name: sita
Semester: 5

Enter details for student 3:


USN: 1bi22ec003
Name: Hanuman
Semester: 6

Student records:

Record 1:
USN: 1bi22ec001
Name: Ram
Semester: 4

Record 2:
USN: 1bi22ec002
Name: sita
Semester: 5
Record 3:
USN: 1bi22ec003
Name: Hanuman
Semester: 6
Enter USN to search: 1bi22ec004
Record with USN 1bi22ec004 not found
Test case 2:
Enter the number of records: 2
Enter details for student 1:
USN: 1bi22ec005
Name: Susheel
Semester: 3
Enter details for student 2:
USN: 1bi22ec006
Name: Rohan
Semester: 2

Student records:

Record 1:
USN: 1bi22ec005
Name: Susheel
Semester: 3

Record 2:
USN: 1bi22ec006
Name: Rohan
Semester: 2

Enter USN to search: 1bi22ec005

Record found:
USN: 1bi22ec005
Name: Susheel
Semester: 3
2. Write a C Program to construct a stack of integers and to perform
the following operations on it: a. Push b. Pop c. Display The program
should print appropriate messages for stack overflow, stack
underflow, and stack empty. Develop a C program to implement
stack of integers to perform push, pop, peek and display operations.

#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");

}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}
}
OUTPUT:
Enter the size of STACK[MAX=100]:10
STACK OPERATIONS USING ARRAY
--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:12

Enter the Choice:1


Enter a value to be pushed:24

Enter the Choice:1


Enter a value to be pushed:98
Enter the Choice:3
The elements in STACK
98
24
12
Press Next Choice
Enter the Choice:2
The popped elements is 98
Enter the Choice:3

The elements in STACK

24
12
Press Next Choice
Enter the Choice:4

EXIT POINT

3. Write a C Program to convert and print a given valid parenthesized


infix arithmetic expression to postfix expression. The expression
consists of single character operands and the binary operators +
(plus), - (minus), * (multiply) and / (divide).

#include<stdio.h>

#include<ctype.h>

char stack[100];

int top = -1;

void push(char x)

stack[++top] = x;

char pop()

if(top == -1)

return -1;

else

return stack[top--];
}

int priority(char x)

if(x == '(')

return 0;

if(x == '+' || x == '-')

return 1;

if(x == '*' || x == '/')

return 2;

return 0;

int main()

char exp[100];

char *e, x;

printf("Enter the expression : ");

scanf("%s",exp);

printf("\n");

e = exp;

while(*e != '\0')
{

if(isalnum(*e))

printf("%c ",*e);

else if(*e == '(')

push(*e);

else if(*e == ')')

while((x = pop()) != '(')

printf("%c ", x);

else

while(priority(stack[top]) >= priority(*e))

printf("%c ",pop());

push(*e);

e++;

while(top != -1)

{
printf("%c ",pop());

}return 0;

OUTPUT:

Test case1:

Enter the expression : a+b*c

abc*+

Test case2:

Enter the expression : (a+b)*c+(d-a)

ab+c*da-+

Test case3:

Enter the expression : ((4+8)(6-5))/((3-2)(2+2))

48+65-32-22+/

4. Write a C Program to simulate the working of a queue of integers


using an array. Provide the following operations: a. Insert b. Delete c.
Display .

#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */

void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */

void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */

void display()
{
int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /* End of display() */
Output :
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 15
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 20
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 30
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Element deleted from queue is : 10
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 3
Queue is :
15 20 30
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 4
5. Write a C Program using dynamic variables and pointers to
construct a stack of integers using singly linked list and to perform
the following operations: a. Push b. Pop c. Display The program
should print appropriate messages for stack overflow and stack
empty.

#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;

int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count = 0;
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}

/* Create empty stack */


void create()
{
top = NULL;
}

/* Count stack elements */


void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}
/* Push data into stack */
void push(int data)
{
if (top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
/* Display stack elements */
void display()
{
top1 = top;

if (top1 == NULL)
{
printf("Stack is empty");
return;
}
while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
/* Pop Operation on stack */
void pop()
{
top1 = top;

if (top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}

/* Return top element */


int topelement()
{
return(top->info);
}

/* Check if stack is empty or not */


void empty()
{
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}

/* Destroy entire stack */


void destroy()
{
top1 = top;

while (top1 != NULL)


{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;

printf("\n All stack elements destroyed");


count = 0;
}
OUTPUT
$ cc pgm2.c
$ a.out
1 - Push
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Dipslay
7 - Stack Count
8 - Destroy stack
Enter choice : 1
Enter data : 56

Enter choice : 1
Enter data : 80

Enter choice : 2

Popped value : 80
Enter choice : 3

Top element : 56
Enter choice : 1
Enter data : 78

Enter choice : 1
Enter data : 90
Enter choice : 6
90 78 56
Enter choice : 7

No. of elements in stack : 3


Enter choice : 8

All stack elements destroyed


Enter choice : 4

Stack is empty
Enter choice : 5

You might also like