DS Polynomial Addition
DS Polynomial Addition
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
struct node *prev;
};
struct node *head = NULL, *last = NULL;
void create() {
int val;
printf("Enter the value which you want to insert: ");
scanf("%d", &val);
struct node* newnode = (struct node*)malloc(sizeof(struct node));
if (newnode == NULL) {
printf("Memory allocation failed\n");
return;
}
newnode->data = val;
newnode->next = newnode->prev = NULL;
if (head == NULL) {
head = last = newnode;
head->next = head;
head->prev = head;
}
}
void display() {
if (head == NULL) {
printf("List is empty\n");
return;
}
struct node *tmp = head;
printf("List elements: ");
do {
printf("%d ", tmp->data);
tmp = tmp->next;
} while (tmp != head);
printf("\n");
}
void displayReverse() {
if (head == NULL) {
printf("List is empty\n");
return;
}
struct node *tmp = last;
printf("List in Reverse: ");
do {
printf("%d ", tmp->data);
tmp = tmp->prev;
} while (tmp != last);
printf("\n");
}
int main() {
int ch, val, cnt = 1;
while (1) {
printf("\nList of elements: ");
display();
printf("\n\n1. Insert At Beginning\n2. Insert At Last\n3. Insert At Position\n");
printf("4. Delete At Beginning\n5. Delete At Last\n");
printf("6. Delete Specific Value\n7. Display in Reverse\n8. Search an Element\n");
printf("9. Exit\n");
printf("Enter your choice: ");
scanf("%d", &ch);
if (ch >= 1 && ch <= 2) {
create();
if (head == NULL) {
head = last = newnode;
head->next = head;
head->prev = head;
continue;
}
}
struct node *tmp;
switch (ch) {
case 1:
newnode->next = head;
newnode->prev = last;
last->next = newnode;
head->prev = newnode;
head = newnode;
break;
case 2:
newnode->next = head;
newnode->prev = last;
last->next = newnode;
head->prev = newnode;
last = newnode;
break;
case 3:
printf("Enter the value after which you want to insert: ");
scanf("%d", &val);
tmp = head;
do {
if (tmp->data == val) {
create();
newnode->next = tmp->next;
newnode->prev = tmp;
tmp->next->prev = newnode;
tmp->next = newnode;
if (tmp == last) last = newnode;
break;
}
tmp = tmp->next;
} while (tmp != head);
break;
case 4:
if (head == last) {
free(head);
head = last = NULL;
} else {
tmp = head;
head = head->next;
head->prev = last;
last->next = head;
free(tmp);
}
break;
case 5:
if (head == last) {
free(head);
head = last = NULL;
} else {
tmp = last;
last = last->prev;
last->next = head;
head->prev = last;
free(tmp);
}
break;
case 6:
printf("Enter the value to delete: ");
scanf("%d", &val);
tmp = head;
do {
if (tmp->data == val) {
if (tmp == head) {
head = head->next;
last->next = head;
head->prev = last;
} else if (tmp == last) {
last = last->prev;
last->next = head;
head->prev = last;
} else {
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
}
free(tmp);
break;
}
tmp = tmp->next;
} while (tmp != head);
break;
case 7:
displayReverse();
break;
case 8:
if (head == NULL) {
printf("No elements are there\n");
} else {
cnt = 1;
printf("Enter the element to search: ");
scanf("%d", &val);
tmp = head;
do {
if (tmp->data == val) {
printf("%d is found at position %d\n", val, cnt);
break;
}
cnt++;
tmp = tmp->next;
} while (tmp != head);
if (tmp == head)
printf("%d not found\n", val);
}
break;
case 9:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Try again.\n");
}
}
return 0;
}