C & Data Structure Lab
C & Data Structure Lab
AIM:
To write a C program to perform some expressions using I/O statements.
ALGORITHM:
1. Start
2. Declare variables as dec,str,ch,pi
3. Access the value of the variable using I/O statements
4. Print the values of dec,str,ch,pi
5. Stop
PROGRAM:
#include <stdio.h>
Void main()
{
int dec = 5;
char str[] = "abc";
char ch = 's';
float pi = 3.14;
printf("%d %s %f %c\n", dec, str, pi, ch);
}
Output:
5 abc 3.140000 c
RESULT:
AIM:
ALGORITHM:
1. Start
2. Declare variables as x=15, y=18
3. Check the condition if(x>y)
If x is big print x is greater than y
If y is big print y is greater than x
4. Stop
PROGRAM:
#include <stdio.h>
Void main( )
{
int x, y;
x = 15;
y = 18;
if (x > y )
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
}
Output:
y is greater than x
RESULT:
AIM:
ALGORITHM:
1. Start
2. i is initialized to 1.
3. The test expression i < 11 is evaluated. Since 1 less than 11 is true, the body of for
loop is executed. This will print the 1 (value of i) on the screen.
4. The update statement ++i is executed. Now, the value of i will be 2. Again, the
test expression is evaluated to true, and the body of for loop is executed. This will
print 2 (value of i) on the screen.
5. Again, the update statement ++i is executed and the test expression i < 11 is
evaluated. This process goes on until i becomes 11.
6. When i becomes 11, i < 11 will be false, and the for loop terminates.
7. Stop.
PROGRAM:
#include <stdio.h>
int main()
{
int num, count, sum = 0;
return 0;
}
Output:
Enter a positive integer: 10
Sum = 55
RESULT:
AIM:
ALGORITHM:
1. Start
2. Initially, the sum() is called from the main() function with number passed as an
argument.
3. Suppose, the value of n inside sum() is 3 initially. During the next function call, 2
is passed to the sum() function. This process continues until n is equal to 0.
4. When n is equal to 0, the if condition fails and the else part is executed returning
the sum of integers ultimately to the main() function.
5. Stop.
PROGRAM:
#include <stdio.h>
int sum(int n);
int main() {
int number, result;
result = sum(number);
int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}
Output:
RESULT:
AIM:
ALGORITHM:
1. Start
2. Arrays have 0 as the first index, not 1. In this example, mark[0] is the first
element.
3. If the size of an array is n, to access the last element, the n-1 index is used. In this
example, mark[4]
4. Suppose the starting address of mark[0] is 2120d. Then, the address of the
mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d and so on.
5. This is because the size of a float is 4 bytes.
6. Stop.
PROGRAM:
#include <stdio.h>
int main() {
int values[5];
Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3
RESULT:
AIM:
ALGORITHM:
1. Start
2. Here, a pointer pc and a normal variable c, both of type int, is created.
3. Since pc and c are not initialized at initially, pointer pc points to either no address
or a random address. And, variable c has an address but contains random garbage
value.
4. This assigns the address of variable c to the pointer pc.
5. This change the value at the memory location pointed by the pointer pc to 2.
6. Stop.
PROGRAM:
#include <stdio.h>
int main()
{
int* pc, c;
c = 22;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 22
pc = &c;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 22
c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11
*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
Output:
Address of c: 2686784
Value of c: 22
Address of c: 2686784
Value of c: 2
RESULT:
AIM:
ALGORITHM:
1. Start.
2. Like primitive types, we can have pointer to a structure.
3. If we have a pointer to structure, members are accessed using arrow ( -> )
operator.
4. Stop.
PROGRAM:
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {1, 2};
// p2 is a pointer to structure p1
struct Point *p2 = &p1;
Output:
12
RESULT:
AIM:
To write a C program to perform files.
ALGORITHM:
1. Start.
2. Firstly, It searches the file to be opened.
3. Then, it loads the file from the disk and place it into the buffer. The buffer is used
to provide efficiency for the read operations.
4. It sets up a character pointer which points to the first character of the file.
5. Stop.
PROGRAM:
Run 1:
Enter file name to create : file1.txt
File created successfully.
Run 2:
Enter file name to create : d:/file1.txt
File created successfully.
Run 3:
Run 1:
Enter file name to create : h:/file1.txt
File does not created!!!
RESULT:
AIM:
To write a C program to real time c application.
ALGORITHM:
1. Start.
2. When this occurs is determined by the location of the operators.
3. If the ++ or -- operator appears before the variable, the increment or decrement
operation occurs before any subsequent operations using that variable.
4. If the ++ or -- operator appears after the variable name, the increment or
decrement occurs after the indicated operation.
5. Stop.
PROGRAM:
#include <stdio.h>
int main(void)
{
int c;
switch(c) {
case '0':
printf("Numeral 0\n");
case '1':
printf("Numeral 1\n");
case '2':
printf("Numeral 2\n");
case '3':
printf("Numeral 3\n");
}
}
}
Output:
0
Numeral 0
Numeral 1
Numeral 2
Numeral 3
1
Numeral 1
Numeral 2
Numeral 3
2
Numeral 2
Numeral 3
3
Numeral 3
.
RESULT:
C program using real time c application are compiled and executed successfully.
EX NO: 6
ARRAY IMPLEMENTATION OF LIST ADT
Date:
AIM:
To write a C program for implement the concept of list ADT using arrays.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void create();
void insert();
void deletion();
void search();
void display();
inta,b[20], n, p, e, f, i, pos;
void main()
{
clrscr();
intch;
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:display();
break;
case 6:exit();
break;
default: printf("\n Enter the correct choice:");
}
printf("\n Do u want to continue:::");
scanf("\n%c", &g);
}
while(g=='y'||g=='Y');
getch();
}
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++)
{
if(b[i]==e)
{
printf("Value is in the %d Position", i);
}
}
}
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]);
}
}
OUTPUT:
Main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 1
Enter the number of elements: 4
Enter the elements:
10
20
30
40
Do u want to continue(y/n): y
main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 2
Enter the element to delete:20
Elements after deletion: 10
30
40
Do u want to continue(y/n): y
main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 3
Enter the element to search: 100
Element not found
Do u want to continue(y/n): y
main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 4
Enter the element to insert: 15
Enter the position to insert:2
Elements after insertion:
10
15
30
40
Do u want to continue(y/n): y
main Menu
1.Create
2.Delete
3.Search
4.Insert
5.Display
6.Exit
Enter your Choice: 4
Result:
AIM:
To write a C program to implement Stack operations such as push, pop and display using
array.
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define size 5
int item;
int s[10];
int top;
void display()
{
inti;
if(top==-1)
{
printf("\n stack is empty");
return;
}
printf("\n Content of stack is:\n");
for(i=0;i<=top ;i++)
printf("%d\t",s[i]);
}
void push()
{
if(top==size-1)
{
printf("\nStack is full");
return;
}
printf("\nEnter item:\n");
scanf("%d",&item);
s[++top]=item;
}
void pop()
{
if(top==-1)
{
printf("\nstack is empty");
return;
}
printf("\nDeleted item is: %d",s[top]);
top--;
}
void main()
{
intch;
top=-1;
clrscr();
printf("\n1.push\t\t2.pop\n3.display\t4.exit\n");
do{
printf("\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter item:\n");
scanf("%d",&item);
push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong entry ! try again");
}}while(ch<=4);
getch();
}
OUTPUT:
1. Push 2.pop
3. Display 4.exit
Enter your choice:
1
Enter item:
100
Enter your choice:
1
Enter item:
200
Enter your choice:
1
Enter item:
300
Enter your choice:
2
Deleted item is: 300
Enter your choice:
3
Content of stack is:
100 200
Enter your choice: 4
RESULT:
AIM:
To write a C program to implement Queue operations such as enqueue, dequeue and
display using array.
ALGORITHM:
Step 1: Start the program.
Step 2: Initialize front=0; rear=-1.
Step 3: Enqueue operation moves a rear by one position and inserts a element at the rear.
Step 4: Dequeue operation deletes an element at the front of the list and moves the front by one
position
Step 5: Display operation displays all the element in the list.
Step 6: Stop.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define SIZE 5 /* Size of Queue */
int Q[SIZE],f=0,r=-1; /* Global declarations */
Qinsert(intelem)
{ /* Function for Insert operation */
if( Qfull())
printf("\n\n Overflow!!!!\n\n");
else
{
++r;
Q[r]=elem;
}
}
intQdelete()
{ /* Function for Delete operation */
intelem;
if(Qempty()){ printf("\n\nUnderflow!!!!\n\n");
return(-1); }
else
{
elem=Q[f];
f=f+1;
return(elem);
}}
intQfull()
{ /* Function to Check Queue Full */
if(r==SIZE-1) return 1;
return 0;
}
intQempty()
{ /* Function to Check Queue Empty */
if(f> r) return 1;
return 0;
}
display()
{ /* Function to display status of Queue */
inti;
if(Qempty()) printf(" \n Empty Queue\n");
else
{
printf("Front->");
for(i=f;i<=r;i++)
printf("%d ",Q[i]);
printf("<-Rear");
}}
void main()
{ /* Main Program */
intopn,elem;
do
{
clrscr();
printf("\n ### Queue Operations using Arrays### \n\n");
printf("\n Press 1-Insert, 2-Delete,3-Display,4-Exit\n");
printf("\n Your option ? ");
scanf("%d",&opn);
switch(opn)
{
case 1: printf("\n\nRead the element to be Inserted ?");
scanf("%d",&elem);
Qinsert(elem);
break;
case 2: elem=Qdelete();
if( elem != -1)
printf("\n\nDeleted Element is %d \n",elem);
break;
case 3: printf("\n\nStatus of Queue\n\n");
display();
break;
case 4: printf("\n\n Terminating \n\n");
break;
default: printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
printf("\n\n\n\n Press a Key to Continue . . . ");
getch();
}while(opn != 4);
getch();
}
OUTPUT:
### Queue Operations using Arrays###
Press 1-Insert, 2-Delete,3-Display,4-Exit
Your option ? 1
Read the element to be Inserted ?100
Press a Key to Continue . . .
### Queue Operations using Arrays###
Press 1-Insert, 2-Delete,3-Display,4-Exit
Your option ? 1
Read the element to be Inserted ?200
Press a Key to Continue . . .
### Queue Operations using Arrays###
Press 1-Insert, 2-Delete,3-Display,4-Exit
Your option ? 1
Read the element to be Inserted ?300
Press a Key to Continue . . .
### Queue Operations using Arrays###
Press 1-Insert, 2-Delete,3-Display,4-Exit
Your option ? 2
Deleted Element is 100
Press a Key to Continue . . .
### Queue Operations using Arrays###
Press 1-Insert, 2-Delete,3-Display,4-Exit
Your option ? 3
Status of Queue
Front->200 300 <-Rear
Press a Key to Continue . . .
RESULT:
Thus a C program for Queue using array was implemented successfully.
EX NO : 8(a)
LINKED LIST IMPLEMENTATION OF LIST ADTS
Date:
AIM :
To write a C program for linked list implementation using list ADTs
ALGORITHM :
Step 1: Start the program
Step 2: Initialize the node and position of the list
Step 4: Insert and Delete function are used for inserting, deleting the element in the list
Step 5: Get the correct operation in the choice
Step 6: Display the elements in the list
Step 7 :Stop the program
PROGRAM :
#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node
{
int e;
Position next;
};
void Insert(int x, List l, Position p)
{
Position TmpCell;
TmpCell = (struct Node*) malloc(sizeof(struct Node));
if(TmpCell == NULL)
printf("Memory out of space\n");
else
{
TmpCell->e = x;
TmpCell->next = p->next;
p->next = TmpCell;
}
}
int isLast(Position p)
{
return (p->next == NULL);
}
Position FindPrevious(int x, List l)
{
Position p = l;
while(p->next != NULL && p->next->e != x)
p = p->next;
return p;
}
void Delete(int x, List l)
{
Position p, TmpCell;
p = FindPrevious(x, l);
if(!isLast(p))
{
TmpCell = p->next;
p->next = TmpCell->next;
free(TmpCell);
}
else
printf("Element does not exist!!!\n");
}
void Display(List l)
{
printf("The list element are :: ");
Position p = l->next;
while(p != NULL)
{
printf("%d -> ", p->e);
p = p->next;
}
}
void Merge(List l, List l1)
{
int i, n, x, j;
Position p;
printf("Enter the number of elements to be merged :: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
p = l1;
scanf("%d", &x);
for(j = 1; j < i; j++)
p = p->next;
Insert(x, l1, p);
}
printf("The new List :: ");
Display(l1);
printf("The merged List ::");
p = l;
while(p->next != NULL)
{
p = p->next;
}
p->next = l1->next;
Display(l);
}
int main()
{
int x, pos, ch, i;
List l, l1;
l = (struct Node *) malloc(sizeof(struct Node));
l->next = NULL;
List p = l;
printf("LINKED LIST IMPLEMENTATION OF LIST ADT\n\n");
do
{
printf("\n\n1. INSERT\t 2. DELETE\t 3. MERGE\t 4. PRINT\t 5. QUIT\n\nEnter the choice :: ");
scanf("%d", &ch);
switch(ch)
{
case 1:p = l;
printf("Enter the element to be inserted :: ");
scanf("%d",&x);
printf("Enter the position of the element :: ");
scanf("%d",&pos);
for(i = 1; i < pos; i++)
{
p = p->next;
}
Insert(x,l,p);
break;
case 2:p = l;
printf("Enter the element to be deleted :: ");
scanf("%d",&x);
Delete(x,p);
break;
case 3:l1 = (struct Node *) malloc(sizeof(struct Node));
l1->next = NULL;
Merge(l, l1);
break;
case 4:Display(l);
break;
}
}
while(ch<5);
return 0;
}
OUTPUT :
RESULT :
Thus a C program for list using linked list was implemented successfully.
EX NO : 8(b)
LINKED LIST IMPLEMENTATION OF STACK ADTS
Date:
AIM:
To write a C program to implement Stack operations such as push, pop and display using
linked list.
ALGORITHM:
PROGRAM:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
void pop();
void push(int value);
void display();
struct node
{
intdata;
struct node *link;
};
struct node *top=NULL,*temp;
void main()
{
int choice,data;
while(1) //infinite loop is used to insert/delete infinite number of elements in stack
{
printf("\n1.Push\n2.Pop\n3.Display\n4.Exit\n");
printf("\nEnterur choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: //To push a new element into stack
printf("Enter a new element :");
scanf("%d",&data);
push(data);
break;
case 2: // pop the element from stack
pop();
break;
case 3: // Display the stack elements
display();
break;
case 4: // To exit
exit(0);
}}
getch();
//return 0;
}
void display()
{
temp=top;
if(temp==NULL)
{
printf("\nStack is empty\n");
}
printf("\n The Contents of the Stack are...");
while(temp!=NULL)
{
printf(" %d ->",temp->data);
temp=temp->link;
}
}
void push(int data)
{
temp=(struct node *)malloc(sizeof(struct node)); // creating a space for the new element.
temp->data=data;
temp->link=top;
top=temp;
display();
}
void pop()
{
if(top!=NULL)
{
printf("The poped element is %d",top->data);
top=top->link;
}
else
{
printf("\nStack Underflow");
}
display();
}
OUTPUT:
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:1
Enter a new element :10
The Contents of the Stack are... 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:1
Enter a new element :20
The Contents of the Stack are... 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:1
Enter a new element :30
The Contents of the Stack are... 30 -> 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:2
The poped element is 30
The Contents of the Stack are... 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:3
The Contents of the Stack are... 20 -> 10 ->
1.Push
2.Pop
3.Display
4.Exit
Enter ur choice:4
RESULT:
Thus a C program for Stack using linked list was implemented successfully.
EX NO : 8(c)
LINKED LIST IMPLEMENTATION OF QUEUE ADTS
Date:
AIM:
ALGORITHM:
PROGRAM:
#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *link;
}*front = NULL, *rear = NULL;
void insert();
voiddelet();
void display();
int item;
void main()
{
intch;
do
{
printf("\n\n1.\tEnqueue\n2.\tDequeue\n3.\tDisplay\n4.\tExit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:insert();
break;
case 2:delet();
break;
case 3:display();
break;
case 4:exit(0);
default:printf("\n\nInvalid choice. Please try again...\n");
}
} while(1);
getch();
}
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;
}}
Voiddelet()
{
struct node *ptr;
if(front == NULL)
printf("\n\nQueue is empty.\n");
else
{
ptr = front;
item = front->info;
front = front->link;
free(ptr);
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:
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 12
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 15
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Enter ITEM: 20
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Item deleted: 12
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice:3
15 20
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice:4
RESULT:
Thus a C program for Queue using linked list was implemented successfully.
EX NO : 9(a)
APPLICATIONS OF LIST ADTS
Date:
AIM:-
To write a ‘C ' program for implementing addition of two polynomials using list.
ALGORITHM:-
PROGRAM :-
#include <stdio.h>
typedef struct pnode
{
float coef;
int exp;
struct pnode *next;
}p;
p *getnode();
void main()
{
p *p1,*p2,*p3;
p *getpoly(),*add(p*,p*);
void display(p*);
clrscr();
printf(“\n enter first polynomial”);
p1=getpoly();
printf(“\n enter second polynomial”);
p2=getpoly();
printf(“\nthe first polynomial is”);
display(p1);
printf(“\nthe second polynomial is”);
display(p2);
p3=add(p1,p2);
printf(“\naddition of two polynomial is :\n”);
display(p3);
}
p *getpoly()
{
p *temp,*New,*last;
int flag,exp;
char ans;
float coef;
temp=NULL;
flag=1;
printf(“\nenter the polynomial in descending order of exponent”);
do
{
printf(“\nenter the coef & exponent of a term”);
scanf(“%f%d”,&coef,&exp);
New=getnode();
if(New==NULL)
printf(“\nmemory cannot be allocated”);
New->coef=coef;
New->exp=exp;
if(flag==1)
{
temp=New;
last=temp;
flag=0;
}
else
{
last->next=New;
last=New;
}
printf(“\ndou want to more terms”);
ans=getch();
}
while(ans==’y');
return(temp);
}
p *getnode()
{
p *temp;
temp=(p*) malloc (sizeof(p));
temp->next=NULL;
return(temp);
}
void display(p*head)
{
p*temp;
temp=head;
if(temp==NULL)
printf(“\npolynomial empty”);
while(temp->next!=NULL)
{
printf(“%0.1fx^%d+”,temp->coef,temp->exp);
temp=temp->next;
}
printf(“\n%0.1fx^%d”,temp->coef,temp->exp);
getch();
}
p*add(p*first,p*second)
{
p *p1,*p2,*temp,*dummy;
char ch;
float coef;
p *append(int,float,p*);
p1=first;
p2=second;
temp=(p*)malloc(sizeof(p));
if(temp==NULL)
printf(“\nmemory cannot be allocated”);
dummy=temp;
while(p1!=NULL&&p2!=NULL)
{
if(p1->exp==p2->exp)
{
coef=p1->coef+p2->coef;
temp=append(p1->exp,coef,temp);
p1=p1->next;
p2=p2->next;
}
else
if(p1->expexp)
{
coef=p2->coef;
temp=append(p2->exp,coef,temp);
p2=p2->next;
}
else
if(p1->exp>p2->exp)
{
coef=p1->coef;
temp=append(p1->exp,coef,temp);
p1=p1->next;
}
}
while(p1!=NULL)
{
temp=append(p1->exp,p1->coef,temp);
p1=p1->next;
}
while(p2!=NULL)
{
temp=append(p2->exp,p2->coef,temp);
p2=p2->next;
}
temp->next=NULL;
temp=dummy->next;
free(dummy);
return(temp);
}
p*append(int Exp,float Coef,p*temp)
{
p*New,*dum;
New=(p*)malloc(sizeof(p));
if(New==NULL)
printf(“\ncannot be allocated”);
New->exp=Exp;
New->coef=Coef;
New->next=NULL;
dum=temp;
dum->next=New;
dum=New;
return(dum);
}
OUTPUT :
RESULT :
Thus the C program for implement the concept of addition of two polynomials using list
was implemented successfully.
EX NO : 9(b)
APPLICATIONS OF STACK ADTS
Date:
AIM:-
To write a ‘C ' program that checks if expression is correctly parenthesized using stack.
ALGORITHM:-
PROGRAM :-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int top =-1;
char stack[100];
void push(char);
void pop();
void find_top();
void main()
{
int i;
char a[100];
printf("enter expression\n");
scanf("%s",&a);
for(i =0; a[i]!='\0';i++)
{
if(a[i]=='(')
{
push(a[i]);
}
elseif(a[i]==')')
{
pop(); }
}
find_top();
}
void push(char a)
{
stack[top]= a;
top++;
}
void pop()
{
if(top ==-1)
{
printf("expression is invalid\n");
exit(0);
}
else
{
top--;
}}
void find_top()
{
if(top ==-1)
printf("\nexpression is valid\n");
else
printf("\nexpression is invalid\n");
}
OUTPUT :
enter expression
(a+b)
expression is valid
enter expression
(a+b))
expression is invalid
RESULT :
Thus the C program that checks if expression is correctly parenthesized using
stack was implemented successfully.
EX NO : 9(c)
APPLICATIONS OF QUEUES ADTS
Date:
AIM:-
To write a C program for implementing the concept of priority queue.
ALGORITHM:-
PROGRAM :-
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
void insert_by_priority(int);
void delete_by_priority(int);
void create();
void check(int);
void display_pqueue();
int pri_que[MAX];
int front, rear;
void main()
{
int n, ch;
printf("\n1 - Insert an element into queue");
printf("\n2 - Delete an element from queue");
printf("\n3 - Display queue elements");
printf("\n4 - Exit");
create();
while(1)
{
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case1:printf("\nEnter value to be inserted : ");
scanf("%d",&n);
insert_by_priority(n);
break;
case2:printf("\nEnter value to delete : ");
scanf("%d",&n);
delete_by_priority(n);
break;
case3:
display_pqueue();
break;
case4:
exit(0);
default:
printf("\nChoice is incorrect, Enter a correct choice");
}
}
}
void create()
{
front = rear =-1;
}
void insert_by_priority(int data)
{
if(rear >= MAX -1)
{printf("\nQueue overflow no more elements can be inserted");
return;
}
if((front ==-1)&&(rear ==-1))
{
front++;
rear++;
pri_que[rear]= data;
return;
}
else
check(data);
rear++;
}
void check(int data)
{
int i,j;
for(i =0; i <= rear; i++)
{
if(data >= pri_que[i])
{
for(j = rear +1; j > i; j--)
{
pri_que[j]= pri_que[j -1];
}
pri_que[i]= data;
return;
}
}
pri_que[i]= data;
}
void delete_by_priority(int data)
{
int i;
if((front==-1)&&(rear==-1))
{
printf("\nQueue is empty no elements to delete");
return;
}
for(i =0; i <= rear; i++)
{
if(data == pri_que[i])
{
for(; i < rear; i++)
{
pri_que[i]= pri_que[i +1];
}
pri_que[i]=-99;
rear--;
if(rear ==-1)
front =-1;
return;
}
}
printf("\n%d not found in queue to delete", data);
}
void display_pqueue()
{
if((front ==-1)&&(rear ==-1))
{
printf("\nQueue is empty");
return;
}
for(; front <= rear; front++)
{
printf(" %d ", pri_que[front]);
}
front =0;
}
OUTPUT :
RESULT :
Thus the C program for the concept of priority queue was implemented
successfully.
EX NO : 10 IMPLEMETATION OF BINARY TREES AND OPERATIONS OF BINARY
TREES
Date:
AIM:-
To write a ‘C ' program for implementing the binary tree and operations of binary tree.
ALGORITHM:-
PROGRAM :-
#include<stdlib.h>
#include<stdio.h>
struct bin_tree {
int data;
struct bin_tree * right, * left;
};
typedef struct bin_tree node;
void insert(node ** tree, int val)
{
node *temp = NULL;
if(!(*tree))
{
temp = (node *)malloc(sizeof(node));
temp->left = temp->right = NULL;
temp->data = val;
*tree = temp;
return;
}
if(val < (*tree)->data)
{
insert(&(*tree)->left, val);
}
else if(val > (*tree)->data)
{
insert(&(*tree)->right, val);
}
}
OUTPUT :
Pre Order Display
9
4
2
6
15
12
17
In Order Display
2
4
6
9
12
15
17
Post Order Display
2
6
4
12
17
15
9
Searched node=4
RESULT :
Thus the C program to implement the concept of binary tree and operations of
binary tree was implemented successfully.
EX NO :11
IMPLEMETATION OF BINARY SEARCH
Date :
AIM :
To write a C program to implement the binary search.
ALGORITHM :
PROGRAM :
// Binary Search in C
#include <stdio.h>
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}
OUTPUT :
RESULT :
AIM :
To write a C program to implement of searching techniques.
ALGORITHM :
PROGRAM :
#include <stdio.h>
int main()
{
int arr[50], search, cnt, num;
return 0;
}
OUTPUT :
RESULT :
55
EX NO :13(a)
IMPLEMETATION OF INSERTION SORT
Date :
AIM :
To write a C program to implement the insertion sort.
ALGORITHM :
PROGRAM :
#include <stdio.h>
// Compare key with each element on the left of it until an element smaller than
// it is found.
// For descending order, change key<array[j] to key>array[j].
while (key < array[j] && j >= 0) {
array[j + 1] = array[j];
--j;
}
array[j + 1] = key;
}
}
// Driver code
int main() {
int data[] = {9, 5, 1, 4, 3};
56
int size = sizeof(data) / sizeof(data[0]);
insertionSort(data, size);
printf("Sorted array in ascending order:\n");
printArray(data, size);
}
OUTPUT :
13459
RESULT :
57
EX NO :13(b)
IMPLEMETATION OF MERGE SORT
Date :
AIM :
To write a C program to implement the merge sort.
ALGORITHM :
PROGRAM :
#include <stdio.h>
58
i++;
} else {
arr[k] = M[j];
j++;
}
k++;
}
// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
if (l < r) {
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
// Driver program
int main() {
int arr[] = {6, 5, 12, 10, 9, 1};
int size = sizeof(arr) / sizeof(arr[0]);
59
mergeSort(arr, 0, size - 1);
OUTPUT :
Sorted array:
1 5 6 9 10 12
RESULT :
60
EX NO :13(c)
IMPLEMETATION OF QUICK SORT
Date :
AIM :
To write a C program to implement the quick sort.
ALGORITHM :
PROGRAM :
#include <stdio.h>
int partition (int a[], int start, int end)
{
int pivot = a[end]; // pivot element
int i = (start - 1);
61
if (start < end)
{
int p = partition(a, start, end); //p is the partitioning index
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}
return 0;
}
OUTPUT :
RESULT :
62
EX NO : 14
HASHING – ANY TWO COLLISION TECHNIQUES
Date:
AIM:
To write a C program to implement the concept of Hash functions.
ALGORITHM:
Step 1: Start the program
Step 2: Choose any element of the array to be the pivot.
Step 3: Divide all other elements (except the pivot) into two partitions.
o All elements less than the pivot must be in the first partition.
o All elements greater than the pivot must be in the second partition.
Step 4: Use recursion to sort both partitions.
Step 5: Join the first sorted partition, the pivot, and the second sorted partition.
Step 6: Tables are sorted in linear partition.
Step 6: Stop the program
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int n, value;
int temp, hash;
clrscr();
printf("nEnter the value of n(table size):");
scanf("%d", &n);
do {
printf("nEnter the hash value");
scanf("%d", &value);
hash = value % n;
if (a[hash] == 0) {
a[hash] = value;
printf("na[%d]the value %d is stored", hash, value);
}
else
{
for (hash++; hash < n; hash++) {
if (a[hash] == 0) {
printf("Space is allocated give other value");
a[hash] = value;
printf("n a[%d]the value %d is stored", hash, value);
goto menu;
63
}}
for (hash = 0; hash < n; hash++) {
if (a[hash] == 0) {
printf("Space is allocated give other value");
a[hash] = value;
printf("n a[%d]the value %d is stored", hash, value);
goto menu;
}}
printf("nnERRORn");
printf("nEnter '0' and press 'Enter key' twice to exit");
}
menu:
printf("n Do u want enter more");
scanf("%d", &temp);
}
while (temp == 1);
getch();
}
OUTPUT:
Collision Handling By Linear Probing
Enter The Number:131
Do you wish to continue? (y/n):y
Enter The Number:21
Do you wish to continue? (y/n):y
Enter The Number:3
Do you wish to continue? (y/n):y
Enter The Number:4
Do you wish to continue? (y/n):y
Enter The Number:5
Do you wish to continue? (y/n):y
Enter The Number:8
Do you wish to continue? (y/n):y
Enter The Number:9
Do you wish to continue? (y/n):n
The hash table is…
0 18
1 131
2 21
3 3
4 4
5 5
6 -1
7 -1
8 8
9 9
64
RESULT:
Thus a C program for the concept of Hash function and collision resolution was implemented
successfully.
65
66