Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

c Programming Lab Manual

Uploaded by

keerthi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

c Programming Lab Manual

Uploaded by

keerthi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 62

DEPARTMENT OF

ELECTRICAL AND ELECTRONIC ENGINEERING

ACADEMIC YEAR: 2024-2025

CS3362 – C PROGRAMMING & DATA


STRUCTURESLABORATORY

NAME: ___________________________________________________________________

ROLL NO:__________________________________________________________________

BRANCH:__________________ SEM:______________________________________
Certified that this is the bonafide record of work done

by Mr./Ms….…………………………………………………………………..in the

CS3362 – C PROGRAMMING & DATA STRUCTURESLABORATORY of

this institution, as prescribed by the Anna University, Chennai for the THIRD

Semester Electrical And Electronic Engineering, during the year 2024-2025.

Staff - In charge Head of the Department

Submitted for the University Practical Examination held on ……………………at


Sree Sakthi Engineering College, Coimbatore – 641 104.

Internal Examiner External Examiner


lOMoARcPSD|26268675

LIST OF EXPERIMENTS

S.NO DATE NAME OF THE PAGE MARKS STAFF


EXPERIMENT NO SIGN
1 Practice of C programming using
statements, expressions, decision
making and iterative
2 Practice of C programming using
Functions and Arrays
3 Implement C programs using
Pointers and Structures
4 Implement C programs using Files

5 Development of real time C


applications
6 Array implementation of List
ADT
7 Array implementation of Stack
and Queue ADTs
8 Linked list implementation of List,
Stack and Queue ADTs
9 Applications of List, Stack and
Queue ADTs
10 Implementation of Binary Trees
and operations of Binary Trees
11 Implementation of Binary Search
Trees
12 Implementation of searching
techniques
13 Implementation of Sorting
algorithms : Insertion Sort, Quick
Sort, Merge Sort
14 Implementation of Hashing – any
two collision techniques

3
lOMoARcPSD|26268675

1.(A) C PROGRAM USING STATEMENTS

AIM:

To write the C program using statements.

ALGORITHM:
Step 1: Open the Turbo C.
Step 2: Add the header file.
Step3: Input the main
Step4: Input the value as you needed.
Step5: Use if condition.
Step 6: Print the condition using 'printf'
Step7 :Close the program.

PROGRAM:

#include<stdio.h>
void main()
{
int num;
printf("enter the number");
scanf("%d",&num);
if(num<0)
printf("the number is negative");
else
printf("the number is positive");
}

OUTPUT:

RESULT:
Thus, the C program using the statements is executed successfully.

4
lOMoARcPSD|26268675

1.(B) C PROGRAM USING EXPRESSIONS

AIM:
To write the C program using expressions.
ALGORITHM:
Step 1: Open the Turbo C.
Step 2: Add the header file.
Step 3: Input the main
Step 4: Input the value as you needed.
Step 5: Print the condition using 'printf'
Step 6: Scan the condition using 'scanf'.
Step7: Calculate the value using expression Operation.
Step 8: The output will be displayed based on the calculation.
Step 9: Close the program.

PROGRAM:
#include <stdio.h>
int main()
{
int a,b,result;
int c=a;
printf("Enter 2 numbers for Arithmetic operation \n");
scanf("%d\n%d",&a,&b);
result = a+b;
printf("==============ARITHMETICEXPRESSIONS=======\n");
printf("Addition of %d and %d is = %d \n",a,b,result);
result = a-b;
printf("Subtraction of %d and %d is = %d \n",a,b,result);
result = a*b;
printf("Multiplication of %d and %d is = %d \n",a,b,result);
result = a/b;
printf("Division of %d and %d is = %d \n",a,b,result);
printf("==========================================");
return 0;
}
OUTPUT

RESULT:

5
lOMoARcPSD|26268675

Thus, the C program using the expressions is executed successfully.

1.(C)CPROGRAM USING DECISION MAKING

AIM:
To write the C program using decision making.

ALGORITHM:
Step 1: Open the Turbo C.
Step 2: Add the header file.
Step3: Input the main
Step4: Input the value as you needed.
Step5: Use if condition.And writes the expressions for the input values.
Step 6: Print the condition using 'printf'
Step7:Scantheconditionusing'scanf'.
Step8: Close the program.

PROGRAM:
#include<stdio.h>
int main()
{
int n,i;
unsigned long long fact=1;
printf("enter an integer:");
scanf("%d",&n);
if(n<0)
printf("error! factorial of a negative number does not exist.");
else
{
for(i=1;i<=n;i++)
{
fact*=i;
}
printf("factorial of %d=%llu",n,fact);
}
return 0;
}

OUTPUT:

RESULT:
Thus, the C program using the decision making is executed successfully
6
lOMoARcPSD|26268675

1.(D)CPROGRAM USING ITERATIVE STATEMENTS

AIM:
To write the C program using iterative statements.

ALGORITHM:
Step 1: Open the Turbo C.
Step 2: Add the header file.
Step3: Input the main
Step4: Input the value as you needed.
Step5: Use for loop condition.And write the expressions for the input values.
Step 6: Print the condition using 'printf'
Step7: Scan the condition using 'scanf'.
Step8: Close the program.

PROGRAM:
// Print numbers from 1 to 10#include<stdio.h>
int main() {int i;
for(i =1;i < 11;++i)
{
printf("%d ",i);
}
return 0;
}

OUTPUT:

RESULT:
Thus,the C program using the iterative statement is executed successfully.

7
lOMoARcPSD|26268675

2.C PROGRAM USING FUNCTIONS

