Dsa Programs 13-19 Aniket
Dsa Programs 13-19 Aniket
Dsa Programs 13-19 Aniket
Prectical=8
Create a “Linked List” structure with the following data members:
1.A Data
2.A link to the next node
Perform the following operations on stack using user-defined functions:
1. Insert a value X at the first place
2. Insert a value X at the end of the list
3. Insert a value X at the place so that it preserves the ordering of the terms in the increasing
order.
i. Delete an element whose address is given by X
ii. Copy a linked linear list.
Code:
// Create Queue and performing its function
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head;
void beginsert ();
void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void writeToFile();
void search();
void main (){
int choice =0;
while(choice != 10)
{
printf("\nChoose any one option:\n");
printf("\n1.Insert at first");
printf("\n2.Insert at last");
printf("\n3.Insert at any random location");
printf("\n4.Delete from Beginning");
printf("\n5.Delete from last");
printf("\n6.Delete node after specified location");
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node *));
if(ptr == NULL){
printf("\nOVERFLOW");
}
else{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));
if(ptr == NULL){
printf("\nOVERFLOW");
}
else{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL){
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL){
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
}}}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL){
printf("\nOVERFLOW");
}
Else
{
printf("\nEnter element value\n ");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++){
temp = temp->next;
if(temp == NULL){
printf("\ncan't insert\n");
return;
}}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}}
void begin_delete()
{
struct node *ptr;
if(head == NULL){
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}}
void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL){
printf("\nlist is empty");
}
else if(head -> next == NULL){
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}
else{
ptr = head;
while(ptr->next != NULL){
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion\n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++){
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL){
printf("\nCan't delete");
return;
}}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search(){
struct node *ptr;
int item,i=0,flag;
ptr = head;
if(ptr == NULL){
printf("\nEmpty List\n");
}
Else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL){
if(ptr->data == item){
printf("item found at location %d ",i+1);
flag=0;
}
else{
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("Item not found\n");
}}}
void display(struct node* head){
struct node *ptr = head;
if(ptr == NULL){
printf("Nothing to print");
}
Else
{
printf("Elements in a List:");
while (ptr!=NULL){
printf("\n%d",ptr->data);
ptr = ptr -> next;
}}}
void writeToFile(){
char *filename = "test.txt";
FILE *fp = fopen(filename, "w");
struct node *ptr;
ptr = head;
if(fp==NULL){
printf("Error\n");
}
else{
while(ptr!=NULL){
Prectical=9
Create a “Queue” structure with following Data members:
1. Integer Array
2. Size of the Array
Perform the following operations on Simple queue using user-defined functions: 1. Insert an
element
2. Remove an element
3. Display
4. Isfull
5. Isempty Create a file which stores all values of Array
Code
// Create Queue and performing its function
#include <stdio.h>
#define MAX 50
void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
void main(){
int choice;
while (1)
{
printf("1.Insert \n");
printf("2.Delete\n");
printf("3.Display \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice \n");
}}}
void insert(){
int add_item;
if (rear == MAX - 1)
{
printf("Queue Overflow \n");
}
else{
if (front == - 1)
front = 0;
printf("Insert the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}}
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;
}}
void display()
{
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");
}}
Output:
Prectical=10
Create a “Queue” user-defined structure with the following data members:
1. A Data
2. A link to the next node
Perform the following operations on Simple queue using user-defined functions:
1. Insert an element
2. Remove an element
3. Display
4. Isfull
5. Isempty
Create a file which stores all values of list.
Code:
//Create user-defined Queue
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
struct node *front;
struct node *rear;
void insert();
void delete();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n1.Insert an element\n2.Delete an element\n3.Display thequeue\n4.Exit\n");
printf("\nEnter your choice?\n");
scanf("%d",& choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}}}
void insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = ptr;
rear = ptr;
rear->next = NULL;
}}}
void delete ()
{
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return;
}
else
{
ptr = front;
front = front -> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{
printf("\nValues are:\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}}}
Output:
Prectical=11
Create a “Circular Queue” structure with following Data members:
1. Integer Array
2. Size of the Array
Perform the following operations on Circular queue using user-defined functions:
1. Insert an element
2. Remove an element
3. Display
4. Isfull
5. Isempty
Create a file which stores all values of Array.
// Create Queue and performing its function
#include<stdio.h>
# define MAX 5
int cqueue_arr[MAX];
int front = -1;
int rear = -1;
void insert(int item)
{
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow n");
return;
}
if(front == -1)
{
front = 0;
rear = 0;
}
else
{
if(rear == MAX-1)
rear = 0;
else
rear = rear+1;
}
cqueue_arr[rear] = item ;
}
void deletion()
{
if(front == -1)
{
printf("Queue Underflown");
return ;
}
printf("Element deleted from queue is : %dn",cqueue_arr[front]);
if(front == rear)
{
front = -1;
rear=-1;
}
else
{
if(front == MAX-1)
front = 0;
else
front = front+1;
}
}
void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
Prectical=12
Create a “Circular Queue” user-defined structure with the following data members:
1. A Data
2. A link to the next node
Perform the following operations on Circular queue using user-defined functions:
1. Insert an element
2. Remove an element
3. Display
4. Isfull
5. Isempty
Create a file which stores all values of list.
Code:
//Create user-define Circular Queue
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node *f = NULL;
struct node *r = NULL;
void enqueue(int d){
struct node* n;
n = (struct node*)malloc(sizeof(struct node));
n->data = d;
n->next = NULL;
if((r==NULL)&&(f==NULL)){
f = r = n;
r->next = f;
}
else{
r->next = n;
r = n;
n->next = f;
}}
void dequeue(){
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else if(f == r){
f = r = NULL;
free(t);
}
else
{
f = f->next;
r->next = f;
free(t);
}}
void print()
{ // Print the elements of Queue
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else{
do
{
printf("\n%d",t->data);
t = t->next;
}while(t != f);
}}
int main(){
int opt,n,i,data;
printf("Enter Your Choice:-");
do
{
printf("\n\n1 Insert\n2 Display \n3 Delete \n0 for Exit\n");
printf("*********************\n");
printf("Enter your choice:");
scanf("%d",&opt);
switch(opt){
case 1:
printf("\nHow many data you want to enter?");
scanf("%d",&n);
printf("\nEnter your data:");
i=0;
while(i<n){
scanf("%d",&data);
enqueue(data);
i++;
}
break;
case 2:
print();
break;
case 3:
dequeue();
break;
case 0:
break;
default:
printf("\nIncorrect Choice");}}while(opt!=0);
return 0;
}
Output:
Program 13 : Create a user-defined “Linked List” structure with the following data members:
1. A Co-efficient 2. An Exponent 3. A link to the next node
4. Multiplication
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct node{
} *head1 = NULL, *head2 = NULL, *head3 = NULL, *head4 = NULL, *temp, *ptr;
void create();
void main() {
int listno;
while (1){
printf("\ntMenu");
printf("\n\t11.Dispose List.");
printf("\n\t12.Exit");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("\nEnter coefficient?");
scanf("%d", &coefficient);
printf("\nEnter exponent?");
scanf("%d", &exponent);
makenode(coefficient, exponent);
head1 = insertend(head1);
break;
case 2:
display(head1);
break;
case 3:
printf("\nEnter coefficient?");
scanf("%d", &coefficient);
printf("\nEnter exponent?");
scanf("%d", &exponent);
makenode(coefficient, exponent);
head2 = insertend(head2);
break;
case 4:
display(head2);
break;
case 5:
head3 = dispose(head3);
break;
case 6:
display(head3);
break;
case 7:
head3 = dispose(head3);
getch();
break;
case 8:
display(head3);
break;
case 9:
head3 = dispose(head3);
head4 = dispose(head4);
break;
case 10:
display(head4);
break;
case 11:
scanf("%d", &listno);
if (listno == 1)
head1 = dispose(head1);
else if (listno == 2)
head2 = dispose(head2);
else if (listno == 3)
head3 = dispose(head3);
else if (listno == 4)
head4 = dispose(head4);
else
break;
case 12:
exit(0);
default:
break;}}}
void create() {
if (ptr == NULL) {
exit(1); } }
create();
ptr->coeff = c;
ptr->exp = e;
ptr->next = NULL; }
if (head == NULL)
head = ptr;
temp = temp->next;
temp->next = ptr; }
return head;}
if (head == NULL)
temp = temp->next; }
printf("bb "); }
getch(); }
struct node *addtwopoly(struct node *h1, struct node *h2, struct node *h3) {
temp1 = h1;
temp2 = h2;
if (temp1->exp == temp2->exp) {
h3 = insertend(h3); }
else {
makenode(temp1->coeff, temp1->exp);
h3 = insertend(h3);
makenode(temp2->coeff, temp2->exp);
h3 = insertend(h3); }
temp1 = temp1->next;
temp2 = temp2->next; }
makenode(temp2->coeff, temp2->exp);
h3 = insertend(h3);
temp2 = temp2->next; } }
makenode(temp2->coeff, temp2->exp);
h3 = insertend(h3);
temp1 = temp1->next; } }
return h3;}
struct node *subtwopoly(struct node *h1, struct node *h2, struct node *h3) {
temp1 = h1;
temp2 = h2;
if (temp1->exp == temp2->exp) {
h3 = insertend(h3); }
else {
makenode(temp1->coeff, temp1->exp);
h3 = insertend(h3);
makenode(-temp2->coeff, temp2->exp);
h3 = insertend(h3); }
temp1 = temp1->next;
temp2 = temp2->next;}
makenode(temp2->coeff, temp2->exp);
h3 = insertend(h3);
temp2 = temp2->next; } }
makenode(-temp2->coeff, temp2->exp);
h3 = insertend(h3);
temp1 = temp1->next; } }
return h3;}
struct node *multwopoly(struct node *h1, struct node *h2, struct node *h3) {
int res = 0;
display(h1);
display(h2);
temp1 = h1;
h3 = insertend(h3);
temp2 = temp2->next; }
temp1 = temp1->next;}
display(h3);
getch();
temp1 = h3;
res = 0;
res += temp2->coeff;
temp2 = temp2->next;}
if (search(head4, temp1->exp) == 1) {
head4 = insertend(head4);}
temp1 = temp1->next;}
return head4;}
tmp = h;
if (tmp->exp == val)
return 0;
tmp = tmp->next; }
return 1;}
return list;}
list = list->next;
temp = list; }
return list;} }
OUTPUT :
4. Delete last node 5. Delete a node before specified data 6. Insert at first
position
7. Insert at last position 8. Insert a node before specified data 9. Insert a node at
specified position
#include<stdio.h>
#include<stdlib.h>
struct queue{
int data;
node *start=NULL,*rear=NULL;
int choice,i;
void create(){
node *temp;
scanf("%d",&i);
while(i!=-1){
temp = (node*)malloc(sizeof(node));
temp->next=NULL;
temp->data=i;
if(start==NULL){
start=temp;
rear=temp }
else{
rear->next=temp;
rear=temp; }
scanf("%d",&i); }
printf("\n-1 encountered\n"); }
void traverse(){
node *temp;
if(start==NULL){
create(); }
else{ temp=start;
while(temp->next!=NULL){
printf(" %d - >",temp->data);
temp=temp->next; }
printf(" %d",temp->data); } }
void delete_first(){
node *temp;
else{ temp=start;
start=start->next;
free(temp); } }
void delete_last() {
node *temp;
if(start==NULL){
else{ temp=start;
while(temp->next!=NULL){
rear=temp;
temp=temp->next; }
rear->next=NULL;
free(temp); } }
void delete_before(){
node *temp,*prev;
int flag=0,count=0;
if(start==NULL){
return; }
scanf("%d",&i);
temp=start;
while (temp!=NULL){
count++;
if(temp->data==i){
flag=1;
break; }
temp=temp->next; }
else if(count==2){
temp=start;
start=start->next;
free(temp); }
else{ temp=start;
while(temp!=NULL){ prev=temp;
temp=temp->next;
if(temp->next->data==i){
prev->next=temp->next;
free(temp);
break; } } } }
void insert_first() {
node *temp;
if(start==NULL){
return; }
temp = (node*)malloc(sizeof(node));
scanf("%d",&i);
temp->data=i;
temp->next=start;
start=temp; }
void insert_last(){
node *temp;
if(start==NULL){
return; }
temp = (node*)malloc(sizeof(node));
scanf("%d",&i);
temp->data=i;
temp->next=NULL;
rear->next=temp;
rear=temp; }
void insert_before() {
node *temp,*trav,*prev;
int val,flag=0,count=0;
if(start==NULL){
return; }
scanf("%d",&val);
temp=start;
while (temp!=NULL){
count++;
if(temp->data==val){
flag=1;
break; }
temp=temp->next; }
if(flag==1){
temp = (node*)malloc(sizeof(node));
scanf("%d",&i);
temp->data=i;
temp->next=NULL;
if(count==1){
temp->next=start;
start=temp; }
else{
trav=start;
while(trav!=NULL){
prev=trav;
trav=trav->next;
if(trav->data==val){
prev->next=temp;
temp->next=trav;
break; } } } }
void insert_specified(){
node *temp,*trav,*prev;
int val,flag=0,count=0;
if(start==NULL){
return; }
scanf("%d",&val);
val--;
if(val<1){
insert_first(); }
else{
trav=start;
count++;
prev=trav;
trav=trav->next; }
if(count==val){
temp = (node*)malloc(sizeof(node));
scanf("%d",&i);
temp->data=i;
temp->next=trav;
prev->next=temp; }
else{ insert_last(); } } }
void count(){
node *temp;
int count=0;
if(start==NULL){
return; }
temp=start;
while(temp!=NULL){
count++;
temp=temp->next; }
void reverse_list(){
node *temp;
int count=0,*rev_array;
if(start==NULL){
return; }
temp=start;
while(temp!=NULL){
count++;
temp=temp->next; }
rev_array=(int *)malloc(count*sizeof(int));
temp=start;
for(i=0;i<count;i++){
*(rev_array+i)=temp->data;
temp=temp->next; }
temp=start;
for(i=count-1;i>=0;i--){
temp->data=*(rev_array+i);
rear=temp;
temp=temp->next; } }
void search(){
node *temp;
int count=0,flag=0,val;
if(start==NULL){
return; }
scanf("%d",&val);
temp=start;
while(temp!=NULL){
count++;
if(val==temp->data){
flag=1;
break; }
temp=temp->next; }
if(flag==0){
void sort_list(){
node *temp,*temp2;
int temp3;
if(start==NULL){
return; }
temp=start;
while(temp!=NULL){
temp2=temp->next;
while(temp2!=NULL){
if(temp2->data<temp->data){
temp3=temp2->data;
temp2->data=temp->data;
temp->data=temp3; }
temp2=temp2->next; }
temp=temp->next; } }
if(start!=NULL) {
FILE *fptr;
fptr = fopen("custom_list.txt","w");
exit(0); }
temp=start;
while(temp->next!=NULL) {
temp = temp->next; }
fprintf(fptr," %d",temp->data);
fclose(fptr); }
int main() {
printf("\n\t ");
printf("\n\t 1. Create a list\n\t 2. Traverse the whole list\n\t 3. Delete first node\n\t 4. Delete last
node\n\t 5. Delete a node before specified data\n\t 6. Insert at first position\n\t 7. Insert at last
position\n\t 8. Insert a node before specified data\n\t 9. Insert a node at specified position\n\t 10.
Count\n\t 11. Copy (NOT DONE YET)\n\t 12. Merge two list (NOT DONE YET)\n\t 13. Reverse\n\t 14.
Search\n\t 15. Sort\n\t 16. Save\n\t 17. Exit");
scanf("%d",&choice);
switch(choice) {
while(choice!=17);
return 0; }
Output :
4. Delete last node 5. Delete a node before specified data 6. Insert at first position
7. Insert at last position 8. Insert a node before specified data 9. Insert a node at specified
position
#include <stdio.h>
#include <stdlib.h>
int choice, i;
void create() {
node *temp;
scanf("%d", &i);
while (i != -1) {
temp->next = NULL;
temp->data = i;
if (start == NULL) {
start = temp;
rear = temp; }
rear = temp;
rear->next = start; }
scanf("%d", &i); }
printf("\n-1 encountered\n"); }
void traverse() {
node *temp;
if (start == NULL) {
create(); }
temp = temp->next; }
void delete_first() {
node *temp;
start = start->next;
rear->next = start;
free(temp); } }
void delete_last() {
rear = temp;
temp = temp->next; }
rear->next = start;
free(temp); } }
void delete_before() {
if (start == NULL) {
return; }
scanf("%d", &i);
temp = start;
do { count++;
if (temp->data == i) {
flag = 1;
break; }
temp = temp->next;
if (flag == 1) {
else if (count == 2) {
temp = start;
start = start->next;
rear->next = start;
free(temp); }
do { prev = temp;
temp = temp->next;
if (temp->next->data == i) {
prev->next = temp->next;
free(temp);
break; }
void insert_first() {
node *temp;
if (start == NULL) {
return; }
scanf("%d", &i);
temp->data = i;
temp->next = start;
start = temp;
rear->next = start; }
void insert_last() {
node *temp;
if (start == NULL) {
return; }
scanf("%d", &i);
temp->data = i;
temp->next = start;
rear->next = temp;
rear = temp; }
void insert_before() {
if (start == NULL) {
return; }
scanf("%d", &val);
temp = start;
do { count++;
if (temp->data == val) {
flag = 1;
break; }
temp = temp->next;
if (flag == 1) {
scanf("%d", &i);
temp->data = i;
temp->next = NULL;
start = temp;
rear->next = start; }
do {
prev = trav;
trav = trav->next;
if (trav->data == val) {
prev->next = temp;
temp->next = trav;
break; }
void insert_specified() {
if (start == NULL) {
return; }
scanf("%d", &val);
val--;
count++;
prev = trav;
trav = trav->next; }
if (count == val) {
scanf("%d", &i);
temp->data = i;
temp->next = trav;
prev->next = temp; }
else { insert_last(); } } }
void count() {
node *temp;
int count = 0;
return; }
temp = start;
do { count++;
temp = temp->next;
if (start == NULL) {
return; }
temp = start;
do { count++;
temp = temp->next;
temp = start;
*(rev_array + i) = temp->data;
temp = temp->next; }
temp = start;
rear = temp;
temp = temp->next; } }
if (start == NULL) {
return; }
scanf("%d", &val);
temp = start;
do { count++;
if (val == temp->data) {
flag = 1;
break; }
temp = temp->next;
int temp3;
return; }
temp = start;
do { temp2 = temp->next;
temp3 = temp2->data;
temp2->data = temp->data;
temp->data = temp3; }
temp2 = temp2->next; }
temp = temp->next;
if (start != NULL) {
FILE *fptr;
if (fptr == NULL) {
printf("Error in file!");
exit(0); }
temp = start;
temp = temp->next; }
fclose(fptr); }
int main() { do {
printf("\n\t ");
printf("\n\t 1. Create a list\n\t 2. Traverse the whole list\n\t 3. Delete first node\n\t 4. Delete last
node\n\t 5. Delete a node before specified data\n\t 6. Insert at first position\n\t 7. Insert at last
position\n\t 8. Insert a node before specified data\n\t 9. Insert a node at specified position\n\t 10.
Count\n\t 11. Copy (NOT DONE YET)\n\t 12. Merge two list (NOT DONE YET)\n\t 13. Reverse\n\t 14.
Search\n\t 15. Sort\n\t 16. Save\n\t 17. Exit");
scanf("%d", &choice);
switch (choice) {
OUTPUT :
Perform the following operations on the doubly-linked list using user-defined functions:
4. Delete last node 5. Delete a node before specified data 6. Insert at first position
7. Insert at last position 8. Insert a node before specified data 9. Insert a node at
specified position
#include<stdio.h>
#include<stdlib.h>
struct queue{
int data;
node *start=NULL,*rear=NULL;
int choice,i;
scanf("%d",&i);
while(i!=-1){
temp = (node*)malloc(sizeof(node));
temp->next=NULL;
temp->prev=NULL;
temp->data=i;
if(start==NULL){
start=temp;
rear=temp; }
else{ temp->prev=rear;
rear->next=temp;
rear=temp; }
scanf("%d",&i); }
printf("\n-1 encountered\n");}
if(start==NULL){
create(); }
else{ temp=start;
while(temp->next!=NULL){
printf(" %d - >",temp->data);
temp=temp->next; }
printf(" %d",temp->data); } }
if(start==NULL){
else{ temp=start;
start=start->next;
start->prev=NULL;
free(temp); } }
if(start==NULL){
else{ temp=start;
while(temp->next!=NULL){
rear=temp;
temp=temp->next; }
rear->next=NULL;
free(temp); } }
void delete_before(){
node *temp,*pre;
int flag=0,count=0;
if(start==NULL){
return; }
scanf("%d",&i);
temp=start;
while (temp!=NULL){
count++;
if(temp->data==i){
flag=1;
break; }
temp=temp->next; }
if(flag==1){ if(count==1){
start=start->next;
start->prev=NULL;
free(temp); }
else{ temp=start;
while(temp!=NULL){
pre=temp;
temp=temp->next;
if(temp->next->data==i){ pre->next=temp->next;
temp->next->prev=pre;
free(temp);
break; } } } }
void insert_first(){
node *temp;
return; }
temp = (node*)malloc(sizeof(node));
scanf("%d",&i);
temp->data=i;
temp->prev=NULL;
temp->next=start;
start->prev=temp;
start=temp; }
void insert_last() {
node *temp;
return; }
temp = (node*)malloc(sizeof(node));
scanf("%d",&i);
temp->data=i;
temp->next=NULL;
rear->next=temp;
temp->prev=rear;
rear=temp; }
int val,flag=0,count=0;
if(start==NULL){
return; }
scanf("%d",&val);
temp=start;
if(temp->data==val){
flag=1;
break; }
temp=temp->next; }
scanf("%d",&i);
temp->data=i;
temp->next=NULL;
temp->prev=NULL;
if(count==1){ temp->next=start;
start->prev=temp;
start=temp; }
else{ trav=start;
while(trav!=NULL){
pre=trav;
trav=trav->next;
if(trav->data==val){
pre->next=temp;
temp->prev=pre;
temp->next=trav;
trav->prev=temp;
break; } } } }
void insert_specified(){
node *temp,*trav,*pre;
int val,flag=0,count=0;
if(start==NULL){
return; }
scanf("%d",&val);
val--;
if(val<1){ insert_first(); }
else{ trav=start;
count++;
pre=trav;
trav=trav->next; }
scanf("%d",&i);
temp->data=i;
temp->next=trav;
trav->prev=temp;
pre->next=temp;
temp->prev=pre; }
else{ insert_last(); } } }
void count(){
node *temp;
int count=0;
return; }
temp=start;
while(temp!=NULL){
count++;
temp=temp->next; }
if(start==NULL){
return; }
temp=rear;
while(temp->prev!=NULL){
temp=temp->prev; }
printf(" %d",temp->data); }
void search(){
node *temp;
int count=0,flag=0,val;
if(start==NULL){
return; }
scanf("%d",&val);
temp=start;
while(temp!=NULL){
count++;
if(val==temp->data){
flag=1;
break; }
temp=temp->next; }
int temp3;
return; }
temp=start;
while(temp!=NULL){
temp2=temp->next;
while(temp2!=NULL){
if(temp2->data<temp->data){
temp3=temp2->data;
temp2->data=temp->data;
temp->data=temp3; }
temp2=temp2->next; }
temp=temp->next; } }
fptr = fopen("custom_circular_list.txt","w");
if(fptr == NULL) {
printf("Error in file!");
exit(0); }
temp=start;
while(temp->next!=NULL){
temp = temp->next; }
fprintf(fptr," %d",temp->data);
fclose(fptr); }
printf("\n\t ");
printf("\n\t 1. Create a list\n\t 2. Traverse the whole list\n\t 3. Delete first node\n\t 4. Delete last
node\n\t 5. Delete a node before specified data\n\t 6. Insert at first position\n\t 7. Insert at last
position\n\t 8. Insert a node before specified data\n\t 9. Insert a node at specified position\n\t 10.
Count\n\t 11. Copy (NOT DONE YET)\n\t 12. Merge two list (NOT DONE YET)\n\t 13. Reverse\n\t 14.
Search\n\t 15. Sort\n\t 16. Save\n\t 17. Exit");
scanf("%d",&choice);
switch(choice) {
while(choice!=17);
return 0; }
Perform the following operations on doubly-linked Circular list using user defined functions:
4. Delete last node 5. Delete a node before specified data 6. Insert at first position
7. Insert at last position 8. Insert a node before specified data 9. Insert a node at specified position
#include<stdio.h>
#include<stdlib.h>
struct queue{
int data;
node *start=NULL,*rear=NULL;
int choice,i;
scanf("%d",&i);
while(i!=-1) {
temp = (node*)malloc(sizeof(node));
temp->next=NULL;
temp->prev=NULL;
temp->data=i;
if(start==NULL) {
start=temp;
rear=temp; }
else{ temp->prev=rear;
rear->next=temp;
temp->next=start;
start->prev=temp;
rear=temp; }
scanf("%d",&i); }
printf("\n-1 encountered\n");}
if(start==NULL) {
create(); }
else{ temp=start;
while(temp->next->data!=start->data){
printf(" %d - >",temp->data);
temp=temp->next; }
printf(" %d",temp->data); }}
else{ temp=start;
start=start->next;
start->prev=rear;
rear->next=start;
free(temp); }}
void delete_last() {
node *temp;
else{ temp=rear;
rear=rear->prev;
rear->next=start;
start->prev=rear;
free(temp); }}
void delete_before() {
node *temp,*pre;
int flag=0,count=0;
if(start==NULL){
return; }
scanf("%d",&i);
temp=start;
do{ count++;
if(temp->data==i) {
flag=1;
break; }
temp=temp->next;
}while (temp->data!=start->data);
if(flag==1){
else if(count==2){
temp=start;
start=start->next;
start->prev=rear;
rear->next=start;
free(temp); }
else{ temp=start;
while(temp!=NULL){
pre=temp;
temp=temp->next;
if(temp->next->data==i){
pre->next=temp->next;
temp->next->prev=pre;
free(temp);
break; } } } }
void insert_first(){
node *temp;
if(start==NULL){
return; }
temp = (node*)malloc(sizeof(node));
scanf("%d",&i);
temp->data=i;
temp->prev=rear;
temp->next=start;
start->prev=temp;
rear->next=temp;
start=temp;}
void insert_last(){
node *temp;
if(start==NULL){
return; }
temp = (node*)malloc(sizeof(node));
scanf("%d",&i);
temp->data=i;
temp->next=start;
rear->next=temp;
temp->prev=rear;
start->prev=temp;
rear=temp;}
void insert_before(){
node *temp,*trav,*pre;
int val,flag=0,count=0;
if(start==NULL){
return; }
scanf("%d",&val);
temp=start;
do{ count++;
if(temp->data==val) {
flag=1;
break; }
temp=temp->next;
}while (temp->data!=start->data);
if(flag==1){
temp = (node*)malloc(sizeof(node));
scanf("%d",&i);
temp->data=i;
temp->next=NULL;
temp->prev=NULL;
if(count==1) {
temp->next=start;
start->prev=temp;
rear->next=temp;
temp->prev=rear;
start=temp; }
else{ trav=start;
do{ pre=trav;
trav=trav->next;
if(trav->data==val){
pre->next=temp;
temp->prev=pre;
temp->next=trav;
trav->prev=temp;
break; }
}while(trav->data!=start->data); } }
void insert_specified(){
node *temp,*trav,*pre;
int val,flag=0,count=0;
if(start==NULL){
return; }
scanf("%d",&val);
val--;
if(val<1){ insert_first(); }
else{ trav=start;
count++;
pre=trav;
trav=trav->next; }
if(count==val){
temp = (node*)malloc(sizeof(node));
scanf("%d",&i);
temp->data=i;
temp->next=trav;
trav->prev=temp;
pre->next=temp;
temp->prev=pre; }
else{ insert_last(); } }}
void count(){
node *temp;
int count=0;
if(start==NULL){
return; }
temp=start;
do{ count++;
temp=temp->next;
}while(temp->data!=start->data);
void reverse_list(){
node *temp;
if(start==NULL){
return; }
temp=rear;
while(temp->prev->data!=rear->data){
temp=temp->prev; }
printf(" %d",temp->data);}
void search(){
node *temp;
int count=0,flag=0,val;
if(start==NULL){
return; }
scanf("%d",&val);
temp=start;
do{ count++;
if(val==temp->data){
flag=1;
break; }
temp=temp->next;
}while(temp->data!=start->data);
if(flag==0){
void sort_list(){
node *temp,*temp2;
int temp3;
temp=start;
do{ temp2=temp->next;
while(temp2!=NULL){
if(temp2->data<temp->data){
temp3=temp2->data;
temp2->data=temp->data;
temp->data=temp3; }
temp2=temp2->next; }
temp=temp->next;
}while(temp->data!=start->data);}
void save() {
node *temp;
if(start!=NULL) {
FILE *fptr;
fptr = fopen("custom_circular_doublylist.txt","w");
if(fptr == NULL) {
printf("Error in file!");
exit(0); }
temp=start;
while(temp->next->data!=start->data){
temp = temp->next; }
fprintf(fptr," %d",temp->data);
fclose(fptr); }
int main(){
do {
printf("\n\t ");
printf("\n\t 1. Create a list\n\t 2. Traverse the whole list\n\t 3. Delete first node\n\t 4. Delete last
node\n\t 5. Delete a node before specified data\n\t 6. Insert at first position\n\t 7. Insert at last
position\n\t 8. Insert a node before specified data\n\t 9. Insert a node at specified position\n\t 10.
Count\n\t 11. Copy (NOT DONE YET)\n\t 12. Merge two list (NOT DONE YET)\n\t 13. Reverse\n\t 14.
Search\n\t 15. Sort\n\t 16. Save\n\t 17. Exit");
scanf("%d",&choice);
switch(choice) {
while(choice!=17);
return 0;}
OUTPUT :
Program 18 : Write a program to represent an undirected graph using the adjacency matrix to
implement the graph and perform following operations, with menu driven options for following tasks:
4. List all vertices that are adjacent to a specified vertex. 5. Print out vertices using depth
first search
#include <stdio.h>
#include <stdlib.h>
int dir_graph();
int undir_graph();
void main() {
int option;
do {
scanf("%d", &option);
switch (option) {
case 1:
dir_graph();
break;
case 2:
undir_graph();
break;
case 3:
exit(0);
} // switch
} while (1);
int dir_graph() {
int adj_mat[50][50];
int n;
scanf("%d", &n);
read_graph(adj_mat, n);
in_deg = out_deg = 0;
if (adj_mat[j][i] == 1)
in_deg++; }
if (adj_mat[i][j] == 1)
out_deg++;
return;}
int undir_graph() {
int adj_mat[50][50];
int deg, i, j, n;
scanf("%d", &n);
read_graph(adj_mat, n);
deg = 0;
if (adj_mat[i][j] == 1)
deg++;
return;}
int i, j;
char reply;
if (i == j) {
adj_mat[i][j] = 0;
continue; }
scanf("%c", &reply);
adj_mat[i][j] = 1;
else
adj_mat[i][j] = 0; }}
return;}
OUTPUT:
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
#include<stdio.h>
int main(void) {
root = NULL;
do { do {
printf("\n********************MENU*********************\n");
printf("\n********************************************\n");
scanf(" %d",&choice);
if(choice<1 || choice>7)
switch(choice) {
case 1:
scanf("%d", &item);
root= insert(root,item);
inorder(root);
break;
case 2:
scanf(" %d",&item_no);
root=delet(root,item_no);
inorder(root);
break;
case 3:
inorder(root);
break;
case 4:
postorder(root);
break;
case 5:
preorder(root);
break;
case 6:
root=search(root);
break;
default:
}while(choice !=7);
return(0);}
if(!root) {
root->info = x;
root->left = NULL;
root->right = NULL;
return(root); }
if(root->info > x)
if(root->info < x)
root->right = insert(root->right,x); }
return(root); }
if(root != NULL) {
inorder(root->left);
printf(" %d",root->info);
inorder(root->right); }
return;}
if(root != NULL) {
postorder(root->left);
postorder(root->right);
printf(" %d",root->info); }
return; }
if(root != NULL) {
printf(" %d",root->info);
preorder(root->left);
preorder(root->right); }
return; }
return(ptr);
ptr->right = delet(ptr->right,x);
ptr->left=delet(ptr->left,x);
return ptr;
} else
if(ptr->info == x)
if(ptr->left == ptr->right)
free(ptr);
return(NULL);
} else if(ptr->left==NULL) {
p1=ptr->right;
free(ptr);
return p1;
} else if(ptr->right==NULL) {
p1=ptr->left;
free(ptr);
return p1;
} else { p1=ptr->right;
p2=ptr->right;
while(p1->left != NULL)
p1=p1->left;
p1->left=ptr->left;
free(ptr);
return p2; } } } }
return(ptr);}
int no,i,ino;
ptr=root;
scanf(" %d",&no);
fflush(stdin);
while(ptr) { if(no>ptr->info)
ptr=ptr->left; else
break; }
if(ptr) {
scanf(" %d",&i);
if(i==1) {
scanf(" %d",&ino);
ptr->info=ino;
} else
} else
return(root);}