DS Lab
DS Lab
/***************************************************************************
*File : 01Calender.c
*Description: Calendar operations
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define NUM_DAYS_IN_WEEK 7
int main()
{
// Create the calendar
DAYTYPE *weeklyCalendar = fnCreateCal();
return 0;
}
DAYTYPE *fnCreateCal()
{
DAYTYPE *calendar = (DAYTYPE *)malloc(NUM_DAYS_IN_WEEK * sizeof(DAYTYPE));
return calendar;
}
if(tolower(cChoice) == 'n')
continue;
printf("Date: ");
scanf("%d", &calendar[i].iDate);
printf("Activity: ");
char activityBuffer[100];
scanf(" %[^n]", activityBuffer); // Read the entire line, including spaces
calendar[i].acActivity = strdup(activityBuffer);
printf("n");
getchar(); //remove trailing enter character in input buffer
}
}
Output:
putta:~/.../Programs$ ./a.out
Do you want to enter details for day 1 [Y/N]: N
Do you want to enter details for day 2 [Y/N]: Y
Day Name: Monday
Date: 10
Activity: Meeting with Chairman.
Day 2:
Day Name: Monday
Date: 10
Activity: Meeting with Chairman.
Day 3:
No Activity
Day 4:
No Activity
Day 5:
Day Name: Thursday
Date: 13
Activity: Product Survey
Day 6:
Day Name: Friday
Date: 14
Activity: Budget Breakdown and Planning
Day 7:
Day Name: Saturday
Date: 15
Activity: Outing with family
Program 02 : String Operations
Develop a Program in C for the following operations on Strings.
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with REP if
PAT exists in STR. Report suitable messages in case PAT does not exist in STR
Support the program with functions for each of the above operations. Don’t use Built-in functions.
C Code
/***************************************************************************
*File : 02_SearchReplaceStringOps.c
*Description: Search for a pattern text in main text and replace it with replacement string
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char acMainStr[200], acSrchStr[30], acRepStr[30], acResStr[200], acCopyStr[200];
int i=0, j=0 ,k=0, l, iMtchCnt, iStop, len, iNumOfMatch=0;
strcpy(acCopyStr, acMainStr);
for(i=0;i<(strlen(acMainStr)-strlen(acSrchStr)+1);i++)
{
iMtchCnt = 0;
for(j=0;j<strlen(acSrchStr);j++)
{
if(acMainStr[i+j] == acSrchStr[j])
{
iMtchCnt++;
}
else
{
break;
}
if(iMtchCnt == strlen(acSrchStr)) //Check if number of character matches equals length of
pattern string
{
iNumOfMatch++; //update number of total matches by 1
// printf("nMatch occured at %d position in textn", i+1);
for(k=0;k<i;k++)
{
acResStr[k] = acMainStr[k]; //copy till the ith character where the match occured
}
iStop = k + strlen(acSrchStr); //point from where rest of the original string has to be copied
acResStr[k] = '';
strcat(acResStr, acRepStr); // append the replacement string
len = strlen(acResStr);
for(k=iStop, l=0; acMainStr[k] != '';k++, l++) //copy rest of original string
{
acResStr[len+l] = acMainStr[k];
}
acResStr[len+l] = '';
// printf("n%s",acResStr);
strcpy(acMainStr,acResStr);
}
}
}
printf("nInput Textn");
printf("%sn",acCopyStr);
if(iNumOfMatch > 0)
{
printf("n%d matches occurednnText after replacing matched patterns is shown belown",
iNumOfMatch);
printf("n%sn",acResStr);
}
else
{
printf("nPattern String not found in Textn");
}
return 0;
}
Output
putta:~/.../Programs$ ./a.out
2 matches occured
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAX 4
bool fnStkFull(int);
bool fnStkEmpty(int);
void fnPush(int [], int, int*);
int fnPop(int [], int*);
void fnDisplay(int[], int);
int fnPeek(int [], int);
bool fnChkPalindrome(int);
int main(void)
{
int stkArray[MAX];
int top = -1;
int iElem, iChoice;
for(;;)
{
printf("nSTACK OPERATIONSn");
printf("====================");
printf("n1.Pushn2.Popn3.Displayn4.Peekn5.Check Palindromen6.Demonstarte
Overflown7.Demonstarte Underflown8.EXITn");
printf("Enter your choicen");
scanf("%d",&iChoice);
switch(iChoice)
{
case 1: if(!fnStkFull(top))
{
printf("nEnter element to be pushed onto the stackn");
scanf("%d", &iElem);
fnPush(stkArray, iElem, &top);
}
else
{
printf("nStack Overflown");
}
break;
case 2: if(!fnStkEmpty(top))
{
iElem = fnPop(stkArray, &top);
printf("nPopped Element is %dn", iElem);
}
else
{
printf("nStack Underflown");
}
break;
case 3: if(fnStkEmpty(top))
{
printf("nStack Emptyn");
}
else
{
fnDisplay(stkArray, top);
}
break;
case 4: if(!fnStkEmpty(top))
{
iElem = fnPeek(stkArray, top);
printf("nElement at the top of the stack is %dn",
iElem);
}
else
printf("nEmpty Stackn");
break;
case 6: if(!fnStkFull(top))
printf("nThere are currently %d elements in StacknPush %d elemnts for Stack to
overflow", top+1, MAX - (top+1));
while(!fnStkFull(top))
{
printf("nEnter an element : ");
scanf("%d", &iElem);
fnPush(stkArray, iElem, &top);
}
printf("nStack Overflow cannot push elements onto the stackn");
break;
case 7: if(!fnStkEmpty(top))
printf("nThere are currently %d elements in StacknPop out %d elemnts for Stack to
Underflow", top+1, MAX - (top+1));
while(!fnStkEmpty(top))
{
iElem = fnPop(stkArray, &top);
printf("nPopped Element is %dn", iElem);
}
printf("nStack Underflow cannot pop elements from the stackn");
break;
case 8: exit(1);
bool fnStkFull(int t)
{
return ((t == MAX-1) ? true : false);
}
bool fnStkEmpty(int t)
{
return ((t == -1) ? true : false);
}
while(iCopy != 0)
{
iDig = iCopy % 10;
fnPush(palStk, iDig, &t);
iCopy /= 10;
}
int p = 0;
while(p <= t)
{
iDig = palStk[p];
iRev = iRev *10 + iDig;
p++;
}
if(iRev == iVal)
return true;
else
return false;
}
Output
putta:~/.../Programs$ ./a.out
STACK OPERATIONS
====================
1.Push
2.Pop
3.Display
4.Peek
5.Check Palindrome
6.Demonstarte Overflow
7.Demonstarte Underflow
8.EXIT
Enter your choice
5
STACK OPERATIONS
====================
1.Push
2.Pop
3.Display
4.Peek
5.Check Palindrome
6.Demonstarte Overflow
7.Demonstarte Underflow
8.EXIT
Enter your choice
5
5665 is a palindrome
STACK OPERATIONS
====================
1.Push
2.Pop
3.Display
4.Peek
5.Check Palindrome
6.Demonstarte Overflow
7.Demonstarte Underflow
8.EXIT
Enter your choice
6
Enter an element : 5
Enter an element : 6
Enter an element : 7
STACK OPERATIONS
====================
1.Push
2.Pop
3.Display
4.Peek
5.Check Palindrome
6.Demonstarte Overflow
7.Demonstarte Underflow
8.EXIT
Enter your choice
3
STACK OPERATIONS
====================
1.Push
2.Pop
3.Display
4.Peek
5.Check Palindrome
6.Demonstarte Overflow
7.Demonstarte Underflow
8.EXIT
Enter your choice
2
Popped Element is 7
STACK OPERATIONS
====================
1.Push
2.Pop
3.Display
4.Peek
5.Check Palindrome
6.Demonstarte Overflow
7.Demonstarte Underflow
8.EXIT
Enter your choice
7
Popped Element is 5
Popped Element is 4
STACK OPERATIONS
====================
1.Push
2.Pop
3.Display
4.Peek
5.Check Palindrome
6.Demonstarte Overflow
7.Demonstarte Underflow
8.EXIT
Enter your choice
1
STACK OPERATIONS
====================
1.Push
2.Pop
3.Display
4.Peek
5.Check Palindrome
6.Demonstarte Overflow
7.Demonstarte Underflow
8.EXIT
Enter your choice
1
STACK OPERATIONS
====================
1.Push
2.Pop
3.Display
4.Peek
5.Check Palindrome
6.Demonstarte Overflow
7.Demonstarte Underflow
8.EXIT
Enter your choice
3
STACK OPERATIONS
====================
1.Push
2.Pop
3.Display
4.Peek
5.Check Palindrome
6.Demonstarte Overflow
7.Demonstarte Underflow
8.EXIT
Enter your choice
4
STACK OPERATIONS
====================
1.Push
2.Pop
3.Display
4.Peek
5.Check Palindrome
6.Demonstarte Overflow
7.Demonstarte Underflow
8.EXIT
Enter your choice
8
Program 04 : Infix to Postfix Conversion
Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should
support for both parenthesised and free parenthesised expressions with the operators: +, -, *, /, %
(Remainder), ^ (Power) and alphanumeric operands.
C Code
/***************************************************************************
*File : 04_Infix_Postfix.c
*Description: Infix to Postfix conversion
***************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define STK_SIZE 10
int main()
{
int i, j=0;
char acExpr[50], acStack[50], acPost[50], cSymb;
int top = -1;
}
while(acStack[top] != '#')
{
acPost[j++] = fnPop(acStack, &top);
}
acPost[j] = '';
putta:~/.../Programs$ ./a.out
putta:~/.../Programs$ ./a.out
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#define STK_SIZE 10
int main()
{
int iaStack[50], i, iOp1, iOp2, iRes;
char acExpr[50], cSymb;
int top = -1;
}
iRes = fnPop(iaStack, &top);
printf("nValue of %s expression is %dn", acExpr, iRes);
return 0;
}
putta:~/.../Programs$ ./a.out
#include <stdio.h>
int main()
{
int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("n");
return 0;
printf("n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
}
Output
Enter the number of disks : 4
The sequence of moves involved in the Tower of Hanoi are :
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define QUEUE_SIZE 5
int main()
{
char myQueue[QUEUE_SIZE];
int iFront = -1, iRear = -1;
int iChoice;
char cElem;
for(;;)
{
printf("nQueue Operationsn");
printf("=====================");
printf("n1.Qinsertn2.Qdeleten3.Qdisplayn4.Exitn");
printf("Enter your choicen");
scanf("%d",&iChoice);
getchar(); //read trialing enter character
switch(iChoice)
{
case 1: if(!fnQueueFull(iFront, iRear))
{
printf("nEnter an element : ");
scanf("%c", &cElem);
fnInsertRear(myQueue, &iFront, &iRear, cElem);
}
else
{
printf("nQueue is Fulln");
}
break;
case 2: if(!fnQueueEmpty(iFront, iRear))
{
cElem = fnDeleteFront(myQueue, &iFront, &iRear);
printf("nDeleted element is %cn", cElem);
}
else
{
printf("nQueue is Emptyn");
}
break;
case 3: if(!fnQueueEmpty(iFront, iRear))
{
printf("nContents of the Queue is n");
fnDisplay(myQueue, iFront, iRear);
}
else
{
printf("nQueue is Emptyn");
}
break;
case 4: exit(0);
break;
}
}
return 0;
}
queue[*r] = cVal;
}
if(*f == *r)
{
*f = -1;
*r = -1;
}
else
{
*f = (*f + 1)%QUEUE_SIZE;
}
return cElem;
}
Output
putta:~/.../Programs$ ./a.out
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
2
Queue is Empty
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
1
Enter an element : I
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
1
Enter an element : N
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
1
Enter an element : D
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
1
Enter an element : I
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
1
Enter an element : A
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
3
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
1
Queue is Full
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
2
Deleted element is I
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
2
Deleted element is N
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
2
Deleted element is D
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
2
Deleted element is I
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
2
Deleted element is A
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
2
Queue is Empty
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
3
Queue is Empty
Queue Operations
=====================
1.Qinsert
2.Qdelete
3.Qdisplay
4.Exit
Enter your choice
4
Program 07 : Singly Linked List of Student Data
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
C Code
/***************************************************************************
*File : 07_SLL.c
*Description: SLL of N Students Data
***************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
char cUSN[11], cName[40], cProgram[4];
int iSem;
char cPhNo[11];
struct node *link;
};
NODEPTR fnGetNode(void);
void fnFreeNode(NODEPTR);
NODEPTR fnInsRear(NODEPTR);
NODEPTR fnDelFront(NODEPTR);
NODEPTR fnInsFront(NODEPTR);
NODEPTR fnDelRear(NODEPTR);
void fnDisplay(NODEPTR);
int main()
{
NODEPTR first = NULL;
int iChoice, iNum, i;
switch(iChoice)
{
case 1:
first = fnInsFront(first);
break;
case 2:
first = fnInsRear(first);
break;
case 5: fnDisplay(first);
break;
case 6: exit(0);
}
}
return 0;
}
NODEPTR fnGetNode()
{
NODEPTR newborn;
newborn = (NODEPTR)malloc(sizeof(struct node));
if(newborn == NULL)
{
printf("nMemory Overflow");
exit(0);
}
void fnFreeNode(NODEPTR x)
{
free(x);
}
temp = fnGetNode();
temp->link = NULL;
if(first == NULL)
return temp;
cur = first;
while(cur->link != NULL)
{
cur = cur->link;
}
cur->link = temp;
return first;
}
temp = fnGetNode();
temp->link = NULL;
temp->link = first;
first = temp;
return first;
prev = NULL;
cur = first;
if(cur->link == NULL)
{
printf("nNode deleted for %sn",cur->cName);
fnFreeNode(cur);
return NULL;
}
while(cur->link != NULL)
{
prev = cur;
cur = cur->link;
}
prev->link = cur->link;
printf("nNode deleted for %sn",cur->cName);
fnFreeNode(cur);
return first;
}
/*CPP*/
Output
Enter semester : 3
Enter semester : 1
QUEUE OPERATIONS
====================
1.Insert Front
2.Insert Rear
3.Delete Front
4.Delete Rear
5.Display
6.Exit
QUEUE OPERATIONS
====================
1.Insert Front
2.Insert Rear
3.Delete Front
4.Delete Rear
5.Display
6.Exit
Enter semester : 3
QUEUE OPERATIONS
====================
1.Insert Front
2.Insert Rear
3.Delete Front
4.Delete Rear
5.Display
6.Exit
Enter semester : 1
Enter Phone no : 8712658824
QUEUE OPERATIONS
====================
1.Insert Front
2.Insert Rear
3.Delete Front
4.Delete Rear
5.Display
6.Exit
QUEUE OPERATIONS
====================
1.Insert Front
2.Insert Rear
3.Delete Front
4.Delete Rear
5.Display
6.Exit
QUEUE OPERATIONS
====================
1.Insert Front
2.Insert Rear
3.Delete Front
4.Delete Rear
5.Display
6.Exit
QUEUE OPERATIONS
====================
1.Insert Front
2.Insert Rear
3.Delete Front
4.Delete Rear
5.Display
6.Exit
QUEUE OPERATIONS
====================
1.Insert Front
2.Insert Rear
3.Delete Front
4.Delete Rear
5.Display
6.Exit
QUEUE OPERATIONS
====================
1.Insert Front
2.Insert Rear
3.Delete Front
4.Delete Rear
5.Display
6.Exit
QUEUE OPERATIONS
====================
1.Insert Front
2.Insert Rear
3.Delete Front
4.Delete Rear
5.Display
6.Exit
SLL is empty
QUEUE OPERATIONS
====================
1.Insert Front
2.Insert Rear
3.Delete Front
4.Delete Rear
5.Display
6.Exit
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int iSSN;
char cName[30], cDept[4], cDesignation[30], cPhNo[11];
int iSalary;
struct node *plink;
struct node *nlink;
};
NODEPTR fnGetNode(void);
void fnFreeNode(NODEPTR);
NODEPTR fnInsRear(NODEPTR);
NODEPTR fnDelFront(NODEPTR);
NODEPTR fnInsFront(NODEPTR);
NODEPTR fnDelRear(NODEPTR);
void fnDisplay(NODEPTR);
int main()
{
NODEPTR first = NULL;
int iChoice, iNum, i;
switch(iChoice)
{
case 1: first = fnInsRear(first);
break;
case 5: fnDisplay(first);
break;
case 6: exit(0);
}
}
return 0;
}
NODEPTR fnGetNode()
{
NODEPTR newborn;
if(newborn == NULL)
{
printf("nMemory Overflow");
exit(0);
}
printf("nEnter SSN : ");
scanf("%d",&newborn->iSSN);
printf("nEnter name : ");
scanf("%s",newborn->cName);
printf("nEnter Department : ");
scanf("%s", newborn->cDept);
printf("nEnter Designation : ");
scanf("%s", newborn->cDesignation);
printf("nEnter Salary : ");
scanf("%d",&newborn->iSalary);
printf("nEnter Phone no : ");
scanf("%s",newborn->cPhNo);
return newborn;
}
void fnFreeNode(NODEPTR x)
{
free(x);
}
temp = fnGetNode();
if(first == NULL)
return temp;
cur = first;
while(cur->nlink != NULL)
{
cur = cur->nlink;
}
cur->nlink = temp;
temp->plink = cur;
return first;
}
temp = fnGetNode();
temp->plink = temp->nlink = NULL;
temp->nlink = first;
first = temp;
return first;
cur = first;
if(cur->nlink == NULL)
{
printf("nNode deleted for %sn",cur->cName);
fnFreeNode(cur);
return NULL;
}
while(cur->nlink != NULL)
{
cur = cur->nlink;
}
prev = cur->plink;
prev->nlink = NULL;
printf("nNode deleted for %sn",cur->cName);
fnFreeNode(cur);
return first;
}
/*CPP*/
Output
putta:~/.../Programs$ gcc -Wall 08_DLL.c
putta:~/.../Programs$ ./a.out
DLL OPERATIONS
====================
1.Insert Rear
2.Delete Front
3.Insert Front
4.Delete Rear
5.Display
6.Exit
DLL OPERATIONS
====================
1.Insert Rear
2.Delete Front
3.Insert Front
4.Delete Rear
5.Display
6.Exit
DLL OPERATIONS
====================
1.Insert Rear
2.Delete Front
3.Insert Front
4.Delete Rear
5.Display
6.Exit
DLL OPERATIONS
====================
1.Insert Rear
2.Delete Front
3.Insert Front
4.Delete Rear
5.Display
6.Exit
DLL OPERATIONS
====================
1.Insert Rear
2.Delete Front
3.Insert Front
4.Delete Rear
5.Display
6.Exit
DLL OPERATIONS
====================
1.Insert Rear
2.Delete Front
3.Insert Front
4.Delete Rear
5.Display
6.Exit
DLL OPERATIONS
====================
1.Insert Rear
2.Delete Front
3.Insert Front
4.Delete Rear
5.Display
6.Exit
DLL OPERATIONS
====================
1.Insert Rear
2.Delete Front
3.Insert Front
4.Delete Rear
5.Display
6.Exit
Enter your choice
2
DLL OPERATIONS
====================
1.Insert Rear
2.Delete Front
3.Insert Front
4.Delete Rear
5.Display
6.Exit
DLL OPERATIONS
====================
1.Insert Rear
2.Delete Front
3.Insert Front
4.Delete Rear
5.Display
6.Exit
DLL OPERATIONS
====================
1.Insert Rear
2.Delete Front
3.Insert Front
4.Delete Rear
5.Display
6.Exit
DLL is empty
DLL OPERATIONS
====================
1.Insert Rear
2.Delete Front
3.Insert Front
4.Delete Rear
5.Display
6.Exit
DLL is empty
DLL OPERATIONS
====================
1.Insert Rear
2.Delete Front
3.Insert Front
4.Delete Rear
5.Display
6.Exit
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
POLYPTR fnInsertTerm(POLYPTR poly, int coef, int pow_x, int pow_y, int pow_z)
{
POLYPTR cur;
POLYPTR newNode = (POLYPTR)malloc(sizeof(struct PolyTerm));
newNode->coefficient = coef;
newNode->pow_x = pow_x;
newNode->pow_y = pow_y;
newNode->pow_z = pow_z;
newNode->next = NULL;
cur = poly;
while(cur->next != poly)
{
cur = cur->next;
}
cur->next = newNode;
newNode->next = poly;
return poly;
}
result += termValue;
cur = cur->next;
} while (cur != poly);
return result;
}
do
{
polySum = fnInsertTerm(polySum, cur1->coefficient, cur1->pow_x, cur1->pow_y, cur1-
>pow_z);
cur1 = cur1->next;
}while(cur1 != poly1);
do
{
cur1 = polySum->next;
bool bMatchFound = false;
do
{
if(fnMatchTerm(cur1, cur2))
{
cur1->coefficient += cur2->coefficient;
bMatchFound = true;
break;
}
cur1 = cur1->next;
}while(cur1 != polySum);
if(!bMatchFound)
{
polySum = fnInsertTerm(polySum, cur2->coefficient, cur2->pow_x, cur2->pow_y, cur2-
>pow_z);
}
cur2 = cur2->next;
}while(cur2 != poly2);
return polySum;
int main()
{
POLYPTR poly1 = (POLYPTR)malloc(sizeof(struct PolyTerm));
poly1->next = poly1;
POLYPTR poly2 = (POLYPTR)malloc(sizeof(struct PolyTerm));
poly2->next = poly2;
POLYPTR polySum = (POLYPTR)malloc(sizeof(struct PolyTerm));
polySum->next = polySum;
// Represent and evaluate the polynomial P(x, y, z) = 6x^2y^2z - 4yz^5 + 3x^3yz + 2xy^5z - 2xyz^3
poly1 = fnInsertTerm(poly1, 6, 2, 2, 1);
poly1 = fnInsertTerm(poly1, 4, 0, 1, 5);
poly1 = fnInsertTerm(poly1, 3, 3, 1, 1);
poly1 = fnInsertTerm(poly1, 2, 1, 5, 1);
poly1 = fnInsertTerm(poly1, 2, 1, 1, 3);
return 0;
}
Output
putta:~/.../Programs$ gcc -Wall 09_Polynomial.c -lm
putta:~/.../Programs$ ./a.out
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *lbranch;
struct node *rbranch;
};
typedef struct node* NODEPTR;
/* FUNCTION PROTOTYPES */
NODEPTR fnGetNode(void);
void fnFreeNode(NODEPTR x);
NODEPTR fnInsertNode(int, NODEPTR);
void fnInOrder(NODEPTR);
void fnPreOrder(NODEPTR);
void fnPostOrder(NODEPTR);
void fnSearchBST(NODEPTR, int);
int main()
{
NODEPTR root = NULL;
int iChoice, iItem, i, iNum;
for(;;)
{
printf("n1.Inorder traversaln2.Preorder traversal");
printf("n3.Postorder traversaln4.Searchn5.Exitn");
printf("nEnter your choice : ");
scanf("%d",&iChoice);
switch(iChoice)
{
case 1: if(root ==NULL)
{
printf("nTree is Emptyn");
}
else
{
printf("nInorder Traversal is :n");
fnInOrder(root);
printf("n");
}
break;
case 5: exit(0);
}
return 0;
}
NODEPTR fnGetNode(void)
{
NODEPTR x;
x = ( NODEPTR ) malloc (sizeof(struct node));
if(x == NULL)
{
printf("nOut of Memory");
exit(0);
}
return x;
}
void fnFreeNode(NODEPTR x)
{
free(x);
}
temp = fnGetNode();
temp->info = iItem;
temp->lbranch = NULL;
temp->rbranch = NULL;
if(root == NULL)
return temp;
prev = NULL;
cur = root;
while(cur != NULL)
{
prev = cur;
if(iItem == cur->info)
{
printf("nDuplicate items not allowedn");
fnFreeNode(temp);
return root;
}
return root;
Enter 6 numbers
50
30
70
40
60
20
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.Search
5.Exit
Inorder Traversal is :
20 30 40 50 60 70
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.Search
5.Exit
Preorder Traversal is :
50 30 20 40 70 60
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.Search
5.Exit
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.Search
5.Exit
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.Search
5.Exit
1.Inorder traversal
2.Preorder traversal
3.Postorder traversal
4.Search
5.Exit
#include <stdio.h>
#include <stdio.h>
/******************************************************************************
*Function : main
*Input parameters: no parameters
*RETURNS : 0 on success
******************************************************************************/
int main(void)
{
int graph[MAX][MAX];
int visited[MAX];
int numVert, startVert, i,j;
/******************************************************************************
*Function : fnBreadthFirstSearchReach
*Description : Function to perform BFS traversal and mark visited vertices
*Input parameters:
* int vertex - source vertex
* int g[][] - adjacency matrix of the graph
* int v[] - vector to store visited information
* int n - no of vertices
*RETURNS : void
******************************************************************************/
void fnBreadthFirstSearchReach(int vertex, int g[MAX][MAX], int v[MAX], int n)
{
QUEUE stQueue;
stQueue.iFront = 0;
stQueue.iRear = -1;
int frontVertex, i;
v[vertex] = 1;
fnQInsert(&stQueue, vertex);
while (!fnQEmpty(&stQueue))
{
frontVertex = fnQDelete(&stQueue);
for (i=0; i<n; i++)
{
if (g[frontVertex][i] && !v[i])
{
v[i] = 1;
fnQInsert(&stQueue, i);
}
}
}
}
/***************************************************************************
*Function : fnQInsert
*Description: inserts an element at the rear of the queue
*Input parameters: a structure queue
*RETURNS : updated queue
***************************************************************************/
/***************************************************************************
*Function : fnQDelete
*Description: deletes an element from the front of the queue
*Input parameters: a structure queue
*RETURNS : updated queue
***************************************************************************/
/***************************************************************************
*Function : fnQFull
*Description: checks wheteher the queue is full or not
*Input parameters: a structure queue
*RETURNS : 1 if the queue is full or 0 otherwise
***************************************************************************/
int fnQFull(QUEUE *stQueue)
{
if(stQueue->iRear == SIZE-1)
return 1;
else
return 0;
}
/***************************************************************************
*Function : fnQEmpty
*Description: checks wheteher the queue is empty or not
*Input parameters: a structure queue
*RETURNS : 1 if the queue is empty or 0 otherwise
***************************************************************************/
int fnQEmpty(QUEUE *stQueue)
{
if(stQueue->iRear == stQueue->iFront-1)
return 1;
else
return 0;
}
Output
putta:~/.../Programs$ gcc -Wall 11_GraphBFS.c
putta:~/.../Programs$ ./a.out
Enter the number of vertices : 4
Enter the adjacency matrix :
0100
1000
0001
0010
Enter the starting vertex : 1
Vertices which can be reached from vertex 1 are :-
12
putta:~/.../Programs$ ./a.out
Enter the number of vertices : 4
Enter the adjacency matrix :
0110
1001
1001
0110
Enter the starting vertex : 1
Vertices which can be reached from vertex 1 are :-
1234
putta:~/.../Programs$ ./a.out
Enter the number of vertices : 4
Enter the adjacency matrix :
0100
1000
0001
0010
Enter the starting vertex : 3
Vertices which can be reached from vertex 3 are :-
34
C Code (DFS method)
/***************************************************************************
*File : 11_GraphDFS.c
*Description: Program to find all nodes reachable from a given node using DFS
***************************************************************************/
#include <stdio.h>
const int MAX = 100;
void fnDepthFirstSearch(int currentVertex, int v[MAX], int g[MAX][MAX], int n);
/******************************************************************************
*Function: main
*Input parameters: no parameters
*RETURNS: 0 on success
******************************************************************************/
int main(void)
{
int i,j,k;
int visited[MAX];
int graph[MAX][MAX];
int numVert, Vert;
printf("Enter the number of vertices : ");
scanf("%d", &numVert);
for (i=0; i<numVert; i++)
visited[i] = 0;
printf("Enter the adjacency matrix :n");
for (i=0; i<numVert; i++)
for (j=0; j<numVert; j++)
scanf("%d", &graph[i][j]);
printf("Enter the source vertex : ");
scanf("%d", &Vert);
fnDepthFirstSearch(Vert,visited,graph,numVert);
for (k=0; k<numVert; k++)
{
if(visited[k])
{
printf("nVertex %d is reachablen", k+1);
}
else
{
printf("nVertex %d is not reachablen", k+1);
}
}
return 0;
}
/******************************************************************************
*Function: fnDepthFirstSearch
*Description: Function to perform DFS traversal and mark visited vertices
*Input parameters:
* int currentVertex - source vertex
* int v[] - vector to store visited information
* int g[][] - adjacency matrix of the graph
* int n - no of vertices
*RETURNS: void
******************************************************************************/
void fnDepthFirstSearch(int currentVertex, int v[MAX], int g[MAX][MAX], int n)
{
int i;
v[currentVertex] = 1;
for (i=0; i<n; i++)
{
if (g[currentVertex][i] && !v[i])
fnDepthFirstSearch(i,v,g,n);
}
}
Output
putta:~/.../Programs$ gcc -Wall 11_GraphDFS.c
putta:~/.../Programs$ ./a.out
Enter the number of vertices : 4
Enter the adjacency matrix :
0100
1000
0001
0010
Enter the source vertex : 3
Vertex 3 is reachable
Vertex 4 is reachable
putta:~/.../Programs$ ./a.out
Enter the number of vertices : 4
Enter the adjacency matrix :
0110
1001
1001
0110
Enter the source vertex : 1
Vertex 1 is reachable
Vertex 2 is reachable
Vertex 3 is reachable
Vertex 4 is reachable
putta:~/.../Programs$ ./a.out
Enter the number of vertices : 4
Enter the adjacency matrix :
0100
1000
0001
0010
Enter the source vertex : 1
Vertex 1 is reachable
Vertex 2 is reachable
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int m; // Size of the hash table
printf("Enter the size of the hash table (m): ");
scanf("%d", &m);
int n = 0;
EMPLOYEE emp;
while(fscanf(file, "%d %s", &emp.iKey, emp.cName) != EOF)
{
EMPLOYEE* newEmp = (EMPLOYEE*)malloc(sizeof(EMPLOYEE));
newEmp->iKey = emp.iKey;
strcpy(newEmp->cName, emp.cName);
fnInsRecord(newEmp, m);
n++;
}
fclose(file);
int iSrchKey;
printf("Enter a key to search for an employee record: ");
scanf("%d", &iSrchKey);
return 0;
}
stHashTable[index] = emp;
}
// Linear probing
while(stHashTable[index] != NULL)
{
if(stHashTable[index]->iKey == iKey)
{
return stHashTable[index];
}
index = (index + 1) % m;
}
return NULL; // Employee record not found
}
putta:~/.../Programs$ ./a.out
Enter the size of the hash table (m): 50
Enter a key to search for an employee record: 5216
Employee found with key 5216:
Name: sanjay
putta:~/.../Programs$ ./a.out
Enter the size of the hash table (m): 50
Enter a key to search for an employee record: 4327
Employee found with key 4327:
Name: nahar