AIM:
To write the C program using functions.

ALGORITHM:
Step 1: Open the Turbo C.
Step 2: Add the header file.
Step3: Input the main
Step4: Input the value as you needed.
Step5:Use for loop condition.And write the expressions for the input values.
Step 6: Print the condition using 'printf'.
Step 7: Scan the condition using 'scanf'.
Step8:Close the program.

PROGRAM:
#include <stdio.h>
float findAverage(int [], int);
int main(void)
{
int score[5] = {60, 50, 40, 30, 25};
int papers = 5;
float avg = findAverage(score, papers);
printf("Average: %f\n", avg);
return 0;
}
float findAverage(int arr[], int size)
{
// variables
int
i;
float
sum = 0,
avg = 0;
// find total
for (i = 0; i < size; i++)
{
sum += arr[i];
}
// find average
avg = sum / size;
// return average
return avg;
}

8
lOMoARcPSD|26268675

OUTPUT:

RESULT:

Thus,the C program using the functions is executed successfully.

9
lOMoARcPSD|26268675

3.C PROGRAM USING POINTERS

AIM:
To write the C program using pointers.

ALGORITHM:
Step 1: Open the Turbo C.
Step 2: Add the header file.
Step3:Input the main
Step4:Input the value as you needed.
Step5:Useforloopcondition.And write the expressions for the input values.
Step6:Printtheconditionusing'printf'.
Step7:Scan the condition using 'scanf'.
Step8:Close the program.

PROGRAM:

#include <stdio.h>
int main(void)
{
// student structure
struct student {
char id[15];
char firstname[64];
char lastname[64];
float points;
};

// student structure variable


struct student std;

// student structure pointer variable


struct student *ptr = NULL;

// assign std to ptr


ptr = &std;

// get student detail from user


printf("Enter ID: ");
scanf("%s", ptr->id);
printf("Enter first name: ");
scanf("%s", ptr->firstname);
printf("Enter last name: ");
scanf("%s", ptr->lastname);
printf("Enter Points: ");
scanf("%f", &ptr->points);

// display result via std variable


10
lOMoARcPSD|26268675

printf("\nResult via std\n");


printf("ID: %s\n", std.id);
printf("First Name: %s\n", std.firstname);
printf("Last Name: %s\n", std.lastname);
printf("Points: %f\n", std.points);

// display result via ptr variable


printf("\nResult via ptr\n");
printf("ID: %s\n", ptr->id);
printf("First Name: %s\n", ptr->firstname);
printf("Last Name: %s\n", ptr->lastname);
printf("Points: %f\n", ptr->points);

return 0;
}

OUTPUT

RESULT:
Thus, the C program using the pointers is executed successfully.

11
lOMoARcPSD|26268675

4.C PROGRAM USING FILES

AIM:
Towrite the C program using files.

ALGORITHM:
Step 1: Open the Turbo C.
Step 2: Add the header file.
Step3:Input the main
Step4:Input the value as you needed.
Step5: Create a file at a location.
Step6:Use the file operations for the opening and reading,writingfiles.
Step7:Print the condition using“printf”.
Step8:Close the program.

PROGRAM
#include <stdio.h>
#include <stdlib.h>
#define DATA_SIZE 1000
int main()
{
char data[DATA_SIZE];
FILE * fptr;
fptr=fopen("file1.txt","w");
if(fptr==NULL)
{
printf("Unable to create file.\n");
exit(EXIT_FAILURE);
}
printf("Enter contents to store in file : \n");
fgets(data,DATA_SIZE,stdin);
fputs(data,fptr);
fclose(fptr);
printf("File created and saved successfully.:)\n");
return 0;
}

12
lOMoARcPSD|26268675

OUTPUT:

RESULT:
Thus,the C program using the files is executed successfully.

13
lOMoARcPSD|26268675

5.DEVELOPMENT OF REAL TIME C APPLICATIONS

C PROGRAM USING INTERNAL MARKS

AIM:
To write the C program using internal marks.

ALGORITHM:
Step 1: Open the Turbo C.
Step 2: Add the header file.
Step3:Input the main
Step4:Calculate total,percentage & average of the student.
Step5:Use if & if else condition.And write the expressions for the input values.
Step6:Print the condition using'printf'.
Step 7: Scan the condition using 'scanf'.
Step8:Close the program.

PROGRAM

#include <stdio.h>
int main()
{
float subject_1,subject_2,subject_3,subject_4,subject_5;
float total,average,percentage;
char grade;
printf("Enter the marks of five subjects:\n");
scanf("%f%f%f%f%f",&subject_1,&subject_2,&subject_3,&subject_4,&subject_5);

//ItwillcalculatetheTotal,AverageandPercentage
total = subject_1 + subject_2 + subject_3 +subject_4+subject_5;
average=total/5.0;
percentage=(total/500.0)*100;

// It will calculate theGrade


if (average >=90)
grade='A';
else if (average >= 80 &&average<90)grade='B';
else if (average >= 70 &&average<80)grade='C';
else if (average >= 60 &&average<70)grade='D';
else
grade= 'E';

// It will produce the final OUTPUT


printf("\nThe Total marks is: \t%.2f /500.00\n",total);
printf("\nThe Average marks is:\t%.2f\n", average);
printf("\nThe Percentage is: \t%.2f%%\n", percentage);
printf("\nThe Gradeis: \t'%c'\n", grade);
14
lOMoARcPSD|26268675

return 0;
}

OUTPUT:

RESULT:
Thus, the C program using the internal marks is executed successfully.

15
lOMoARcPSD|26268675

