Ds 3-7 Lab Programs
Ds 3-7 Lab Programs
Ds 3-7 Lab Programs
Develop a menu driven Program in C for the following operations on STACK of Integers
f. Exit
Support the program with appropriate functions for each of the above operations
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 5
void pop()
{
if (top == -1)
{
printf("Stack underflow\n");
return;
}
void display()
{
int i;
if (top == -1)
{
printf("Stack is empty\n");
return;
}
printf("Stack: ");
for (i = 0; i <= top; i++)
printf("%d ", stack[i]);
printf("\n");
}
printf("%s: Palindrome\n",str);
}
int main()
{
int item, choice;
char str[10];
for (;;)
{
printf("1: Insert 2: Delete 3: Display 4:Palindrome 5: Exit : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter the item: ");
scanf("%d", &item);
push(item);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
printf("Enter the string: ");
scanf(" %[^\n]", str);
palindrome(str);
break;
default:
exit(0);
}
}
}
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 <string.h>
case '+' :
case '-' : return 2;
case '*' :
case '/' :
case '%' : return 4;
case '^' :
case '$' : return 5;
default : return 8;
}
}
case '+' :
case '-' : return 1;
case '*' :
case '/' :
case '%' : return 3;
case '^' :
case '$' : return 6;
default : return 9;
}
}
void infix_2_postfix(char infix[], char postfix[])
{
int i, j = 0, top = -1;
char s[20];
s[++top] = '#';
for (i = 0; infix[i] != '\0'; i++)
{
while ( F( s[top] ) > G( infix[i] ) )
postfix[j++] = s[top--];
if ( F( s[top] ) != G( infix[i] ) )
s[++top] = infix[i];
else
s[top--];
}
postfix[j] = '\0';
}
void main()
{
char infix[20], postfix[20];
printf("Infix :");
scanf("%[^\n]", infix);
infix_2_postfix(infix, postfix);
printf("Postfix:");
printf("%s\n", postfix);
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
return stack[top--];
}
int main() {
char postfix[20];
double result;
result = evaluate(postfix);
return 0;
}
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 5
count++;
}
void delete_front()
{
if (count == 0)
{
printf("Q underFlow\n");
return;
}
count--;
}
void display()
{
int i, temp;
if (count == 0)
{
printf("Q is empty\n");
return;
}
printf("Queue : ");
temp = front;
for (i = 1; i <= count; i++)
{
printf("%d ", queue[temp]);
temp = (temp + 1) % MAX;
}
printf("\n");
}
void main()
{
int item, choice;
for(;;)
{
printf("1: insert 2:Delete 3:Display 4:Exit : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the item : ");
scanf("%d", &item);
insert_rear(item);
break;
case 2:
delete_front();
break;
case 3:
display();
break;
default:
exit(0);
}
}
}
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>
#include<string.h>
struct node
{
char usn[25];
char name[25];
char programme[25];
char phone[12];
int sem;
struct node*link;
};
NODE getnode()
{
NODE x;
if(x == NULL)
{
printf("not enough memory");
exit(0);
}
return x;
}
NODE insert_front(char usn[], char name[], char programme[],char phone[], int sem, NODE first)
{
NODE temp;
temp = getnode();
return temp;
}
NODE insert_rear(char usn[], char name[], char programme[], char phone[], int sem, NODE first)
{
NODE temp, cur;
temp = getnode();
cur = first;
while(cur -> link != NULL)
{
cur=cur -> link;
}
return first;
if(first == NULL)
{
printf("Student list is empty\n");
return NULL;
}
return NULL;
}
cur = first;
while(cur -> link != NULL)
{
prev = cur;
cur = cur -> link;
}
return first;
}
return NULL;
}
return cur;
}
printf("Student list\n");
if (first == NULL)
{
printf( "empty");
return;
}
cur = first;
while(cur != NULL)
{
printf("\n%s", cur -> usn);
printf("%10s", cur -> name);
printf("%10s", cur -> programme);
printf("%10s", cur -> phone);
printf("%10d", cur -> sem);
printf("\n");
}
void main()
{
char usn[25], name[25],programme[25], phone[12];
int choice, sem;
NODE first;
for(;;)
{
printf("\n\n 1:Front insert 2:rear insert\n");
printf("1:Front delete 2:rear delete\n");
printf("5: Display 6:exit\n");
printf("Enter your choice");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("USN: ");
scanf("%s",usn);
printf("Name: ");
scanf("%s", name);
printf("programme: ");
scanf("%s", programme);
printf("phone: ");
scanf("%s", phone);
printf("sem: ");
scanf("%d", &sem);
case 2:
printf("USN : "); scanf("%s", usn);
printf("Name : "); scanf("%s", name);
printf("Progrm : "); scanf("%s", programme);
printf("Phone : "); scanf("%s", phone);
printf("Sem : "); scanf("%d", &sem);
case 3:
first = delete_front(first);
break;
case 4:
first = delete_rear(first);
break;
case 5:
display(first);
break;
default:
exit(0);
}
}
}