0% found this document useful (0 votes)
18 views21 pages

program 1-7

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views21 pages

program 1-7

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

​#include <stdio.

h>
#include <stdlib.h>

struct Day {
char *dayName;
int date;
char *activity;
};

void create(struct Day *day) {


day->dayName = (char *)malloc(sizeof(char) * 20);
day->activity = (char *)malloc(sizeof(char) * 100);

printf("Enter the day name: ");


scanf("%s", day->dayName);

printf("Enter the date: ");


scanf("%d", &day->date);

printf("Enter the activity for the day: ");


scanf(" %[^\n]s", day->activity);
}

void read(struct Day *calendar, int size) {


for (int i = 0; i < size; i++) {
printf("Enter details for Day %d:\n", i + 1);
create(&calendar[i]);
}
}

void display(struct Day *calendar, int size) {


printf("\nWeek's Activity Details:\n");
for (int i = 0; i < size; i++) {
printf("Day %d:\n", i + 1);
printf("Day Name: %s\n", calendar[i].dayName);
printf("Date: %d\n", calendar[i].date);
printf("Activity: %s\n", calendar[i].activity);
printf("\n");
}
}

void freeMemory(struct Day *calendar, int size) {


for (int i = 0; i < size; i++) {
free(calendar[i].dayName);
free(calendar[i].activity);
}
}

int main() {
int size;
printf("Enter the number of days in the week: ");
scanf("%d", &size);

struct Day *calendar = (struct Day *)malloc(sizeof(struct Day) * size);

if (calendar == NULL) {
printf("Memory allocation failed. Exiting program.\n");
return 1;
}

read(calendar, size);
display(calendar, size);

freeMemory(calendar, size);
free(calendar);

return 0;
}
​#include<stdio.h>

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


int c = 0, m = 0, i = 0, j = 0, k, flag = 0;
void stringmatch()
{
while (str[c] != '\0')
{
if (str[m] == pat[i])
{
i++;
m++;
if (pat[i] == '\0')
{
flag = 1;
for (k = 0; rep[k] != '\0'; k++, j++)
{
res[j] = rep[k];
}
i = 0;
c = m;
}
}
else
{
res[j] = str[c];
j++;
c++;
m = c;
i = 0;
}
}
res[j] = '\0';
}
void main()
{
printf("Enter the main string:");
gets(str);
printf("\nEnter the pat string:");
gets(pat);
printf("\nEnter the replace string:");
gets(rep);
printf("\nThe string before pattern match is:\n %s", str);
stringmatch();
if (flag == 1)
printf("\nThe string after pattern match and replace is: \n %s ", res);
else
printf("\nPattern string is not found");
}
​#include<stdio.h>

#include<stdlib.h>

#define MAX 3

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);
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()
{
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");
}
}
​#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("\n Enter the valid infix expression:");
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 '^':
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 '^':
case '$':
p = 3;
break;
}
return p;
}
​#include<stdio.h>

#include<stdlib.h>

#include<math.h>

int i, top = -1;


int op1, op2, res, s[20];
char postfix[90], symb;

void push(int item)


{
top = top + 1;
s[top] = item;
}

int pop()
{
int item;
item = s[top];
top = top - 1;
return item;
}

void main()
{
printf("\nEnter a valid postfix expression:\n");
scanf("%s", postfix);
for (i = 0; postfix[i] != '\0'; i++)
{
symb = postfix[i];
if (isdigit(symb))
{
push(symb - '0');
}
else
{
op2 = pop();
op1 = pop();
switch (symb)
{
case '+':
push(op1 + op2);
break;
case '-':
push(op1 - op2);
break;
case '*':
push(op1 * op2);
break;
case '/':
push(op1 / op2);
break;
case '%':
push(op1 % op2);
break;
case '$':
case '^':
push(pow(op1, op2));
break;
default:
push(0);
}
}
}
res = pop();
printf("\n Result = %d", res);
}
​#include <stdio.h>void tower(int n, int source, int temp, int destination)
{
if (n == 0)
return;
tower(n - 1, source, destination, temp);
printf("\nMove disc %d from %c to %c", n, source, destination);
tower(n - 1, temp, source, destination);
}
void main()
{
int n;
printf("\nEnter the number of discs: \n");
scanf("%d", & n);
tower(n, 'A', 'B', 'C');
printf("\n\nTotal Number of moves are: %d", (int) pow(2, n) - 1);
}
#include <stdio.h>void tower(int n, int source, int temp, int destination)
{
if (n == 0)
return;
tower(n - 1, source, destination, temp);
printf("\nMove disc %d from %c to %c", n, source, destination);
tower(n - 1, temp, source, destination);
}
void main()
{
int n;
printf("\nEnter the number of discs: \n");
scanf("%d", & n);
tower(n, 'A', 'B', 'C');
printf("\n\nTotal Number of moves are: %d", (int) pow(2, n) - 1);
}
​#include<stdio.h>
#include<stdlib.h>

#define MAX 5

char circular_queue[MAX];
int front = -1, rear = -1;

int isEmpty()
{
if (front == -1 && rear == -1)
return 1;
else
return 0;
}

int isFull()
{
if ((rear + 1) % MAX == front)
return 1;
else
return 0;
}

void insertElement(char element)


{
if (isFull())
{
printf("Circular Queue Overflow\n");
return;
}
else if (isEmpty())
{
front = rear = 0;
}
else
{
rear = (rear + 1) % MAX;
}
circular_queue[rear] = element;
}

void deleteElement()
{
if (isEmpty())
{
printf("Circular Queue Underflow\n");
return;
}
else if (front == rear)
{
front = rear = -1;
}
else
{
front = (front + 1) % MAX;
}
}

void display()
{
int i;
if (isEmpty())
{
printf("Circular Queue is empty\n");
return;
}
printf("Circular Queue elements: ");
i = front;
do
{
printf("%c ", circular_queue[i]);
i = (i + 1) % MAX;
}
while (i != (rear + 1) % MAX);
printf("\n");
}

int main()
{
int choice;
char element;
do
{
printf("\n\n---- Circular Queue Menu ----\n");
printf("1. Insert an Element\n");
printf("2. Delete an Element\n");
printf("3. Display Circular Queue\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("Enter element to be inserted: ");
scanf(" %c", &element);
insertElement(element);
break;
case 2:
deleteElement();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Please enter a valid option.\n");
}
}
while(choice != 4);

return 0;
}
​#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)
{
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;
}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("\n--------Stack 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()
{
int ch, i, n;
while (1)
{
printf("\n--------Menu--------");
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");}
}
}

You might also like