6.ARRAY IMPLEMENTATION OF LIST ADT

AIM
To write a C program to implement List ADT using array.

ALGORITHM
Step1: Start the program.
Step2:For Push operation,check for stack overflow.
Step3:If Top>=N then print stack over flow else increment Top and insert the element.
Step4:For Pop operation, check for under flow of the stack.
Step5:If Top=0 then print stack under flow else decrement Top and delete the element.
Step 6: Stop the program.

PROGRAM
#include<stdio.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
int a,b[20], n, p, e, f, i, pos;
int main()
{
//clrscr();
int ch;
char g='y';
do
{
printf("\n main Menu");
printf("\n 1.Create \n 2.Delete \n 3.Search \n 4.Insert \n 5.Display\n 6.Exit \n");
printf("\n Enter your Choice");
scanf("%d", &ch);
switch(ch)
{
case 1:
create();

break;
case 2:
deletion();
break;
case 3:
search();
break;
case 4:
insert();
break;
case 5:
lOMoARcPSD|26268675

display();
break;
case 6:
break;
default:
printf("\n Enter the correct choice:");
}
printf("\n Do u want to continue:::");
scanf("\n%c", &g);
}
while(g=='y'||g=='Y');
}
void create()
{
printf("\n Enter the number of nodes");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n Enter the Element:",i+1);
scanf("%d", &b[i]);
}
}
void deletion()
{
printf("\n Enter the position u want to delete::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n Invalid Location::");

}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n--;
}
printf("\n The Elements after deletion");
for(i=0;i<n;i++)
{
printf("\t%d", b[i]);
}
}
void search()
{
printf("\n Enter the Element to be searched:");
scanf("%d", &e);
for(i=0;i<n;i++)
{
lOMoARcPSD|26268675

if(b[i]==e)
{
printf("Value is in the %d Position", i);
}
else
{
printf("Value %d is not in the list::", e);
continue;
}
}
}
void insert()
{
printf("\n Enter the position u need to insert::");
scanf("%d", &pos);
if(pos>=n)
{
printf("\n invalid Location::");
}

else
{
for(i=MAX-1;i>=pos-1;i--)
{
b[i+1]=b[i];
}
printf("\n Enter the element to insert::\n");
scanf("%d",&p);
b[pos]=p;
n++;
}
printf("\n The list after insertion::\n");
display();
}
void display()
{
printf("\n The Elements of The list ADT are:");
for(i=0;i<n;i++)
{
printf("\n\n%d", b[i]);
}
}
lOMoARcPSD|26268675

OUTPUT

RESULT
Thus the C program for stack ADT using array was implemented and executed successfully.
lOMoARcPSD|26268675

7(a).ARRAY IMPLEMENTATION OF STACK ADTs

AIM
To write a C program to implement Stack ADT using array.

ALGORITHM
Step1: Start the program.
Step2:For queue insert i on operation, check for queue overflow.
Step3:If Rear>=N then print queue overflow
Else increment rear pointer and insert the element.
Step4:For queue deletion operation,check for under flow of the queue.
Step5:If Front=0 then print queue underflow
else delete the element and increment the front pointer.
Step6:Stop the program.

PROGRAM
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}

