Ds 1, 3-9 Programs With Output
Ds 1, 3-9 Programs With Output
Ds 1, 3-9 Programs With Output
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 NO_OF_DAYS 7
typedef struct
{
char *name_of_day;
int date_of_day;
char *activity_descr;
} CALENDER;
void create_calender(CALENDER a[], int i, char name[], int date, char activity[])
{
a[i].name_of_day = (char *) malloc(strlen(name) + 1);
strcpy(a[i].name_of_day, name);
a[i].date_of_day = date;
printf("Weeks activity\n");
for (i = 0; i < NO_OF_DAYS; i++)
{
printf("%-10s : %s\n", a[i].name_of_day, a[i].activity_descr);
}
}
void main()
{
CALENDER a[NO_OF_DAYS];
print_weeks_activity(a);
}
Output:
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 <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 5
int top = -1;
int stack[10];
void push(int item)
{
if (top == STACK_SIZE - 1)
{
printf("Overflow of stack\n");
return;
}
top++;
stack[top] = item;
}
void pop()
{
if (top == -1)
{
printf("Stack underflow\n");
return;
}
printf("Item deleted = %d\n", stack[top]);
top = top - 1;
}
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);
}
}
}
Output:
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);
}
Output:
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>
return stack[top--];
}
int main() {
char postfix[20];
double result;
return 0;
}
Output:
#include <stdio.h>
void main()
{
int n;
Output:
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 5
int queue[10], front = 0, rear = -1, count = 0;
void insert_rear(int item)
{
if (count == MAX)
{
printf("Q overflow\n");
return;
}
rear = (rear + 1) %MAX;
queue[rear] = item;
count++;
}
void delete_front()
{
if (count == 0)
{
printf("Q underFlow\n");
return;
}
printf("ITem deleted = %d\n", queue[front]);
front = (front + 1) % MAX;
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);
}
}
}
Output:
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;
};
typedef struct node *NODE;
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 NULL;
}
return NULL;
}
return cur;
}
printf("Student list\n");
if (first == NULL)
{
printf( "empty");
return;
}
cur = first;
while(cur != NULL)
{
printf("\n%-13s", cur -> usn);
printf("%-13s", cur -> name);
printf("%-13s", cur -> programme);
printf("%-13s", cur -> phone);
printf("%-5d", 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("3:Front delete 4: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 3:
first = delete_front(first);
break;
case 4:
first = delete_rear(first);
break;
case 5:
display(first);
break;
default:
exit(0);
}
}
}
Output:
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>
#include <string.h>
typedef struct
{
char ssn[10];
char name[10];
char dept[10];
char desg[10];
char ph[10];
float sal;
} EMPLOYEE;
struct node
{
char ssn[10];
char name[10];
char dept[10];
char desg[10];
char ph[10];
float sal;
NODE getnode()
{
NODE x;
return x;
}
if (head->rlink == head)
{
printf("Empty\n");
return;
}
cur = head->rlink;
count = 0;
return count;
}
temp = getnode();
strcpy(temp->ssn, emp.ssn);
strcpy(temp->name, emp.name);
strcpy(temp->dept, emp.dept);
strcpy(temp->desg, emp.desg);
strcpy(temp->ph, emp.ph);
temp->sal = emp.sal;
return head;
}
temp = getnode();
strcpy(temp->ssn, emp.ssn);
strcpy(temp->name, emp.name);
strcpy(temp->dept, emp.dept);
strcpy(temp->desg, emp.desg);
strcpy(temp->ph, emp.ph);
temp->sal = emp.sal;
// Get the address of the first node
first = head->rlink;
head->rlink = temp;
temp->llink = head;
return head;
}
return head;
}
return head;
}
int main()
{
int choice, count;
NODE head;
EMPLOYEE emp;
head = getnode();
head->rlink = head->llink = head;
for (;;)
{
printf("1:Insert Front 2:Insert Rear\n");
printf("3:Delete Front 4:Delete Rear\n");
printf("5:Display 6:Count 7: Exit : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
read_employee_details(&emp);
break;
case 2:
read_employee_details(&emp);
break;
case 3:
head = delete_front(head);
break;
case 4:
head = delete_rear(head);
break;
case 5:
display(head);
break;
break;
default:
exit(0);
}
}
}
Output:
9. Develop a Program in C for the following operations on Singly Circular Linked List
(SCLL) with header nodes.
a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3
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>
struct node
{
int c;
int px;
int py;
int pz;
struct node *link;
};
sum = 0;
cur = head->link;
while (cur != head)
{
sum = sum + cur->c * pow(x, cur->px) * pow(y,cur->py) * pow(z, cur->pz);
cur = cur->link;
}
return sum;
}
printf("\n");
}
NODE getnode()
{
NODE x;
if (x == NULL)
{
printf("Not enough memory\n");
exit(0);
}
return x;
}
NODE insert_rear(int c, int px, int py, int pz, NODE head)
{
NODE cur, temp;
temp = getnode();
temp->c = c;
temp->px = px;
temp->py = py;
temp->pz = pz;
cur = head->link;
while (cur->link != head)
{
cur = cur->link;
}
cur->link = temp;
temp->link = head;
return head;
}
NODE read_polynomial()
{
NODE head;
int c, px, py, pz;
head = getnode();
head->link = head;
for(;;)
{
scanf("%d", &c);
if (c == 0) break;
scanf("%d", &px);
scanf("%d", &py);
scanf("%d", &pz);
return head;
}
h3 = getnode();
h3->link = h3;
break;
}
}
return h3;
}
int main()
{
NODE h1, h2, h3;
int choice;
float x, y, z, sum;
for(;;)
{
printf("1:Add 2:Evaluate 3:Exit : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter first polynomial : ");
h1 = read_polynomial();
printf("Poly1: ");
print_polynomial(h1);
printf("Poly2: ");
print_polynomial(h2);
h3 = add_2_polynomials(h1, h2);
printf("Poly3: ");
print_polynomial(h3);
break;
case 2:
printf("Enter a polynomial");
h1 = read_polynomial();
break;
default:
exit(0);
}
}
}
Output: