Dsgouri
Dsgouri
case 5: DisplayDQ();
break;
default: printf("Invalid choice!\n");
break;
}
printf("Enter 0 to continue: ");
int n;
scanf("%d", &n);
if(n != 0)
break;
} while(1);
}
RESULT:
The Program to Implement Double Ended Queue is Successful and Output is Obtained.
45
ALGORITHM:
1: To create new node, do
typedef struct Node {
int data;
struct Node *next;
}node;
3: Function Insertion_Front(item)
3.1 : Begin
3.2 : Create a new node, new
3.3 : Set new->data = item
3.4 : Set new->next = NULL
3.5 : If head = NULL, Set head = new and go to step 3.7
3.6 : Else, do
3.6.1 : Set new->next = head
3.6.2 : Set head = new
3.7 : End
4: Function Insertion_End(item)
4.1 : Begin
4.2 : Create a new node, new
4.3 : Set new->data = item
4.4 : Set new->next = NULL
4.5 : If head = NULL, Set head = new and go to step 4.7
4.6 : Else, do
4.6.1 : Set *ptr = head
4.6.2 : While ptr->next!=NULL, Set ptr = ptr->next
4.6.3 : Set ptr->next = new
4.7 : End
DATE: 14/10/2024
PROGRAM NO : 13
SINGLY LINKED LIST
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
void In_Front(int x)
{ node *new = (node *)malloc(sizeof(node));
new->data = x;
new->next = NULL;
if(head == NULL)
{ head = new;}
else
{ new->next = head;
head = new; }
printf("Element added.\n");
}
void In_End(int x)
{ node *new = (node *)malloc(sizeof(node));
new->data = x;
new->next = NULL;
if (head == NULL)
{ head = new; }
else
{
47
6: Function Deletion_Front()
6.1 : Begin
6.2 : If head = NULL, display “List is Empty” and go to step 6.4
6.3 : Else, do
6.3.1 : Set *temp = head
6.2.2: Set head= head->next
6.3.3: Dispose temp
6.4 : End
7: Function Deletion_End()
7.1 : Begin
7.2 : If head=NULL, display “List is Empty” and go to step 7.5
7.3 : Else if head->next = NULL, do
7.3.1 : Set *temp = head
7.3.2 : Set head = NULL
7.3.3 : Dispose temp and go to step 7.5
7.4 : Else, do
7.4.1 : Set *prev = head
7.4.2 : Set *cur= head->next
7.4.3 : While cur->next!=NULL, do
7.4.3.1 : Set prev = cur
7.4.3.2 : Set cur = cur->next
7.4.4 : Set prev->next = NULL
7.4.5 : Dispose cur
7.5 : End
8: Function Deletion_Specific(key)
8.1 : Begin
8.2 : If head = NULL, display “List is Empty” and go to step 8.5
48
void Del_Front(){
{ if (head == NULL)
{ printf("List is Empty, Deletion not possible.\n"); }
else
{ node *temp = head;
head = head->next;
free(temp);
printf("Element Deleted.\n"); }
}
9: Function Display()
9.1 : If head = NULL, display “List is Empty” and go to step 9.3
9.2 : Else, do
9.2.1 : Set *ptr = head
9.2.2 : While ptr!=NULL, do
9.2.2.1 : Display ptr->data
9.2.2.2 : Set ptr = ptr->next
9.3 : End
50
void Del_End(){
{ if (head == NULL)
{ printf(“List is Empty, Deletion not possible.\n”); }
else if(head->next == NULL)
{ node *temp = head;
head = NULL;
free(temp);
printf("Element Deleted.\n"); }
else
{ node *prev = head, *cur = head->next;
while (cur->next != NULL){
{ prev = cur;
cur = cur->next; }
prev->next = NULL;
free(cur);
printf(“Element Deleted.\n”);
}
}
void Display()
{ if (head == NULL
{ printf(“List is Empty\n”); }
else
{ node *ptr = head;
printf("List: ");
51
44
while(ptr != NULL)
{ printf("%d ", ptr->data);
ptr = ptr->next; }
printf("\n"); }
}
void main()
{ int c, x, key, n;
printf("1. INSERTION AT FRONT\n");
printf("2. INSERTION AT END\n");
printf("3. DELETION AT FRONT\n");
printf("4. DELETION AT END\n");
printf("5. INSERTION AFTER A SPECIFIC NODE\n");
printf("6. DELETION OF SPECIFIC NODE\n");
printf("7. DISPLAY\n");
do
{ printf("Enter your choice: ");
scanf("%d", &c);
switch(c)
{ case 1: printf("Enter data: ");
scanf("%d", &x);
In_Front(x);
break;
case 2: printf("Enter data: ");
scanf("%d", &x);
In_End(x);
break;
case 3: Del_Front();
break;
case 4: Del_End();
break;
case 5: printf("Enter key: ");
scanf("%d", &key);
printf("Enter data: ");
scanf("%d", &x);
In_spec(key, x);
break;
case 6: printf("Enter key: ");
45
OUTPUT :
1. INSERTION AT FRONT
2. INSERTION AT END
3. DELETION AT FRONT
4. DELETION AT END
5. INSERTION AFTER A SPECIFIC NODE
6. DELETION OF SPECIFIC NODE
7. DISPLAY
Enter your choice: 1
Enter data: 2
Element added.
Enter 0 to continue! 0
Enter your choice: 2
Enter data: 4
Element added.
Enter 0 to continue! 0
Enter your choice: 5
Enter key: 2
Enter data: 3
Element added.
Enter 0 to continue! 0
Enter your choice: 7
List: 2 3 4
Enter 0 to continue! 0
Enter your choice: 3
Element Deleted.
Enter 0 to continue! 0
Enter your choice: 4
Element Deleted.
Enter 0 to continue! 0
Enter your choice: 7
List: 3
Enter 0 to continue! 1
46
scanf("%d", &key);
Del_spec(key);
break;
case 7: Display();
break;
default:printf("Invalid choice!\n");
break;
}
printf("Enter 0 to continue! ");
scanf("%d", &n);
if(n != 0)
break;
} while(1);
}
RESULT:
The Program to Implement Singly Linked List is Successful and Output is Obtained.
47
ALGORITHM:
1: To create new node, do
typedef struct Node {
int data;
struct Node *next;
}node;
3: Function Push(item)
3.1 : Begin
3.2 : Create a new node, new
3.3 : Set new->data = item
3.4 : Set new->next = top
3.5 : Set top=new
3.6 : End
4: Function Pop()
4.1 : Begin
4.2 : If top = NULL, display “Stack is Empty” and go to step 4.4
4.3 : Else, do
4.3.1 : Set *temp = top
4.3.2 : Set top = top->next
4.3.3 : Set temp=NULL
4.3.4 : Dispose temp
4.4 : End
5: Function Peek()
5.1 : Begin
5.2 : If top= NULL, display “Stack is Empty”, and go to step 5.4
5.3 : Else, display top->data
5.4 : End
6: Function Display()
6.1 : Begin
6.2 : If top = NULL, display “Stack is Empty” and go to step 6.4
6.3 : Else, do
6.3.1 : Set *temp = top
48
DATE: 14/10/2024
PROGRAM NO : 14
STACK USING LINKED LIST
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
void Push(int x)
{ node *new=(node *)malloc(sizeof(node));
new->data=x;
new->next=top;
top=new;
printf("Element Added.\n");
}
void Pop(){
{ if(top==NULL)
printf("Stack is Empty.\n");
else
{ node *temp=top;
printf("Deleted element is %d \n", temp->data);
top=top->next;
temp=NULL;
free(temp); }
}
void Peek()
49
{ if(top==NULL)
printf("Stack is Empty.\n");
else
{ printf("Top Element: %d \n", top->data); }
}
void Display()
{ node *temp=top;
if (top == NULL)
printf("Stack is Empty\n");
else
{ printf("Stack: ");
while(temp != NULL)
{ printf("%d ", temp->data);
temp = temp->next; }
printf("\n");
}
}
void main()
{ int c, x, n;
printf("1. PUSH\n");
printf("2. POP\n");
printf("3. PEEK\n");
printf("4. DISPLAY\n");
do
{ printf("Enter your choice: ");
scanf("%d", &c);
switch(c)
{ case 1: printf("Enter data: ");
scanf("%d", &x);
Push(x);
break;
case 2: Pop(x);
break;
case 3: Peek();
break;
51
OUTPUT :
1. PUSH
2. POP
3. PEEK
4. DISPLAY
Enter your choice: 1
Enter data: 1
Element Added.
Enter 0 to continue! 0
Enter your choice: 1
Enter data: 2
Element Added.
Enter 0 to continue! 0
Enter your choice: 4
Stack: 2 1
Enter 0 to continue! 0
Enter your choice: 2
Deleted element is 2
Enter 0 to continue! 0
Enter your choice: 3
Top Element: 1
Enter 0 to continue! 0
Enter your choice: 4
Stack: 1
Enter 0 to continue! 1
52
case 4: Display();
break;
default: printf("Invalid choice!\n");
break;
}
printf("Enter 0 to continue! ");
scanf("%d", &n);
if(n != 0)
break;
} while(1);
}
RESULT:
The Program to Implement Stack using Linked List is Successful and Output is Obtained.
53
ALGORITHM:
1: To create new node, do
typedef struct Node {
int data;
struct Node *next;
}node;
3: Function Enqueue(item)
3.1 : Begin
3.2 : Create a new node, new
3.3 : Set new->data = item
3.4 : Set new->next = NULL
3.5 : If Front = Rear = NULL, Set Front = Rear = new , and go to step 3.7
3.6 : Else, Set Rear->next=new
3.7 : End
4: Function Dequeue()
4.1 : Begin
4.2 : If Front = NULL, display “Queue is Empty” and go to step 4.5
4.3 : Else if Front = Rear, do
4.3.1 : Set *temp = Front
4.3.2 : Set Front=Rear=NULL
4.3.3 : Dispose temp and go to step 4.5
4.4 : Else, do
4.4.1 : Set temp = Front
4.4.2 : Set Front=Front->next
4.4.3 : Dispose temp
4.5 : End
5: Function Display()
5.1 : Begin
5.2 : If Front = NULL, display “Queue is Empty”, and go to step 5.4
5.3 : Else, do
5.3.1 : Set *pointer=Front
5.3.2 : While pointer != NULL, do
5.3.2.1 : Display pointer->data
54
DATE: 14/10/2024
PROGRAM NO : 15
QUEUE USING LINKED LIST
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
node *R = NULL;
node *F = NULL;
void Enqueue(int x)
{ node *new=(node *)malloc(sizeof(node));
new->data=x;
new->next=NULL;
if(F==NULL && R==NULL)
{ F=new;
R=new; }
else
{ R->next=new;
R=new; }
printf("Element Added.\n");
}
void Dequeue()
{ if(F==NULL)
{ printf("Queue is Empty.\n");}
else if(F==R)
{ node *temp=F;
printf("Deleted element is %d \n", temp->data);
55
F=NULL;
R=NULL;
free(temp);}
else
{ node *temp=F;
printf("Deleted element is %d \n", temp->data);
F=F->next;
temp=NULL;
free(temp); }
}
void Display()
{ if (F == NULL)
{ printf("Queue is Empty\n"); }
else
{ node *temp=F;
printf("Queue: ");
while(temp != NULL){
printf("%d ", temp->data);
temp = temp->next; }
printf("\n"); }
}
void main(){
{ int c, x, n;
printf("1. ENQUEUE\n");
printf("2. DEQUEUE\n");
printf("3. DISPLAY\n");
do{
{ printf("Enter your choice: ");
scanf("%d", &c);
switch(c)
{ case 1: printf("Enter data: ");
scanf("%d", &x);
Enqueue(x);
break;
case 2: Dequeue();
break;
57
OUTPUT :
1. ENQUEUE
2. DEQUEUE
3. DISPLAY
Enter your choice: 1
Enter data: 2
Element Added.
Enter 0 to continue! 0
Enter your choice: 3
Queue: 2
Enter 0 to continue! 0
Enter your choice: 1
Enter data: 7
Element Added.
Enter 0 to continue! 0
Enter your choice: 1
Enter data: 3
Element Added.
Enter 0 to continue! 0
Enter your choice: 3
Queue: 2 7 3
Enter 0 to continue! 0
Enter your choice: 2
Deleted element is 2
Enter 0 to continue! 0
Enter your choice: 3
Queue: 7 3
Enter 0 to continue! 1
58
case 3: Display();
break;
default: printf("Invalid choice!\n");
break;
}
printf("Enter 0 to continue! ");
scanf("%d", &n);
if(n != 0)
break;
} while(1);
}
RESULT:
The Program to Implement Queue using Linked List is Successful and Output is Obtained.
59
ALGORITHM:
1: To create new node, do
typedef struct Node {
int data;
struct Node *rlink, *llink;
}node;
3: Function Insertion_Front(item)
3.1 : Begin
3.2 : Create a new node, new
3.3 : Set new->data = item
3.4 : Set new->rlink = NULL and new->llink = NULL
3.5 : If head = NULL, Set head = new and go to step 3.7
3.6 : Else, do
3.6.1 : Set new->rlink= head and head->llink = new
3.6.2 : Set head = new
3.7 : End
4: Function Insertion_End(item)
4.1 : Begin
4.2 : Create a new node, new
4.3 : Set new->data = item
4.4 : Set new->rlink = NULL and new->llink = NULL
4.5 : If head = NULL, Set head = new and go to step 4.7
4.6 : Else, do
4.6.1 : Set *ptr = head
4.6.2 : While ptr->rlink!=NULL, Set ptr = ptr->rlink
4.6.3 : Set ptr->rlink = new and new->llink=ptr
4.7 : End
DATE: 19/10/2024
PROGRAM NO : 16
DOUBLY LINKED LIST
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
void In_End(int x)
{ node *new = (node *)malloc(sizeof(node));
new->data = x;
new->rlink = NULL;
new->llink = NULL;
if (head == NULL)
61
6: Function Deletion_Front()
6.1 : Begin
6.2 : If head = NULL, display “List is Empty” and go to step 6.5
6.3 : Else if head->rlink = NULL, do
6.3.1 : Set *temp = head
6.2.2: Set head= NULL
6.3.3: Dispose temp
6.4 : Else, do
6.4.1 : Set *temp=head
6.4.1 : Set head=head->rlink
6.4.1 : Set head->llink=NULL
6.4.1 : Dispose temp
6.5 : End
7: Function Deletion_End()
7.1 : Begin
7.2 : If head=NULL, display “List is Empty” and go to step 7.5
7.3 : Else if head->rlink = NULL, do
7.3.1 : Set *temp = head
7.3.2 : Set head = NULL
7.3.3 : Dispose temp and go to step 7.5
7.4 : Else, do
7.4.1 : Set *ptr= head
7.4.2 : While ptr->rlink!=NULL, do
7.4.2.1 : Set ptr = ptr->rlink
7.4.3 : Set ptr->llink->rlink = NULL
7.4.5: Set ptr->llink=NULL
62
head = new;
else
{ node *ptr = head;
while(ptr->rlink != NULL)
{ ptr = ptr->rlink; }
ptr->rlink = new;
new->llink=ptr; }
printf("Element added.\n");
}
void Del_Front()
63
8: Function Deletion_Specific(key)
8.1 : Begin
8.2 : If head = NULL, display “List is Empty” and go to step 8.5
8.3 : Else if head->rlink = NULL, do
8.3.1 : If head->data = key, do
8.3.1.1 : Set *temp=head
8.3.1.2 : Set head=NULL
8.3.1.3 : Dispose temp and go to step 8.5
8.3.2 : Else, display “Search not found” and go to step 8.5
8.4 : Else, do
8.4.1 : Set *ptr = head
8.4.2 : While ptr->rlink !=NULL and ptr->data!=key, do
8.4.3.1: Set ptr = ptr->rlink
8.4.3 : If ptr->data!=key, display “Search not Found” and go to step 8.5
8.4.4 : Else, do
8.4.4.1 : Set ptr->llink->rlink = ptr->rlink
8.5 : End
9: Function Display()
9.1 : If head = NULL, display “List is Empty” and go to step 9.3
9.2 : Else, do
9.2.1 : Set *ptr = head
9.2.2 : While ptr->rlink!=NULL, do
9.2.2.1 : Display ptr->data
9.2.2.2 : Set ptr = ptr->rlink
9.3 : End
64
{ if (head == NULL)
{ printf("List is Empty, Deletion not possible.\n"); }
else if(head->rlink==NULL) {
node *temp = head;
head = NULL;
free(temp);
printf("Element Deleted.\n"); }
else{
node *temp=head;
head=head->rlink;
head->llink=NULL;
free(temp);
printf("Element Deleted.\n"); }
}
printf("Element Deleted.\n");}
else {
printf("Key not found, Deletion not possible.\n"); } }
}
void Del_End()
{ if (head == NULL){
printf("List is Empty, Deletion not possible.\n"); }
else if(head->rlink == NULL){
node *temp = head;
head = NULL;
free(temp);
printf("Element Deleted.\n"); }
else {
node *ptr = head;
while (ptr->rlink != NULL){
ptr = ptr->rlink; }
ptr->llink->rlink = NULL;
ptr->llink=NULL;
ptr->rlink=NULL;
printf("Element Deleted.\n"); }
}
void Display()
{ if (head == NULL){
printf("List is Empty\n"); }
else {
node *ptr = head;
printf("List: ");
while(ptr->rlink != NULL){
printf("%d ", ptr->data);
ptr = ptr->rlink; }
printf("%d ", ptr->data);
printf("\n"); }
}
void main()
{ int c, item, x, key, n;
67
OUTPUT :
1. INSERTION AT FRONT
2. INSERTION AT END
3. DELETION AT FRONT
4. DELETION AT END
5. INSERTION AFTER A SPECIFIC NODE
6. DELETION OF SPECIFIC NODE
7. DISPLAY
Enter your choice: 1
Enter data: 3
Element added.
Enter 0 to continue! 0
Enter your choice: 2
Enter data: 4
Element added.
Enter 0 to continue! 0
Enter your choice: 7
List: 3 4
Enter 0 to continue! 0
Enter your choice: 5
Enter key: 3
Enter data: 5
Element added.
Enter 0 to continue! 0
Enter your choice: 7
List: 3 5 4
Enter 0 to continue! 0
Enter your choice: 6
Enter key: 5
Element Deleted.
Enter 0 to continue! 0
Enter your choice: 3
Element Deleted.
Enter 0 to continue! 0
Enter your choice: 2
Enter data: 7
Element added.
Enter 0 to continue! 0
68
RESULT:
The Program to Implement Doubly Linked List is Successful and Output is Obtained
71
ALGORITHM:
1: To create new node, do
typedef struct Node {
int data;
struct Node *next;
}node;
3: Function Insertion_Front(item)
3.1 : Begin
3.2 : Create a new node, new
3.3 : Set new->data = item
3.4 : Set new->next = NULL
3.5 : If head = NULL, do
3.5.1 : Set head = new
3.5.2 : Set head->next = head and go to step 3.7
3.6 : Else
3.6.1 : Set *ptr = head
3.6.2 : While ptr->next != head, Set ptr = ptr->next
3.6.3 : Set new->next = head
3.6.4 : Set head = new
3.6.5 : Set ptr->next = head
3.7 : End
4: Function Insertion_End(item)
4.1 : Begin
4.2 : Create a new node, new
4.3 : Set new->data = item
4.4 : Set new->next = NULL
4.5 : If head = NULL
4.5.1 : Set head = new
4.5.2 : Set head->next = head and go to step 4.7
4.6 : Else, do
4.6.1 : Set *ptr = head
4.6.2 : While ptr->next != head, Set ptr = ptr->next
4.6.3 : Set ptr->next = new
4.6.4 : Set new->next = head
72
DATE: 19/10/2024
PROGRAM NO : 17
CIRCULAR LINKED LIST
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
void In_Front(int x)
{ node *new = (node *)malloc(sizeof(node));
new->data = x;
new->next = NULL;
if(head == NULL){
head = new;
head->next=new; }
else {
node *ptr=head;
while(ptr->next!=head){
ptr=ptr->next;}
new->next = head;
head = new;
ptr->next=head; }
printf("Element added.\n");
}
void In_End(int x)
{ node *new = (node *)malloc(sizeof(node));
new->data = x;
73
4.7 : End
6: Function Deletion_Front()
6.1 : Begin
6.2 : If head = NULL
Display "List is Empty, Deletion not possible."
6.3 : Else if head->next == head
6.3.1 : Set *temp=head
6.3.2 : Set head = NULL
6.3.3 : Dispose temp
6.4 : Else
6.4.1 : Set *temp = head
6.4.2 : Set *ptr = head
6.4.3 : While ptr->next != head, Set ptr = ptr->next
6.4.4 : Set head = head->next
6.4.5 : Set ptr->next = head
6.4.6 : Dispose temp
6.5 : End
7: Function Deletion_End()
7.1 : Begin
7.2 : If head = NULL, Display "List is Empty, Deletion not possible."
7.3 : Else if head->next == head, do
74
new->next = NULL;
if (head == NULL){
head = new;
head->next=head; }
else {
node *ptr = head;
while(ptr->next != head){
ptr = ptr->next; }
ptr->next = new;
new->next=head; }
printf("Element added.\n");
}
void Del_Front()
{ if (head == NULL){
printf("List is Empty, Deletion not possible.\n"); }
else if(head->next==head) {
node *temp = head;
head = NULL;
free(temp);
printf("Element Deleted.\n"); }
75
8: Function Deletion_Specific(key)
8.1 : Begin
8.2 : If head = NULL, Display "List is Empty, Deletion not possible."
8.3 : Else if head->next = head, do
8.3.1 : If head->data = key, do
8.3.2.1 : Set *temp = head
8.3.2.2 : Set head = NULL
8.3.2.3 : Dispose temp
8.3.2 : Else, Display "Key not found, Deletion not possible."
8.4 : Else
8.4.1 : Set *prev = head
8.4.2 : Set *cur = head
8.4.3 : While cur->data != key and cur->next != head, do
8.4.3.1 : Set prev = cur
8.4.3.2 : Set cur = cur->next
8.4.4 : If cur->data != key, Display "Key not found, Deletion not possible."
8.4.5 : Else, do
8.4.5.1 : Set prev->next = cur->next
8.4.5.2 : Dispose cur
8.5 : End
9: Function Display()
9.1 : Begin
9.2 : If head = NULL, Display "List is Empty"
9.3 : Else, do
76
else{
node *temp=head;
node *ptr=head;
while(ptr->next!=head){
ptr=ptr->next;}
head=head->next;
ptr->next=head;
printf("Element Deleted.\n"); }
}
void Del_End()
{ if (head == NULL){
printf("List is Empty, Deletion not possible.\n"); }
77
OUTPUT :
1. INSERTION AT FRONT
2. INSERTION AT END
3. DELETION AT FRONT
4. DELETION AT END
5. INSERTION AFTER A SPECIFIC NODE
6. DELETION OF SPECIFIC NODE
7. DISPLAY
Enter your choice: 1
Enter data: 5
Element added.
Enter 0 to continue! 0
Enter your choice: 2
Enter data: 7
Element added.
Enter 0 to continue! 0
Enter your choice: 5
Enter key: 5
Enter data: 6
Element added.
Enter 0 to continue! 0
Enter your choice: 7
List: 5 6 7
Enter 0 to continue! 0
Enter your choice: 6
Enter key: 6
Element Deleted.
Enter 0 to continue! 0
Enter your choice: 4
Element Deleted.
78
void Display()
{ if (head == NULL){
printf("List is Empty\n"); }
else {
node *ptr = head;
printf("List: ");
while(ptr->next != head){
printf("%d ", ptr->data);
ptr = ptr->next; }
printf("%d ", ptr->data);
printf("\n");}
}
void main()
{ int c, item, x, key, n;
printf("1. INSERTION AT FRONT\n");
printf("2. INSERTION AT END\n");
printf("3. DELETION AT FRONT\n");
printf("4. DELETION AT END\n");
printf("5. INSERTION AFTER A SPECIFIC NODE\n");
printf("6. DELETION OF SPECIFIC NODE\n");
printf("7. DISPLAY\n");
do{
79
Enter 0 to continue! 0
Enter your choice: 7
List: 5
Enter 0 to continue! 0
Enter your choice: 3
Element Deleted.
Enter 0 to continue! 0
Enter your choice: 7
List is Empty
Enter 0 to continue! 1
80
RESULT:
The Program to Implement Circular Linked List is Successful and Output is Obtained.
81
ALGORITHM:
1: To create new node, do
typedef struct Node {
int exp, coef;
struct Node *next;
}node;
DATE: 04/11/2024
PROGRAM NO : 18
POLYNOMIAL ADDITION USING LINKED LIST
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
4: Function display(head)
4.1 : Begin
4.2 : If head == NULL, Display "Polynomial is Empty" and go to step 4.4
4.3 : Else
4.3.1 : Set *ptr = head
4.3.2 : While ptr != NULL
4.3.2.1 : Display ptr->coef and ptr->exp
4.3.2.1 : Set ptr = ptr->next
4.3.2.1 : If ptr != NULL, output " + "
4.4 : End
5: Function Read_Polynomial()
5.1 : Begin
5.2 : Input number of terms, n
84
q = q->next; }
new_node->next = NULL;
if (rhead == NULL) {
rhead = new_node;
r = new_node; }
else {
r->next = new_node;
r = new_node; } }
while (p != NULL) {
node *new_node = (node *)malloc(sizeof(node));
new_node->coef = p->coef;
new_node->exp = p->exp;
new_node->next = NULL;
p = p->next;
if (rhead == NULL) {
rhead = new_node;
r = new_node; }
else {
r->next = new_node;
r = new_node; }
}
while (q != NULL) {
node *new_node = (node *)malloc(sizeof(node));
new_node->coef = q->coef;
new_node->exp = q->exp;
new_node->next = NULL;
q = q->next;
if (rhead == NULL) {
rhead = new_node;
r = new_node; }
else {
r->next = new_node;
r = new_node; }
}}
}
{ if (rhead == NULL) {
printf("Polynomial is Empty\n"); }
else {
node *ptr = rhead;
while (ptr != NULL) {
printf("%dx^%d", ptr->coef, ptr->exp);
ptr = ptr->next;
if (ptr != NULL) {
printf(" + "); } }
printf("\n"); }
}
void main()
{ int n, m;
printf("Polynomial addition\n");
printf("Enter number of terms of first polynomial: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
node *new_node = (node *)malloc(sizeof(node));
printf("Enter coefficient of term %d: ", (i + 1));
scanf("%d", &new_node->coef);
printf("Enter exponent of term %d: ", (i + 1));
scanf("%d", &new_node->exp);
new_node->next = phead;
phead = new_node; }
printf("Enter number of terms of second polynomial: ");
scanf("%d", &m);
for (int i = 0; i < m; i++) {
node *new_node = (node *)malloc(sizeof(node));
printf("Enter coefficient of term %d: ", (i + 1));
scanf("%d", &new_node->coef);
printf("Enter exponent of term %d: ", (i + 1));
scanf("%d", &new_node->exp);
new_node->next = qhead;
qhead = new_node; }
add(phead, qhead);
display(rhead);
}
87
OUTPUT :
Polynomial addition
Enter number of terms of first polynomial: 2
Enter coefficient of term 1: 2
Enter exponent of term 1: 1
Enter coefficient of term 2: 3
Enter exponent of term 2: 2
Enter number of terms of second polynomial: 3
Enter coefficient of term 1: 4
Enter exponent of term 1: 1
Enter coefficient of term 2: 5
Enter exponent of term 2: 3
Enter coefficient of term 3: 7
Enter exponent of term 3: 7
7x^7 + 5x^3 + 3x^2 + 6x^1
88
RESULT:
The Program to Implement Polynomial addition using Linked List is Successful and Output is
Obtained.
89
ALGORITHM:
1: To create new node, do
typedef struct Node {
int data;
struct Node *lchild, *rchild;
}node;
DATE: 04/11/2024
PROGRAM NO : 19
BINARY SEARCH TREE
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{ int info;
struct Node *lchild, *rchild;
}node;
node *root=NULL;
OUTPUT :
1. Insert
2. Display
3. Quit
Enter your choice:1
Enter the element to be inserted
5
Enter your choice:1
Enter the element to be inserted
4
Enter your choice:1
Enter the element to be inserted
7
Enter your choice:1
Enter the element to be inserted
8
Enter your choice:2
4 5 7 8
Enter your choice:3
Exiting...
92
par->lchild=newnode(val);
else
par->rchild=newnode(val);
}
}
void inorder(node* p)
{ if(p!=NULL)
{ inorder(p->lchild);
printf("%d\t", p->info);
inorder(p->rchild); }
}
void main()
{ int choice,val;
printf("\n1. Insert\n2. Display\n3. Quit\n");
do
{ printf("Enter your choice:");
scanf("%d", &choice);
switch(choice)
{ case 1: printf("Enter the element to be inserted\n");
scanf("%d", &val);
insert(val);
break;
case 2: inorder(root);
printf("\n");
break;
case 3: printf("Exiting...\n");
break;
RESULT:
The Program to Implement Binary Search Tree is Successful and Output is Obtained.
93
ALGORITHM:
1. Begin
2. swap(*a,*b)
2.1. Declare temp=a
2.2. Set a=b
2.3. Set b=temp
3. heapify(a[], size, i)
3.1. Declare lc=2*i
3.2. Declare rc=(2*i)+1
3.3. Declare large=i
3.4. Check if(lc<=size and a[lc]>a[large])
3.4.1. Set large=lc
3.5. Check if(rc<=size and a[rc]>a[large])
3.5.1. Set large=rc
3.6. Check if(large!=i)
3.6.1. swap(&a[i], &a[large])
3.6.2. heapify(a,size,large)
4. End function
5. buildheap(a[], n)
5.1. for i=n/2, i>=1 do
5.1.1. heapify(a, n, i)
6. End function
7. heapsort(a[], n)
7.1. buildheap(a,n)
7.2. for i=n, i>=1 do
7.2.1. swap(&a[1], &a[i])
7.2.2. heapify(a,i-1,1)
8. End function
9. main()
9.1. Declare size
9.2. Print, Enter the number of elements.
9.3. Read the size.
9.4. Declare a[size+1]
9.5. Print, Enter elements.
9.6. for i=1, i<=size do
9.6.1. Read the elements.
9.7. heapsort(a, size)
9.8. for i=1, i<=size do
9.8.1. Print, a[i]
11. End
94
DATE: 04/11/2024
PROGRAM NO : 20
HEAP SORT
PROGRAM:
#include<stdio.h>
void swap(int *a,int *b)
{ int temp=*a;
*a=*b;
*b=temp;
}
OUTPUT :
Enter the number of elements
5
Enter 5 elements
6
1
8
7
2
1 2 6 7 8
96
void main()
{ int size;
printf("Enter the number of elements\n");
scanf("%d",&size);
int a[size+1];
printf("Enter %d elements\n", size);
for(int i=1;i<=size;i++)
scanf("%d", &a[i]);
heapsort(a, size);
for(int i=1;i<=size;i++)
printf("%d\t", a[i]);
}
RESULT:
The Program to Implement Heap Sort is Successful and Output is Obtained.