case 2:
{
pop();
break;
}
case 3:
{
display();
break;
lOMoARcPSD|26268675

}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push()
{
if(top>=n-1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)

{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
lOMoARcPSD|26268675

{
printf("\n The STACK is empty");
}
}

OUTPUT

RESULT
Thus the C program for stack ADT using array was implemented and executed
Successfully.
lOMoARcPSD|26268675

7(b).ARRAY IMPLEMENTATIONOF QUEUE ADTs

AIM
To write a C program to implement Queue ADT using array.

ALGORITHM
Step1: Start the program.
Step2:For queue insert it on operation,check for queueoverflow.
Step3:If Rear>=N then print queue overflow
Else increment rear pointer and insert the element.
Step4:For queue deletion operation,check for under flow of the queue.
Step5:If Front=0 then print queue underflow else delete the element and increment the front pointer.
Step6:Stop the program.

PROGRAM
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
int main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
lOMoARcPSD|26268675

break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choice \n");
} /* End of switch */
return 0;
} /* End of while */
} /* End of main() */
void insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */
void delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */
void display()
{
lOMoARcPSD|26268675

int i;
if (front == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /* End of display() */

OUTPUT:

RESULT
Thus the C program for Queue ADT using array was implemented and executed
Successfully.
lOMoARcPSD|26268675

8. LINKED LIST IMPLEMENTATION OF LIST, STACK AND QUEUE ADTS


A.IMPLEMENTATION OF SINGLY LINKED LIST

AIM
To writea C program for singly linked list using list.

ALGORITHM
Step1:Start the program
Step2: Get the values for insertion into the list
Step3:Get the position of the value to be deleted
Step4: Display the values entered into the list
Step5: Display the number of items in the list
Step6:Stop the program

PROGRAM
#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
struct node *head = NULL;
struct node
{
int data;
struct node* next;
};
int main()
{
int choice;
while(1)
{
printf("\n*****\n");
printf("0. Create\n");
printf("1. display\n");
printf("2. Insert Node at beginning\n");
printf("3. Insert Node in specific position\n");
printf("4. Insert Node at end of LinkedList\n");
printf("5. Delete Node at beginning\n");
printf("6. Delete Node at end\n");
printf("7. Delete Node at position\n");
lOMoARcPSD|26268675

printf("8. ** To exit **");


printf("\n Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 0: create();
break;
case 1: display();
break;
case 2: insert_begin();
break;
case 3: insert_pos();
break;
case 4: insert_end();
break;
case 5: delete_begin();
break;
case 6: delete_end();
break;
case 7: delete_pos();
break;
case 8: exit(0);
default:printf("\n Wrong Choice");
break;
}
}
}
//creates a node
void create()
{
struct node *temp,*n;
int i,num;
printf("enter the number of nodes");
scanf("%d",&num);
//creating new node
head = (struct node*)malloc(sizeof(struct node));
scanf("%d",&head->data);
head->next = NULL;
temp=head;
for(i=1;i<num;i++)
{
n = (struct node*)malloc(sizeof(struct node));
scanf("%d",&n->data);
n->next = NULL;
temp->next=n;
temp=temp->next;
lOMoARcPSD|26268675

}
}
// prints the entire LinkedList
void display()
{
if(head==NULL)
{
printf("Linked List is Empty\n");
return;
}
printf("LinkedList: ");
struct node* ptr = head;
while(ptr!=NULL) // start from first node
{
printf("%d ",ptr->data);
ptr = ptr->next;
}
printf("\n");
}
// to insert node at start of LinkedList
void insert_begin()
{
struct node* temp;
// creating a new node
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL)
{
head = temp;
return;
}
else
{
temp->next = head; //point it to old head node
head = temp; //point head to new first node
}
}
// to insert node at given position
void insert_pos()
{
struct node* temp;
int i;
// creating a new node
temp = (struct node*)malloc(sizeof(struct node));
lOMoARcPSD|26268675

printf("Enter node data: ");


scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL) // if list empty we return
{
head = temp;
return;
}
else
{
struct node* prev_ptr;
struct node* ptr = head;
int pos;
printf("Enter position: ");
scanf("%d",&pos);
for(i=0;i<pos;i++)
{
prev_ptr = ptr;
ptr = ptr->next;
}
//new node pointing to node in that pos
temp->next = ptr;
//prevptr pointing to new node
prev_ptr->next = temp;
}
}
// to insert node at end of LinkedList
void insert_end()
{
struct node* temp;
//creating new node
temp = (struct node*)malloc(sizeof(struct node));
printf("Enter node data: ");
scanf("%d",&temp->data);
temp->next = NULL;
if(head==NULL)
{
head = temp; //if list is empty, we return
return;

}
else{
struct node* ptr = head;
while(ptr->next!=NULL)
{
lOMoARcPSD|26268675

ptr = ptr->next;
}
// tail node pointing to new node
ptr->next = temp;
}
}
// to delete first node of LinkedList
void delete_begin()
{
if(head==NULL) //if List is empty we return
{
printf("Linked List is empty | Nothing to delete \n");
return;
}
else
{
struct node* ptr = head;
head = head->next; // head node pointing to second node
free(ptr); // deleting prev head node
printf("Node Deleted \n");
}
}
// to delete last node of LinkedList
void delete_end()
{
if(head==NULL) //if List is empty we return
{
printf("Linked List is empty | Nothing to delete \n");
return;
}
else if(head->next==NULL)
{
struct node* ptr = head;
head = ptr->next;
free(ptr);
}
else
{
struct node* ptr = head;
struct node* prev_ptr = NULL;
while(ptr->next!=NULL)// traverse till last but one node
{
prev_ptr = ptr;
ptr = ptr->next;
}
lOMoARcPSD|26268675

prev_ptr->next = NULL; // next field of last but one field is made as NULL
free(ptr); // deleting last node
}
}
// to delete node at given position
void delete_pos()
{
int pos,i;
printf("Enter node position to delete: ");
scanf("%d",&pos);
struct node* ptr=head;
if(head==NULL) //we return if List is empty
{
printf("Linked List is empty \n");
return;
}
else if(pos == 0)
{
ptr = head;
head=ptr->next; // head pointing to second node
free(ptr); // deleting old first node
}
else
{
struct node* prev_ptr;
for(i=0;i<pos;i++)
{
prev_ptr = ptr;
ptr = ptr->next;
}
prev_ptr->next = ptr->next; //prev node pointing to pos+1 node
free(ptr); //deleting node at pos
}
}
lOMoARcPSD|26268675

OUTPUT:

RESULT:
Thus the C program for array implementation of list ADT was created, executed an Output was
verified successfully.
lOMoARcPSD|26268675

8.B.LINKED LIST IMPLEMENTATION OF STACK ADT

AIM
To write a c program to implement stack using linked list.

ALGORITHM
Step1: Start the program.
Step2:For Push operation,check for stack overflow
Step3:If Top>=N then print stack overflow else increment Top and insert the element.
Step4:For Pop operation, check for under flow of the stack.
Step5:If Top=0 then print stack under flow else decrement Top and delete the element.
Step 6: Stop the program.

