0% found this document useful (0 votes)
6 views14 pages

Calendar

The document contains multiple C programming code snippets for various data structures and algorithms, including calendar management, string manipulation, stack operations, infix to postfix conversion, circular queues, singly linked lists, and doubly linked lists. Each section includes function definitions for creating, reading, displaying, and manipulating these data structures. The code examples illustrate fundamental programming concepts and data handling techniques in C.

Uploaded by

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

Calendar

The document contains multiple C programming code snippets for various data structures and algorithms, including calendar management, string manipulation, stack operations, infix to postfix conversion, circular queues, singly linked lists, and doubly linked lists. Each section includes function definitions for creating, reading, displaying, and manipulating these data structures. The code examples illustrate fundamental programming concepts and data handling techniques in C.

Uploaded by

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

1.

calendar for ( i = 0; i < n; i++)


{free(calendar[i]->name); free(calendar[i]-
#include <stdio.h>
>activity); free(calendar[i]);}
#include <stdlib.h>
getch();
#include <string.h>
return 0;}
struct Day {char *name; int date;
char *activity;}; 2. string
void create(struct Day *calendar[], int n)
{int i; #include <stdio.h>
for (i=0; i<n; i++) int length(char str[]) { int len = 0;
{calendar[i] = (struct Day*) while (str[len] != '\0') { len++;}
malloc(sizeof(struct Day));} return len;}
}void read(struct Day *calendar[], int n) int patternExists(char str[], char pattern[], int
{ int i; startPos) { int i, j;
for ( i = 0; i < n; i++) { for (i = startPos; str[i] != '\0'; i++) {
calendar[i]->name=(char*) malloc(20); for (j = 0; pattern[j] != '\0'; j++) {
calendar[i]->activity=(char*) malloc(20); if (str[i + j] != pattern[j]) { break;}
printf("Enter the name of day %d: ", i + 1); }if (pattern[j] == '\0') { return i;}
scanf("%s", calendar[i]->name); printf("Enter }return -1;}
the date"); void replacePattern(char str[], char pattern[],
scanf("%d", &calendar[i]->date); printf("Enter char replace[]) { int strLen = length(str);
the activity"); int patLen = length(pattern); int repLen =
scanf(" %s", calendar[i]->activity);} length(replace); int i, j, k, pos;
}void display(struct Day *calendar[], int n) for (i = 0; i < strLen; ) {
{int i; pos = patternExists(str, pattern, i); if (pos != -1) {
printf("\nWeek's Activity Details:\n"); for (j = pos, k = 0; k < repLen; j++, k++) { str[j] =
for ( i = 0; i < n; i++) replace[k];
{printf("Day: %s, Date: %d, Activity: %s\n", }i = pos + repLen;
calendar[i]->name, calendar[i]->date, } else {
calendar[i]->activity);} printf("pattern does not exist"); exit(0);
}int main() }}}int main() {
{ int n = 7,i; char mainString[100], pattern[20], replace[20];
struct Day *calendar[7]; create(calendar, n); printf("Enter the main string: ");
read(calendar, n); display(calendar, n); gets(mainString);
printf("Enter the pattern string: "); void push()
gets(pattern); {int ele;
printf("Enter the replace string: "); if (top == MAX - 1)
gets(replace); {printf("stack overflow\n"); return;
replacePattern(mainString, pattern, replace); }printf("enter an element to be pushed\n");
printf("Result after pattern matching and scanf("%d", &ele);
replacement: %s\n", mainString); return 0;} top = top + 1; s[top] = ele; return;
3. stack
}void pop()
{if (top == -1)
#include <stdio.h> {printf("Stack Underflow\n"); return;
#include <stdlib.h> }printf("Element popped is %d \n", s[top]); top =
#define MAX 5 top - 1;return;
int s[MAX]; }void display()
int top = -1; void push(); void pop(); {int i;
void palindrome(); void display(); void main() if (top == -1)
{int ch; while (1) {printf("Stack is Empty\n"); return;
{printf("\nMENU:\n1.push\n2.pop\n3.palindro }printf("Stack elements are\n"); for (i = top; i >=
me\n4.dispaly\n5.Exit\nenter your choice"); 0; i--)
scanf("%d", &ch); printf("| %d |\n", s[i]); return;
switch (ch) }void palindrome()
{case 1: {int flag = 1, i;
push(); break; for (i = 0; i <= top / 2; i++)
case 2: {if (s[i] != s[top - i])
pop(); break; {flag = 0; break;
case 3: }}if (flag == 1)
palindrome(); break; printf("It is palindrome\n"); else
case 4: printf("Not a palindrome\n");
display(); break; }
case 5:
exit(0); break; 4. infix
default: #include <stdio.h>
printf("enter the valid choice\n"); #include <string.h>
}}}
#include <ctype.h> }if (top != -1 && s[top] != '(') { printf("Invalid
#define MAX_SIZE 100 Expression\n"); exit(0);
Char }else {
s[MAX_SIZE],token,infix[MAX_SIZE],postfix[MAX pop();
_SIZE]; int top=-1,i,j; top--;
int push( char item) }}else if (isOperator(token))
{.s[top + 1] = item; {while (top != -1 && getPrecedence(s[top]) >=
return top + 1; // Return the updated top index getPrecedence(token))
}char pop() {postfix[j++] = pop();
{return s[top]; top--;
}int isOperator(char ch) { }top = push(token);
return (ch == '+' || ch == '-' || ch == '*' || ch == }}while (top != -1) { postfix[j++] = pop(); top--;
'/' || ch == '%' || ch == '^'); }postfix[j] = '\0';
}int getPrecedence(char operator) }int main()
{ if (operator == '^') {printf("Enter infix expression: ");
return 3; scanf("%s",infix);
else if (operator == '*' || operator == '/' || infixToPostfix(infix, postfix);
operator == '%') return 2; printf("Postfix expression: %s\n", postfix);
else if (operator == '+' || operator == '-') return return 0;}
1;
else //5.a.suffix
return 0; #include <stdio.h>
}void infixToPostfix(char infix[], char postfix[]) #include <stdlib.h>
{for (i = 0, j = 0; infix[i] != '\0'; i++) #include <math.h>
{token = infix[i]; #include <ctype.h>
if (isalnum(token)) { postfix[j++] = token; int i, top = -1;
}else if (token == '(') int op1, op2, res, s[20];
{top = push(token); char postfix[50], symbol;
}else if (token == ')') void push(int item)
{while (top != -1 && s[top] != '(') {top = top + 1;
{postfix[j++] = pop(); top--; s[top] = item;
}int pop()
{int item;
item = s[top]; top = top - 1; return item; {if (n == 0) return;
}void main() towerOfHanoi(n - 1, source, dest, temp);
{printf("\nEnter a valid postfix expression:\ printf("\nMove disc %d from %c to %c ", n,
n"); scanf("%s", postfix); source, dest); towerOfHanoi(n - 1,temp, source,
for (i = 0; postfix[i] != '\0'; i++) dest);
{symbol = postfix[i]; if (isdigit(symbol)) }void main()
{push(symbol - '0'); {int n, res;
}else printf("\n enter the number of disc:\n");
{op2 = pop(); op1 = pop(); switch (symbol) scanf("%d", &n);
{case '+': towerOfHanoi(n, 'A', 'B', 'C'); res = pow(2, n) - 1;
push(op1 + op2); break; printf("the total number of moves are : %d",
case '-': res);
push(op1 - op2); break;
case '*': }
push(op1 * op2); break; //6. circular queue
case '/':
push(op1 / op2); break; #include <stdio.h>
case '%': #include <string.h>
push(op1 % op2); break; #include <conio.h>
case '$': #include <stdlib.h>
case '^': #define MAX 5
push(pow(op1, op2)); break; int front = 0, rear = -1, count = 0; char cq[MAX],
default:printf("invalid operator\n"); exit(0); ele;
}}}res = pop(); void insert(); void display(); void delete(); void
printf("\n Result = %d", res); main()
{int choice; while (1)
} {printf("\n\t1=>insert an element on to
//5.b.hanoi CIRCULAR QUEUE\n\t2=>delete an element
from CIRCULAR QUEUE\n\t3=>display the status
#include <stdio.h> of CIRCULAR QUEUE\n\t4=>exit\n");
#include <math.h> scanf("%d", &choice); switch (choice)
void towerOfHanoi(int n, int source, int temp, {case 1:
int dest) insert(); break;
case 2:
delete (); break;
case 3: //7.sll
display(); break;
case 4: #include <stdio.h>
exit(0); default: #include <string.h>
printf("Enter a valid choice\n"); #include <stdlib.h>
struct node
}}} {char usn[25], name[25], branch[25]; int sem;
long int phone; struct node *link;
void insert()
};
{if (count == MAX)
{printf("Circular Queue is full, elements cannot typedef struct node *NODE; NODE first;
be inserted\n"); return; int count = 0; NODE getNode()
}rear = (rear + 1) % MAX; {NODE newnode;
printf("\nenter the element to be inserted into char usn[20], name[20], branch[20]; int sem;
the Circular Queue\n"); ele = getche(); long int phone;
cq[rear] = ele; count++; printf("\nEnter the usn, Name, Branch, sem,
PhoneNo ofthe student: \n"); scanf("%s %s %s
%d %ld", usn, name, branch, &sem, &phone);
}void delete() newnode = (NODE)malloc(sizeof(struct
{if (count == 0) node));
{printf("Circular Queue is empty, no elements to if (newnode == NULL)
delete\n"); return; {printf("\nMemory is not available"); exit(0);
}ele = cq[front]; }strcpy(newnode->usn, usn); strcpy(newnode-
front = (front + 1) % MAX; >name, name); strcpy(newnode->branch,
printf("the element deleted is%c\n", ele); count- branch); newnode->sem = sem;
-; newnode->phone = phone; count++;
return newnode;
}void display()
}void insertAtFront()
{int i;
{NODE temp; temp = getNode(); temp->link =
if (count == 0)
first; first = temp; return;
{printf("CIRCULAR QUEUE is empty,no element
}void deleteAtFront()
to display\n"); return;
{NODE temp;
}printf("Circular Queue contents are\n");
if (first == NULL)
for (i = front; i != rear; i = (i + 1) % MAX)
{printf("\nLinked list is empty"); return;
{printf("%c\t", cq[i]);
}if (first->link == NULL)
}printf("%c", cq[i]);}
{printf("\nThe Student node with usn:%s is }printf("\nThe student node with the usn:%s is
deleted ", first->usn); count--; deleted", cur->usn); free(cur);
free(first); prev->link = NULL; count--;
first = NULL; return; return;}
}temp = first; void displayStatus()
first = first->link; {NODE cur;
printf("\nThe Student node with usn:%s is int nodeNo = 1;
deleted", temp->usn); count--; if (first == NULL)
free(temp); return; {printf("\nNo Contents to display in SLL \n");
}void insertAtEnd() return;
{NODE cur, temp; temp = getNode(); temp->link }printf("\nThe contents of SLL: \n"); cur = first;
= NULL; if (first == NULL) while (cur != NULL)
{first = temp; return; {printf("\n||%d||", nodeNo); printf(" USN:%s|",
}if (first->link == NULL) cur->usn); printf(" Name:%s|", cur->name);
{first->link = temp; return; printf(" Branch:%s|", cur->branch); printf("
}cur = first; Sem:%d|", cur->sem); printf(" Ph:%ld|", cur-
while (cur->link != NULL) >phone); cur = cur-
{cur = cur->link; >link; nodeNo++;
}cur->link = temp; return; }printf("\n No of student nodes is %d \n",
}void deleteAtEnd() count);
{NODE cur, prev; if (first == NULL) void stackDemoUsingSLL()
{printf("\nLinked List is empty"); return; {int ch; while (1)
}if (first->link == NULL) {printf("\n~~~Stack Demo using SLL~~~\n");
printf("\nThe student node with the usn:%s is printf("\n1:Push operation \n2: Pop operation
deleted\n", first->usn); free(first); \n3: Display\n4:Exit \n"); printf("\nEnter your
first = NULL; count--; return; choice for stack demo");
}prev = NULL; scanf("%d", &ch); switch (ch)
cur = first; {case 1:
while (cur->link != NULL) insertAtFront(); break;
{prev = cur; case 2:
cur = cur->link; deleteAtFront(); break;
case 3:
displayStatus(); break;
case 4: }}}
return;
//8.a.dll
#include <stdio.h>
}}} #include <string.h>
void main() #include <stdlib.h>
{int ch, i, n; while (1) struct node
{printf("\n~~~Menu~~~"); {char ssn[25], name[25], dept[10],
printf("\nEnter your choice for SLL operation designation[25]; long int sal, phoneno;
\n"); printf("\n1:Create SLL of Student Nodes"); struct node *llink; struct node *rlink;
printf("\n2:DisplayStatus"); printf("\ };
n3:InsertAtEnd"); printf("\n4:DeleteAtEnd");
printf("\n5:Stack Demo using SLL(Insertion and typedef struct node *NODE; NODE first = NULL;
Deletion at Front)"); printf("\n6:Exit \n"); int count = 0; NODE getNode()
printf("\nEnter your choice:"); scanf("%d", &ch); {NODE newnode;
switch (ch) newnode = (NODE)malloc(sizeof(struct node));
{case 1: printf("\nEntertheEmployeessn,Name,Dept,Desi
printf("\nEnter the no of students: "); gnation,Salary,PhoneNo\n");
scanf("%d", &n); scanf("%s%s%s%s%ld%ld", newnode->ssn,
for (i = 1; i <= n; i++) insertAtFront(); newnode->name, newnode->dept,
break; newnode-
case 2: >designation, &newnode->sal, &newnode-
displayStatus(); break; >phoneno);
case 3: if (newnode == NULL)
insertAtEnd(); break; {printf("\nRunning out of memory"); exit(0);
case 4: }newnode->llink = NULL; newnode->rlink =
deleteAtEnd(); break; NULL; count++;
case 5: return newnode;
stackDemoUsingSLL(); break; }void insertAtFront()
case 6:
{
exit(0); default:
printf("\nEnter the valid choice"); NODE temp; temp = getNode(); if (first == NULL)
{first = temp; return;
}temp->rlink = first; first->llink = temp; first
temp; return;
}void deleteAtFront()
{NODE temp;
if (first == NULL) return;}
{printf("\nDoubly Linked List is empty"); return; void displayStatus()
}temp = first; {NODE cur;
first = first->rlink; temp->rlink = NULL; first->llink int nodeno = 1;
= NULL; if (first == NULL)
printf("\nThe employee node with the ssn:%sis {printf("\nNo Contents to display in DLL");
deleted", temp->ssn); free(temp); return;}
count--; return;} printf("\nENode||SSN|Name|Department|Desi
void insertAtEnd() gnation|Salary|Phone\n"); cur = first;
{NODE cur, temp; temp = getNode(); if (first == while (cur != NULL)
NULL) {printf("\n%d||%s|%s|%s|%s|%ld|%ld",
{first = temp; return; nodeno, cur->ssn, cur->name, cur->dept,
}cur = first; cur-
while (cur->rlink != NULL) >designation, cur->sal, cur->phoneno);
{cur = cur->rlink; cur = cur->rlink; nodeno++;
}cur->rlink = temp; temp->llink = cur; return; }printf("\nNo of employee nodes is%d", count);
}void deleteAtEnd() }void doubleEndedQueueDemo()
{NODE prev, cur; if (first == NULL) {int ch; while (1)
{printf("\nDoubly Linked List is empty"); return;} {printf("\nDemo Double Ended
if (first->rlink == NULL) Queue Operation");
{printf("\nThe employee node with the ssn:%s is printf("\n1:Insert Queue Front\n2:Delete Queue
deleted", first->ssn); free(first); Front\n3:Insert Queue Rear\n 4:Delete Queue
first = NULL; count--; return; Rear\n5:Display Status\n6:Exit\n");
}prev = NULL; cur = first; scanf("%d", &ch);
while (cur->rlink != NULL) switch (ch)
{prev = cur; {case 1:
cur = cur->rlink; insertAtFront(); break;
}cur->llink = NULL; case 2:
printf("\nThe employee node with the ssn:%s is deleteAtFront(); break;
deleted", cur->ssn); case 3:
free(cur); insertAtEnd(); break;
prev->rlink = NULL; count--; case 4:
deleteAtEnd(); break;
case 5:
displayStatus(); break; typedef struct PolyNode { int coef;
case 6: int expon;
return; default: struct PolyNode *next;
printf("invalid choice\n"); } PolyNode;
typedef struct PolyNode
}}}
*PolyPointer; PolyPointer
void main() getnode(void) {
{int ch, i, n; while (1) PolyPointer temp;
{printf("\n~~~Menu~~~"); printf("\ temp = (PolyPointer)malloc(sizeof(PolyNode)); if
nEnter your choice for DLL (temp == NULL) {
operation\n"); printf("\n1:Create DLL of employ fprintf(stderr, "Memory allocation failed.\n");
Nodes"); exit(EXIT_FAILURE);
printf("\n2:Display Status"); }return temp;}
printf("\n3:double Ended Queue Demo"); PolyPointer createPolynomial() { PolyPointer
printf("\n4:Exit\n"); poly = NULL; PolyPointer last = NULL;
printf("\nEnter your choice:"); scanf("%d", &ch); int choice;
switch (ch)
{case 1:
printf("\nEnter the no of Employees:"); do {
scanf("%d", &n); int coef, expon;
for (i = 1; i <= n; i++) printf("Enter coefficient: "); scanf("%d", &coef);
insertAtEnd(); break; printf("Enter exponent: ");
case 2: scanf("%d", &expon);
displayStatus(); break; PolyPointer term = getnode(); term->coef =
case 3: coef;
doubleEndedQueueDemo(); break; term->expon =expon; term->next = NULL;
case 4: if (poly == NULL) { poly = term;
exit(0); default: last = term;
printf("\nEnter the valid choice"); } else {
last->next = term; last = term;
}}} }printf("Add another term? (1 for yes, 0 for no):
//9a.polynomial ");
#include <stdio.h> scanf("%d", &choice);
#include <stdlib.h> } while (choice == 1);
return poly; fprintf(stderr, "Memory allocation failed.\n");
}int evaluatePoly(PolyPointer poly, int x) { exit(EXIT_FAILURE);
int result = 0; }return temp;
while (poly != NULL) { }int compare(int a, int b) { if (a < b)
result = (result * x) + poly->coef; poly = poly- return -1;
>next; else if (a == b) return 0;
}return result; else
}int main() { return 1;
printf("Enter polynomial coefficients and }void attach(int coef, int expon, PolyPointer
exponents:\n"); PolyPointer poly = *rear) { PolyPointer temp;
createPolynomial(); temp = getnode();
int x; temp->coef = coef; temp->expon =expon;
printf("Enter the value of x: "); scanf("%d", &x); (*rear)->next = temp;
int result = evaluatePoly(poly, x); *rear = temp;
printf("Result of polynomial evaluation at x = }PolyPointer padd(PolyPointer a, PolyPointer b)
%d is: %d\n", x, result); { PolyPointer c, rear, temp;
PolyPointer temp; while (poly != NULL) int sum;
{ temp = poly; rear = getnode(); c = rear;
poly = poly->next; free(temp); while (a && b) {
}return 0; switch (compare(a->expon, b->expon)) { case -
1: attach(b->coef, b->expon,&rear); b = b->next;
}
break; case 0:
//9.b.add polynomial sum = a->coef + b->coef;
#include <stdio.h> if (sum) {
#include <stdlib.h> attach(sum, a->expon, &rear);
typedef struct PolyNode { int coef; a = a->next;
int expon; b = b->next;
struct PolyNode *next; }break; case 1:
} PolyNode; attach(a->coef, a->expon, &rear); a = a->next;
typedef struct PolyNode break;
*PolyPointer; PolyPointer }
getnode(void) {
PolyPointer temp;
temp = (PolyPointer)malloc(sizeof(PolyNode)); if
(temp == NULL) {
}for (; a; a = a->next) printf("Add another term? (1 for yes, 0 for no):
attach(a->coef, a->expon, &rear); for (; b; b = b- "); scanf(" %d", &choice);
>next) } while (choice == 1);
attach(b->coef, b->expon,&rear); rear->next = return poly;
NULL; }
temp = c;
c = c->next; free(temp); int main() {
return c; printf("Enter polynomial A:\n"); PolyPointer a =
} createPolynomial();
printf("Enter polynomial B:\n"); PolyPointer b =
void display(PolyPointer result) { createPolynomial();
printf("\nResult of polynomial addition:\n"); PolyPointer result = padd(a, b);
while (result != NULL) { display(result);
printf("%dx^%d ", result->coef, result->expon); PolyPointer temp;
result = result->next; while (result != NULL) { temp = result;
if (result != NULL) printf("+ "); result = result->next; free(temp);
}printf("\n"); }return 0;}
} //10.
PolyPointer createPolynomial() { PolyPointer #include <stdio.h>
poly = NULL; PolyPointer last = NULL; #include <stdlib.h>
int choice; struct node
do { {int data;
int coef, expon; struct node *lchild; struct node *rchild;};
printf("Enter coefficient: "); scanf("%d", &coef); typedef struct node *NODE; NODE root = NULL;
printf("Enter exponent: "); scanf("%d", &expon); void insert()
PolyPointer term = getnode(); term->coef = {NODE newnode, prev, curr;
coef; newnode = (NODE)malloc(sizeof(struct
term->expon =expon; term->next = NULL; node)); printf("Enter the element to be
if (poly == NULL) { poly = term; inserted in
last = term; tree\n"); scanf("%d", &newnode->data);
} else { newnode->lchild = newnode->rchild = NULL; if
last->next = term; last = term; (root == NULL)
} {root = newnode; return;
}prev = NULL; curr = root;
while (curr != NULL) {if (root != NULL)
{if (curr->data == newnode->data) {inorder(root->lchild); printf("%4d", root->data);
{printf("Duplicate is not possible\ inorder(root->rchild);
n"); free(newnode); }return;
return; }void preorder(NODE root)
}prev = curr; {if (root != NULL)
if (newnode->data < curr->data) curr = curr- {printf("%4d", root->data); preorder(root-
>lchild; >lchild); preorder(root->rchild);
else }return;
curr = curr->rchild; }void postorder(NODE root)
}if (newnode->data <prev->data) prev->lchild = {if (root != NULL)
newnode; {postorder(root->lchild); postorder(root-
else >rchild); printf("%4d", root->data);
prev->rchild = newnode; return; }return;
}void display(NODE root, int level)
}
{int i;
void search(NODE root) if (root)
{display(root->rchild, level + 1);
{ printf("\n");
int key; NODE curr; for (i = 0; i < level; i++) printf("\t");
printf("Enter the element to be searched \n"); printf("%d", root->data); display(root->lchild,
scanf("%d", &key); if (root == NULL) level + 1);
{printf("tree is empty\n"); return; }return;
}curr = root; }
while (curr != NULL)
{if (curr->data == key) void main()
{int ch, i, n, item; NODE res;
{ for (;;)
printf("Number found\n"); return; {printf("\n**TREE OPERATIONS ARE**\n");
}if (key < curr->data) curr = curr->lchild; printf (" 1:Insert node\n 2:Inorder traversal\n
else 3:Preorder traversal\n 4:Postorder
curr = curr->rchild; traversal\n5:Display\n 6:Search\n 7:Exit");
}printf("Number not found\n"); return; printf("\n*********\n"); printf("\nEnter your
}void inorder(NODE root) choice:\n"); scanf("%d",&ch);
switch(ch) int a[50][50], n, visited[50]; int q[20], front = -
{case 1: 1,rear = -1; int s[20], top = -1, count=0;
printf("Enter the no of elements\n"); void bfs(int v)
scanf("%d", &n); {int i, cur; visited[v] = 1; q[++rear] = v; while(front!
for (i = 0; i < n; i++) insert(); =rear)
break; case 2: {cur = q[++front]; for(i=1;i<=n;i++)
if (root == NULL) {if((a[cur][i]==1)&&(visited[i]==0))
printf("Empty tree\n"); else {q[++rear] = i; visited[i] = 1; printf("%d ", i);
{printf("Inorder traversal\n"); inorder(root);
}break; case 3: }}}}
if (root == NULL) void dfs(int v)
printf("Empty tree\n"); else {int i; visited[v]=1; s[++top] = v;
{printf("preorder traversal\n"); preorder(root); for(i=1;i<=n;i++)
}break; case 4: {if(a[v][i] == 1&& visited[i] == 0 )
if (root == NULL) {printf("%d ", i); dfs(i);
printf("Emtpy tree\n"); else }}}int main()
{printf("Postorder traversal\n"); postorder(root); {int ch, start, i,j;
}break; case 5: printf("\nEnter the number of vertices in graph:
if (root == NULL) "); scanf("%d",&n);
printf("Empty tree\n"); else printf("\nEnter the adjacency matrix:\n");
printf("The tree is:\n"); display(root, 1); for(i=1; i<=n; i++)
break; case 6: {for(j=1;j<=n;j++)
search(root); break; { scanf("%d",&a[i][j]);
case 7: }for(i=1;i<=n;i++) visited[i]=0;
exit(0); default: printf("\nEnter the starting vertex: ");
printf("Invalid choice\n"); scanf("%d",&start);
}}} printf("\n==>1. BFS: Print all nodes reachable
//11.bfs/dfs from a given starting node");
printf("\n==>2. DFS: Print all nodes reachable
#include<stdio.h> from a given starting node");
#include<stdlib.h> printf("\n==>3:Exit");
printf("\nEnter your choice: "); scanf("%d",
&ch);
switch(ch)
{case 1: printf("\nNodes reachable from starting {
vertex %d are: ", start); bfs(start);
for(i=1;i<=n;i++) int i;
{if(visited[i]==0) printf("\nEnter the number of employee records
printf("\nThe vertex that is not reachable is %d" (N) : "); scanf("%d", &n);
,i); printf("\nEnter the two digit memory locations
}break; (m) for hash table: "); scanf("%d", &m);
case 2: printf("\nNodes reachable from starting ht = (int *)malloc(m*sizeof(int)); for(i=0; i<m;
vertex %d are:\n",start); dfs(start); i++)
break; ht[i] = -1;
case 3: exit(0); printf("\nEnter the four digit key values (K) for N
default: printf("\nPlease enter valid choice:"); Employee Records:\n "); for(i=0; i<n; i++)
scanf("%d", &key[i]);
}}}
//12.hash table
#include<stdio.h> for(i=0;i<n;i++)
#include<stdlib.h> {if(count == m)
int key[20],n,m; {printf("\n~~~Hash table is full. Cannot insert
int *ht,index; the record %d key~~~",i+1); break;
int count = 0; }insert(key[i]);
void insert(int key) }display();
{index = key % m; while(ht[index] != -1) }
{index = (index+1)%m;
}ht[index] = key; count+
+;
}void display()
{int i;
if(count == 0)
{printf("\nHash Table is empty"); return;
}printf("\nHash Table contents are:\n "); for(i=0;
i<m; i++)
printf("\n T[%d] --> %d ", i, ht[i]);
}void main()

You might also like