Implement basic operations on doubly linked list.
Source Code:
#include<stdio.h>
#include<stdlib.h>
void create();
void insert();
void ins_begin();
void ins_end();
void ins_atgiven();
void delet();
void del_begin();
void del_end();
void del_givennode();
void traverse();
struct node{
int data;
struct node *next;
struct node *prev;
};
struct node *head=NULL;
int main(){
int opt;
printf("::Double Linked List Operations::\n1.Creation\n2.Insertion\n3.Deletion\n4.Traversing\n5.Exit");
do{
printf("\nSelect operation:\n");
scanf("%d",&opt);
switch(opt){
case 1:
create();
break;
case 2:
insert();
break;
case 3:
delet();
break;
case 4:
traverse();
break;
case 5:
exit(0);
break;
default:
printf("Invalid operation.\n");
}
}while(opt!=5);
return 0;
}
void create(){
int ch;
struct node *ptr1,*ptr2;
ptr2=(struct node*)malloc(sizeof(struct node*));
do{
ptr1=(struct node*)malloc(sizeof(struct node*));
printf("Enter node value:\n");
scanf("%d",&ptr1->data);
if(head==NULL){
head=ptr1;
ptr1->prev = NULL;
}
ptr2->next=ptr1;
ptr2=ptr1;
printf("To continue press 1 otherwise 0:\n");
scanf("%d",&ch);
}while(ch==1);
ptr1->next=NULL;
}
void insert(){
int iopt;
printf("::Insertion Options::\n1. Insert at Begin\n2. Insert at End\n3. Insert at Given Node\n");
printf("Where you want to insert:\n");
scanf("%d",&iopt);
switch(iopt){
case 1:
ins_begin();
break;
case 2:
ins_end();
break;
case 3:
ins_atgiven();
break;
default:
printf("Wrong insert option.\n");
}
}
void ins_begin(){
struct node *ptr;
if(head==NULL){
printf("List is empty.\n");
}else{
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter new node:\n");
scanf("%d",&ptr->data);
ptr->next=head;
ptr->prev=NULL;
ptr->next->prev=NULL;
head=ptr;
}
}
void ins_end(){
struct node *ptr1,*ptr2;
if(head==NULL){
printf("List is empty.\n");
}else{
ptr1=(struct node*)malloc(sizeof(struct node));
printf("Enter new node:\n");
scanf("%d",&ptr1->data);
ptr2=head;
while(ptr2->next!=NULL)
ptr2=ptr2->next;
ptr2->next=ptr1;
ptr1->prev=ptr2;
ptr1->next=NULL;
}
}
void ins_atgiven(){
int nodeval;
struct node *ptr1,*ptr2;
if(head==NULL){
printf("List is empty.\n");
}else{
printf("Enter node value:\n");
scanf("%d",&nodeval);
ptr1=(struct node*)malloc(sizeof(struct node));
printf("Enter new node:\n");
scanf("%d",&ptr1->data);
ptr2=head;
while(ptr2->data!=nodeval && ptr2!=NULL)
ptr2=ptr2->next;
if(ptr2->data==nodeval){
ptr1->next=ptr2->next;
ptr1->prev=ptr2;
ptr2->next->prev=ptr1;
ptr2->next=ptr1;
}else{
printf("Node value not found in list.\n");
}
}
}
void delet(){
int dopt;
printf("::Deletion Option::\n1. Delete at Begin\n2. Delete at End\n3. Delete the Given Node\n");
printf("Where you want to delete:\n");
scanf("%d",&dopt);
switch(dopt){
case 1:
del_begin();
break;
case 2:
del_end();
break;
case 3:
del_givennode();
break;
default:
printf("Wrong delete option.\n");
}
}
void del_begin(){
struct node *ptr;
if(head==NULL){
printf("List is empty.\n");
}else{
ptr=head;
head=head->next;
head->prev=NULL;
free(ptr);
}
}
void del_end(){
struct node *ptr1,*ptr2;
if(head==NULL){
printf("List is empty.\n");
}else{
ptr2=head;
while(ptr2->next!=NULL){
ptr1=ptr2;
ptr2=ptr2->next;
}
free(ptr2);
ptr1->next=NULL;
}
}
void del_givennode(){
int nodeval;
struct node *ptr1,*ptr2;
if(head==NULL){
printf("List is empty.");
}else{
printf("Enter node value:\n");
scanf("%d",&nodeval);
ptr2=head;
while(ptr2->next!=NULL && ptr2->data!=nodeval){
ptr1=ptr2;
ptr2=ptr2->next;
}
if(ptr2->data==nodeval){
ptr1->next=ptr2->next;
ptr2->next->prev=ptr1;
free(ptr2);
}else{
printf("Node value not found in list.\n");
}
}
}
void traverse(){
struct node *ptr;
if(head==NULL){
printf("List is empty.\n");
}else{
printf("Double Linked List elements are:\n");
ptr=head;
while(ptr!=NULL){
printf("%d->",ptr->data);
ptr=ptr->next;
}
printf("NULL");
}
}
#include<stdlib.h>
void create();
void insert();
void ins_begin();
void ins_end();
void ins_atgiven();
void delet();
void del_begin();
void del_end();
void del_givennode();
void traverse();
struct node{
int data;
struct node *next;
struct node *prev;
};
struct node *head=NULL;
int main(){
int opt;
printf("::Double Linked List Operations::\n1.Creation\n2.Insertion\n3.Deletion\n4.Traversing\n5.Exit");
do{
printf("\nSelect operation:\n");
scanf("%d",&opt);
switch(opt){
case 1:
create();
break;
case 2:
insert();
break;
case 3:
delet();
break;
case 4:
traverse();
break;
case 5:
exit(0);
break;
default:
printf("Invalid operation.\n");
}
}while(opt!=5);
return 0;
}
void create(){
int ch;
struct node *ptr1,*ptr2;
ptr2=(struct node*)malloc(sizeof(struct node*));
do{
ptr1=(struct node*)malloc(sizeof(struct node*));
printf("Enter node value:\n");
scanf("%d",&ptr1->data);
if(head==NULL){
head=ptr1;
ptr1->prev = NULL;
}
ptr2->next=ptr1;
ptr2=ptr1;
printf("To continue press 1 otherwise 0:\n");
scanf("%d",&ch);
}while(ch==1);
ptr1->next=NULL;
}
void insert(){
int iopt;
printf("::Insertion Options::\n1. Insert at Begin\n2. Insert at End\n3. Insert at Given Node\n");
printf("Where you want to insert:\n");
scanf("%d",&iopt);
switch(iopt){
case 1:
ins_begin();
break;
case 2:
ins_end();
break;
case 3:
ins_atgiven();
break;
default:
printf("Wrong insert option.\n");
}
}
void ins_begin(){
struct node *ptr;
if(head==NULL){
printf("List is empty.\n");
}else{
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter new node:\n");
scanf("%d",&ptr->data);
ptr->next=head;
ptr->prev=NULL;
ptr->next->prev=NULL;
head=ptr;
}
}
void ins_end(){
struct node *ptr1,*ptr2;
if(head==NULL){
printf("List is empty.\n");
}else{
ptr1=(struct node*)malloc(sizeof(struct node));
printf("Enter new node:\n");
scanf("%d",&ptr1->data);
ptr2=head;
while(ptr2->next!=NULL)
ptr2=ptr2->next;
ptr2->next=ptr1;
ptr1->prev=ptr2;
ptr1->next=NULL;
}
}
void ins_atgiven(){
int nodeval;
struct node *ptr1,*ptr2;
if(head==NULL){
printf("List is empty.\n");
}else{
printf("Enter node value:\n");
scanf("%d",&nodeval);
ptr1=(struct node*)malloc(sizeof(struct node));
printf("Enter new node:\n");
scanf("%d",&ptr1->data);
ptr2=head;
while(ptr2->data!=nodeval && ptr2!=NULL)
ptr2=ptr2->next;
if(ptr2->data==nodeval){
ptr1->next=ptr2->next;
ptr1->prev=ptr2;
ptr2->next->prev=ptr1;
ptr2->next=ptr1;
}else{
printf("Node value not found in list.\n");
}
}
}
void delet(){
int dopt;
printf("::Deletion Option::\n1. Delete at Begin\n2. Delete at End\n3. Delete the Given Node\n");
printf("Where you want to delete:\n");
scanf("%d",&dopt);
switch(dopt){
case 1:
del_begin();
break;
case 2:
del_end();
break;
case 3:
del_givennode();
break;
default:
printf("Wrong delete option.\n");
}
}
void del_begin(){
struct node *ptr;
if(head==NULL){
printf("List is empty.\n");
}else{
ptr=head;
head=head->next;
head->prev=NULL;
free(ptr);
}
}
void del_end(){
struct node *ptr1,*ptr2;
if(head==NULL){
printf("List is empty.\n");
}else{
ptr2=head;
while(ptr2->next!=NULL){
ptr1=ptr2;
ptr2=ptr2->next;
}
free(ptr2);
ptr1->next=NULL;
}
}
void del_givennode(){
int nodeval;
struct node *ptr1,*ptr2;
if(head==NULL){
printf("List is empty.");
}else{
printf("Enter node value:\n");
scanf("%d",&nodeval);
ptr2=head;
while(ptr2->next!=NULL && ptr2->data!=nodeval){
ptr1=ptr2;
ptr2=ptr2->next;
}
if(ptr2->data==nodeval){
ptr1->next=ptr2->next;
ptr2->next->prev=ptr1;
free(ptr2);
}else{
printf("Node value not found in list.\n");
}
}
}
void traverse(){
struct node *ptr;
if(head==NULL){
printf("List is empty.\n");
}else{
printf("Double Linked List elements are:\n");
ptr=head;
while(ptr!=NULL){
printf("%d->",ptr->data);
ptr=ptr->next;
}
printf("NULL");
}
}
::Double Linked List Operations::
1.Creation
2.Insertion
3.Deletion
4.Traversing
5.Exit
Select operation:
1
Enter node value:
5
To continue press 1 otherwise 0:
1
Enter node value:
10
To continue press 1 otherwise 0:
1
Enter node value:
15
To continue press 1 otherwise 0:
0
Select operation:
4
Double Linked List elements are:
5->10->15->NULL
Select operation:
2
::Insertion Options::
1. Insert at Begin
2. Insert at End
3. Insert at Given Node
Where you want to insert:
1
Enter new node:
3
Select operation:
2
::Insertion Options::
1. Insert at Begin
2. Insert at End
3. Insert at Given Node
Where you want to insert:
2
Enter new node:
20
Select operation:
2
::Insertion Options::
1. Insert at Begin
2. Insert at End
3. Insert at Given Node
Where you want to insert:
3
Enter node value:
15
Enter new node:
17
Select operation:
4
Double Linked List elements are:
3->5->10->15->17->20->NULL
Select operation:
3
::Deletion Option::
1. Delete at Begin
2. Delete at End
3. Delete the Given Node
Where you want to delete:
1
Select operation:
4
Double Linked List elements are:
5->10->15->17->20->NULL
Select operation:
3
::Deletion Option::
1. Delete at Begin
2. Delete at End
3. Delete the Given Node
Where you want to delete:
2
Select operation:
4
Double Linked List elements are:
5->10->15->17->NULL
Select operation:
3
::Deletion Option::
1. Delete at Begin
2. Delete at End
3. Delete the Given Node
Where you want to delete:
3
Enter node value:
15
Select operation:
4
Double Linked List elements are:
5->10->17->NULL
Select operation:
5
1.Creation
2.Insertion
3.Deletion
4.Traversing
5.Exit
Select operation:
1
Enter node value:
5
To continue press 1 otherwise 0:
1
Enter node value:
10
To continue press 1 otherwise 0:
1
Enter node value:
15
To continue press 1 otherwise 0:
0
Select operation:
4
Double Linked List elements are:
5->10->15->NULL
Select operation:
2
::Insertion Options::
1. Insert at Begin
2. Insert at End
3. Insert at Given Node
Where you want to insert:
1
Enter new node:
3
Select operation:
2
::Insertion Options::
1. Insert at Begin
2. Insert at End
3. Insert at Given Node
Where you want to insert:
2
Enter new node:
20
Select operation:
2
::Insertion Options::
1. Insert at Begin
2. Insert at End
3. Insert at Given Node
Where you want to insert:
3
Enter node value:
15
Enter new node:
17
Select operation:
4
Double Linked List elements are:
3->5->10->15->17->20->NULL
Select operation:
3
::Deletion Option::
1. Delete at Begin
2. Delete at End
3. Delete the Given Node
Where you want to delete:
1
Select operation:
4
Double Linked List elements are:
5->10->15->17->20->NULL
Select operation:
3
::Deletion Option::
1. Delete at Begin
2. Delete at End
3. Delete the Given Node
Where you want to delete:
2
Select operation:
4
Double Linked List elements are:
5->10->15->17->NULL
Select operation:
3
::Deletion Option::
1. Delete at Begin
2. Delete at End
3. Delete the Given Node
Where you want to delete:
3
Enter node value:
15
Select operation:
4
Double Linked List elements are:
5->10->17->NULL
Select operation:
5