PROGRAM
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node*next;
}*top=NULL;
void push(int);
void pop();
void display();
int main()
{
int choice,value;
printf("\n stackusinglinkedlist: \n");
while(1)
{
printf("1.push\n2.pop\n3.display\n4.exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("enterthevaluetobeinsert:");
scanf("%d",&value);
push(value);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:printf("\nwrongselectionpleasetryagain\n");
}
33
lOMoARcPSD|26268675

}
return 0;
}
void push(int value)
{
struct Node *newNode;
newNode=(struct Node*)malloc(sizeof(struct Node));
newNode->data=value;
if(top==NULL)
newNode->next=NULL;
else
newNode->next=top;
top=newNode;
printf("\ninsertion issucess\n");
}
void pop()
{
if(top==NULL)
printf("\n stackisempty\n");
else
{
struct Node*temp=top;
printf("\n deleted element%d\n",temp->data);
top=temp->next;
free(temp);
}
}
void display()
{
if(top==NULL)

printf("\nstack isemplty\n");
else

{
struct Node*temp=top;
while(temp->next!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
printf("%d->NULL\n",temp->data);
}
}

34
lOMoARcPSD|26268675

OUTPUT

RESULT
Thus the C program for implementation of stack using linked list was done and
Executed successfully.

35
lOMoARcPSD|26268675

8.C.LINKED LIST IMPLEMENTATION OF LINEAR QUEUE ADT

AIM
To write a C program to implement queue using linked list.

ALGORITHM
Step 1: Start
Step 2: define structure for queue
Step 3: Read choice
Step 4: If choice = Enqueue,
i) Read the element
ii) Create a data structure
iii) If empty queue then front of queue pointer points to newly created node,
Otherwise end of the queue points to newly created node.
Step 5: If choice = Dequeue,
i) Check if queue is empty. If so, print queue is empty.
ii) Otherwise read the element pointed by front of the queue, temp pointer
points
to front of queue.
iii) Front of queue points to next element, free the element pointed by temp
pointer.
iv) Return and Print the element.
Step 6: If choice = display
i) Check of empty queue if so, print queue is empty.
ii) Otherwise print the elements from front of the queue until the end of the
queue.
Step 7: If choice = exit, stop.

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*front = NULL, *rear = NULL;
void insert();
void delet();
void display();
int item;
int main()
{

int ch;
do
{
printf("\n\n1.\tEnqueue\n2.\tDequeue\n3.\tDisplay\n4.\tExit\n");
36
lOMoARcPSD|26268675

printf("\nEnter your choice: ");


scanf("%d", &ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("\n\nInvalid choice. Please try again...\n");
}
}
while(1);
return 0;
}
void insert()
{
printf("\n\nEnter ITEM: ");
scanf("%d", &item);
if(rear == NULL)
{
rear = (struct node*)malloc(sizeof(struct node));
rear->info = item;
rear->link = NULL;
front = rear;
}

else
{
rear->link= (struct node*)malloc(sizeof(struct node));
rear = rear->link;
rear->info = item;
rear->link = NULL;
}
}
void delet()
{
struct node *ptr;
if(front == NULL)
printf("\n\nQueue is empty.\n");
else
{
ptr = front;
item = front->info; front = front->link;
free(ptr);
37
lOMoARcPSD|26268675

printf("\nItem deleted: %d\n", item);


if(front == NULL)
rear = NULL;
}
}
void display()
{
struct node *ptr = front;
if(rear == NULL)
printf("\n\nQueue is empty.\n");
else
{
printf("\n\n");
while(ptr != NULL)
{
printf("%d\t",ptr->info);
ptr = ptr->link;
}
}
}

OUTPUT

RESULT
Thus the C program forqueue using linked list was implemented and executed
Successfully.
38
lOMoARcPSD|26268675

9(A). APPLICATION OF LIST ADT


A.IMPLEMENTATION OF POLYNOMIAL MANIPULATION USING LINKED LIST

AIM
To write a C program to perform polynomial manipulation using linkedlist.

ALGORITHM
Step 1: While p and q are not null repeat step 2
Step 2: If power of the two term are equal then if the term do not cancel then insert num of terms into the sum
polynomial advance p , advance q, else if the power first
polynomial-&gt; power of the second then insert the term from second polynomial in to
sum polynomial advance q
Step 3: Copy remaining terms from the non empty polynomial in to the sum polynomial.
Step 4: Stop the program.
PROGRAM
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
struct link
{
int coeff;
int pow;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
char ch;
do
{
printf("\n Enter coefficient:");
scanf("%d",&node->coeff);
printf("\n Enter power:");
scanf("%d",&node->pow);
node->next=(struct link *)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\n\nDo u want to continue(y/n):");
ch=getch();

}while(ch=='y' || ch=='Y');
}
void show(struct link *node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->pow);
node=node->next;
if(node->next!=NULL) printf("+");
}
}
lOMoARcPSD|26268675

void polyadd(struct link *poly1,struct link *poly2,struct link *poly)


{
while(poly1->next && poly2->next)
{
if(poly1->pow>poly2->pow)
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
else if(poly1->pow<poly2->pow)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->pow=poly1->pow;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{

poly->pow=poly1->pow;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{
poly->pow=poly2->pow;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
poly->next=(struct link *)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
}
main()
{
char ch;
do
{
poly1=(struct link *)malloc(sizeof(struct link));
lOMoARcPSD|26268675

poly2=(struct link *)malloc(sizeof(struct link));


poly=(struct link *)malloc(sizeof(struct link));
printf("Polynomial Manipulation-ADDTION of two polynomials\n");
printf("\nEnter 1st number");
create(poly1);
printf("\nEnter 2nd number");
create(poly2);
printf("\n\nFirst Number:");
show(poly1);
printf("\n\nSecond Number:");
show(poly2);
polyadd(poly1,poly2,poly);
printf("\n\nAdded polynomial:");
show(poly);
printf("\n\n Do you want to add two more numbers(y/n):");
ch=getch();
}while(ch=='y' || ch=='Y');
return(0);
}

OUTPUT

RESULT
Thus the C program for the implementation of Polynomial Manipulation using Linked
List was done and executed successfully.
lOMoARcPSD|26268675

9(B). APPLICATION OF STACK ADT

AIM:
To write a C program to implement the application of Stack ADT-conversion of infix expression to
postfix expression.

ALGORITHM:
Step 1: Initialize an empty stack.
Step 2: Scan the Infix expression from left to right.
Step 3: If the scanned character is an operand, add it to the postfix expression i.e., output
array.
Step 4: If the scanned character is open parenthesis ‘(‘then push it into the stack.
Step 5: If the scanned character is an operator and if the stack is empty, then push it to the stack.
Step 6: If the scanned character is operator and stack is not empty, then
i) Compare the precedence of scanned character with the operator on the top of the
stack.
ii) While operator at the top of stack has higher precedence over the scanned character
& stack is not empty,
a) Pop the stack.
b) Add the popped character to postfix string i.e., output array.
iii) Push the scanned character to top of the stack.
Step 7: If the scanned operator is less than or equal to the top of the stack then
i) Push the scanned operator into the stack
Step 8: If the scanned character is closing parentheses ’)’ then pop all the characters above
opening parenthesis ‘(‘from the stack.
Step 9: Repeat the steps 3-8 till all the characters are scanned.
Step 10: While stack not empty, pop the elements to the output array until stack is empty.
Step 11: Return the postfix string i.e., expression from output array.

PROGRAM
#include<conio.h>
#include<stdio.h>
#include<string.h>
char stack[50];
int top=-1;
void in_to_post(char infix[]);
void push (char);
char pop();
void main()
{
char infx[25];
printf("Conversion of Infix to Postfix expression\n\n");

printf("Enter the infix expression");


gets(infx);
in_to_post(infx);
getch();
}
void push (char symb)
{
lOMoARcPSD|26268675

if (top>=49)
{
printf("stack overflow");
getch();
return;
}
else
{
top=top+1;
stack[top]=symb;
}
}
char pop()
{
char item;
if(top==-1)
{
printf("stack empty");
getch();
return(0);
}
else
{
item=stack[top];
top--;
}
return(item);
}
int preced(char ch)
{
if(ch==47)
{
return(5);
}
else if(ch==42)
{
return(4);
}

else if(ch==43)
{
return(3);
}
else
return(2);
}
void in_to_post(char infix[])
{
int length;
static int index=0,pos=0;
char symbol,temp, postfix[40];
length=strlen(infix);
lOMoARcPSD|26268675

push('#');
while(index<length)
{
symbol=infix[index];
switch(symbol)
{
case'(':
push(symbol);
break;
case')' :
temp=pop();
while(temp!='(')
{
postfix[pos]=temp;
pos++; temp=pop();
}
break;
case '+' :
case '-' :
case '*' :
case '/' :
case '^' :
while (preced(stack[top])>=preced(symbol))
{
temp=pop();
postfix[pos]=temp;
pos++;
}
push(symbol);
break;
default :
postfix[pos++]=symbol;

break;
}
index++;
}
while(top>0)
{
temp=pop();
postfix[pos++]=temp;
}
postfix[pos++]='\0';
puts(postfix);
return;
}

OUTPUT:
lOMoARcPSD|26268675

RESULT:
Thus the C program for conversion of infix to postfix expression using Stack ADT was executed
successfully.
lOMoARcPSD|26268675

C.APPLICATION OF QUEUE ADT

AIM:
To write a C program to implement the application of Queue ADT- Disk Scheduling.

ALGORITHM:
1. Let Request array represents an array storing indexes of tracks that have been requested in
ascending order of their time of arrival. ‘head’ is the position of disk head.
2. Let us one by one take the tracks in default order and calculate the absolute distance of the
track from the head.
3. Increments the total seek count with this distance.
4. Currently serviced track position now becomes the new head position.
5. Go to step 2 until all tracks in request array have not been serviced.

PROGRAM
#include<stdio.h>
#include<stdlib.h>
int main()
{
int RQ[100],i,n,TotalHeadMoment=0,initial;
printf("Enter the number of Requests\n");
scanf("%d",&n);
printf("Enter the Requests sequence\n");
for(i=0;i<n;i++)
scanf("%d",&RQ[i]);
printf("Enter initial head position\n");
scanf("%d",&initial);
// logic for FCFS disk scheduling
for(i=0;i<n;i++)
{
TotalHeadMoment=TotalHeadMoment+abs(RQ[i]-initial);
initial=RQ[i];
}
printf("Total head moment is %d",TotalHeadMoment);
return 0;
}

OUTPUT:

RESULT
Thus the C program for disk scheduling using Queue ADT was executed successfully.

46
lOMoARcPSD|26268675

10.A.IMPLEMENTATION OF BINARY SEARCH TREES

AIM

To write a C program to construct a binary search tree and perform various operations.

ALGORITHM
1. Declare function create (), search (), delete (), Display ().
2. Create a structure for a tree contains left pointer and right pointer.
3. Insert an element is by checking the top node and the leaf node and the operation will be performed.
4. Deleting an element contains searching the tree and deleting the item.
5. Display the Tree elements.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<malloc.h>
struct tree
{
int data;
struct tree *lchild;
struct tree *rchild;
}*t,*temp; int element;
void inorder(struct tree *);
void preorder(struct tree *);
void postorder(struct tree *);
struct tree * create(struct tree *, int);
struct tree * find(struct tree *, int);
struct tree * insert(struct tree *, int);
struct tree * del(struct tree *, int);
struct tree * findmin(struct tree *);
struct tree * findmax(struct tree *);
void main()
{
int ch;
do
{
printf("\n\t\t\tBINARY SEARCH TREE");
printf("\n\t\t\t****** ****** ****");
printf("\nMain Menu\n");
printf("\n1.Create\n2.Insert\n3.Delete\n4.Find\n5.FindMin\n6.FindMax");
printf("\n7.Inorder\n8.Preorder\n9.Postorder\n10.Exit\n");
printf("\nEnter ur choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the data:");
scanf("%d",&element);
t=create(t,element);

47
lOMoARcPSD|26268675

inorder(t);
break;
case 2:
printf("\nEnter the data:");
scanf("%d",&element);
t=insert(t,element);
inorder(t);
break;
case 3:
printf("\nEnter the data:");
scanf("%d",&element);
t=del(t,element);
inorder(t);
break;
case 4:
printf("\nEnter the data:");
scanf("%d",&element);
temp=find(t,element);
if(temp->data==element)
printf("\nElement %d is at %d",element,temp);
else
printf("\nElement is not found");
break;
case 5:
temp=findmin(t);
printf("\nMax element=%d",temp->data);
break;
case 6:
temp=findmax(t);
printf("\nMax element=%d",temp->data);
break;
case 7:
inorder(t);
break;
case 8:
preorder(t);
break;
case 9:
postorder(t);
break;
case 10:
exit(0);
}
}
while(ch<=10);
}
struct tree * create(struct tree *t, int element)
{
t=(struct tree *)malloc(sizeof(struct tree));
t->data=element;
t->lchild=NULL;
t->rchild=NULL;
48
lOMoARcPSD|26268675

return t;
}
struct tree * find(struct tree *t, int element)

{
if(t==NULL)
return NULL; if(element<t->data)
return(find(t->lchild,element));
else
if(element>t->data) return(find(t->rchild,element));
else
return t;
}
struct tree *findmin(struct tree *t)
{
if(t==NULL)
return NULL;
else
if(t->lchild==NULL)
return t;
else
return(findmin(t->lchild));
}
struct tree *findmax(struct tree *t)
{
if(t!=NULL)
{
while(t->rchild!=NULL) t=t->rchild;
}
return t;
}
struct tree *insert(struct tree *t,int element)
{
if(t==NULL)
{
t=(struct tree *)malloc(sizeof(struct tree));
t->data=element;
t->lchild=NULL; t->rchild=NULL;
return t;

}
else
{
if(element<t->data)
{
t->lchild=insert(t->lchild,element);
}
else
if(element>t->data)
{
t->rchild=insert(t->rchild,element);
}
49
lOMoARcPSD|26268675

else
if(element==t->data)
{
printf("element already present\n");
}
return t;
}
}
struct tree * del(struct tree *t, int element)
{
if(t==NULL)
printf("element not found\n"); else
if(element<t->data)
t->lchild=del(t->lchild,element); else
if(element>t->data)
t->rchild=del(t->rchild,element); else
if(t->lchild&&t->rchild)
{
temp=findmin(t->rchild);
t->data=temp->data;
t->rchild=del(t->rchild,t->data);
}
else
{
temp=t;
if(t->lchild==NULL)
t=t->rchild;
else
if(t->rchild==NULL)
t=t->lchild; free(temp);
}
return t;
}
void inorder(struct tree *t)

{
if(t==NULL)
return;
else
{
inorder(t->lchild);
printf("\t%d",t->data);
inorder(t->rchild);
}
}
void preorder(struct tree *t)
{
if(t==NULL)
return;
else
{
printf("\t%d",t->data);
50
lOMoARcPSD|26268675

preorder(t->lchild);
preorder(t->rchild);
}
}

void postorder(struct tree *t)


{
if(t==NULL)
return; else
{
postorder(t->lchild);
postorder(t->rchild);

printf("\t%d",t->data);
}
}

OUTPUT

RESULT
Thus the C program to construct a binary search tree and perform various operations has been
executed successfully.

51
lOMoARcPSD|26268675

12. IMPLEMENTATION OF SEARCHING TECHNIQUES

IMPLEMENTATION OF LINEAR SEARCH


AIM
To write a C program to implement linear search algorithm.
ALGORITHM:
Step 1: Start the program
Step 2: Read the value of ‘n’ and the elements in array[c].
Step 3: Get the element to be searched in ‘search’.
Step 4: Repeat, steps 5 and 6 upto ‘c<=n’.
Step 5: If (array[c] = search)
Print “element is found at c “else c++
Step 6: If c==n Print element not found”
Step 7: Stop.
PROGRAM
#include <stdio.h>
int main( )
{
int array[100], search, c, n;
printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{
if (array[c] == search)
/* if required element found */
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.\n", search);
return 0;
}

52
lOMoARcPSD|26268675

OUTPUT

RESULT
Thus the C program for linear search, executed and output was verified successfully.
53
lOMoARcPSD|26268675

13.A.IMPLEMENTATION OF INSERTION SORT

AIM

To Write a C Program to perform insertion sort, quick sort and merge sort.
ALGORITHM:
INSERTION SORT
1. Get the n elements to be sorted.
2. The ith element is compared from (i-1)th to 0th element and placed in proper position
according to ascending value.
3. Repeat the above step until the last element.
QUICK SORT
1. Pick an element, called a pivot, from the list.
2. Reorder the list so that all elements which are less than the pivot come before the
pivot and so that all elements greater than the pivot come after it.
3. After this partitioning, the pivot is in its final position. This is called the partition
operation.
4. Recursively sort the sub-list of lesser elements and the sub-list of greater elements
MERGE SORT
step 1: start
step 2: declare array and left, right, mid variable
step 3: perform merge function.
if left &gt; right
return
mid= (left+right)/2
mergesort(array, left, mid)
mergesort(array, mid+1, right)
merge(array, left, mid, right)
step 4: Stop

54
lOMoARcPSD|26268675

PROGRAM:
#include <math.h>
#include <stdio.h>
// Insertion Sort Function
void insertionSort(int array[], int n)
{
int i, element, j;
for (i = 1; i < n; i++)
{
element = array[i];
j = i - 1;
while (j >= 0 && array[j] > element) {
array[j + 1] = array[j];
j = j - 1;
}
array[j + 1] = element;
}
}
// Function to print the elements of an array
void printArray(int array[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", array[i]);
printf("n");
}
// Main Function
int main()
{
int array[] = { 99, 17, 80, 1, 40};
int n = sizeof(array) / sizeof(array[0]);
insertionSort(array, n);
printArray(array, n);
return 0;
}

55
lOMoARcPSD|26268675

OUTPUT

Selection Sort

#include <stdio.h>
// Function to swap elements
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Selection Sort
void selectionSort(int array[], int n)
{
int i, j, min_element;
for (i = 0; i < n-1; i++)
{
min_element = i;
for (j = i+1; j < n; j++)
if (array[j] < array[min_element])
min_element = j;
swap(&array[min_element], &array[i]);
}
}
// Function to print the elements of an array
void printArray(int array[], int size)
{

int i;
for (i=0; i < size; i++)
printf("%d ", array[i]);
printf("n");
}
// Main Function
int main()
56
lOMoARcPSD|26268675

{
int array[] = {15, 10, 99, 53, 36};
int size = sizeof(array)/sizeof(array[0]);
selectionSort(array, size);
printf("Sorted array:n");
printArray(array, size);
return 0;
}
OUTPUT

RESULT
Thus the C program for implementation of sorting algorithm has been executed
successfully

57
lOMoARcPSD|26268675

14.HASHING – ANY TWO COLLISION

AIM:
To write a C program to implement hash table

ALGORITHM:
1. Create a structure, data (hash table item) with key and value as data.
2. Now create an array of structure, data of some certain size (10, in this case). But, the size of
array must be immediately updated to a prime number just greater than initial array capacity
(i.e 10, in this case).
3. A menu is displayed on the screen.
4. User must choose one option from four choices given in the menu
5. Perform all the operations
6. Stop the program
PROGRAM
#include<stdio.h>
#include<stdlib.h>
struct data
{
int key;
int value;
};
struct data *array;
int capacity = 10;
int size = 0;
/* this function gives a unique hash code to the given key */
int hashcode(int key)
{
return (key % capacity);
}
/* it returns prime number just greater than array capacity */
int get_prime(int n)
{
if (n % 2 == 0)
{
n++;
}
for (;!if_prime(n); n += 2);
return n;
}
/* to check if given input (i.e n) is prime or not */
int if_prime(int n)
{

int i;
for (i = 2; i < n; i++)
{
if (n % i == 0)
{
58
lOMoARcPSD|26268675

return 0;
}
}
return 1;
}
void init_array()
{
int i;
capacity = get_prime(capacity);
array = (struct data*) malloc(capacity * sizeof(struct data));
for (i = 0; i < capacity; i++)
{
array[i].key = 0;
array[i].value = 0;
}
}
/* to insert a key in the hash table */
void insert(int key)
{
int index = hashcode(key);
if (array[index].value == 0)
{
/* key not present, insert it */
array[index].key = key;
array[index].value = 1;
size++;
printf("\n Key (%d) has been inserted \n", key);
}
else if(array[index].key == key)
{
/* updating already existing key */
printf("\n Key (%d) already present, hence updating its value \n", key);
array[index].value += 1;
}
else
{

/* key cannot be insert as the index is already containing some other key*/ printf("\n ELEMENT CANNOT BE
INSERTED \n");
}
}
/* to remove a key from hash table */
void remove_element(int key)
{
int index = hashcode(key);
if(array[index].value == 0)
{
printf("\n This key does not exist \n");
}
else
{
59
lOMoARcPSD|26268675

array[index].key = 0;
array[index].value = 0; size--;
printf("\n Key (%d) has been removed \n", key);
}
}
/* to display all the elements of a hash table */
void display()
{
int i;
for (i = 0; i < capacity; i++)
{
if (array[i].value == 0)
{
printf("\n Array[%d] has no elements \n");
}
else
{
printf("\n array[%d] has elements -:\n key(%d) and value(%d) \t",i, array[i].key, array[i].value);
}
}
}
int size_of_hashtable()
{
return size;
}
int main()

{
int choice, key, value, n, c; init_array();
do
{
printf("\n Implementation of Hash Table in C \n\n");
printf("MENU-: \n1.Inserting item in the Hash Table"
"\n 2.Removing item from the Hash Table" "\n3.Check the size of Hash Table"
"\n4.Display a Hash Table" "\n\n Please enter your choice -:");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Inserting element in Hash Table\n"); printf("Enter key -:\t");
scanf("%d", &key);
insert(key);
break;
case 2:
printf("Deleting in Hash Table \n Enter the key to delete-:");
scanf("%d", &key);
remove_element(key);
break;
case 3:
n = size_of_hashtable();
printf("Size of Hash Table is-:%d\n", n);
60
lOMoARcPSD|26268675

break;
case 4:
display();
break;
default:
printf("Wrong Input\n");}
printf("\n Do you want to continue-:(press 1 for yes)\t");
scanf("%d", &c);
}
while(c == 1);
getch();
}

OUTPUT

RESULT
Thus the C program for implementation of sorting algorithm has been executed
61
lOMoARcPSD|26268675

successfully

62

You might also like