Doubly Link List using C
#include<stdio.h>
struct node{
struct node *next, *prev;
int data;
}*head = NULL;
/*displaying nodes*/
void display(){
struct node *ptr;
if(head == NULL){
printf("\nList is empty");
}else{
printf("\nThe nodes are: ");
ptr = head;
while(ptr != NULL){
printf("%d\t",ptr->data);
ptr = ptr->next;
}
}
}
/*inserted new node at beginning*/
void ins_beg(int x){
struct node *tmp;
tmp = (struct node*)malloc(sizeof(struct node));
tmp->next = NULL;
tmp->prev = NULL;
tmp->data = x;
if(head == NULL){
tmp->next = NULL;
}else{
tmp->next = head;
head->prev = tmp;
}
head = tmp;
printf("\nInserted new node successfully");
display();
}
/*inserted new node at end*/
void ins_last(int x){
struct node *tmp, *ptr;
tmp = (struct node*)malloc(sizeof(struct node));
tmp->next = NULL;
tmp->data = x;
tmp->prev = NULL;
if(head == NULL){
head = tmp;
tmp->next = NULL;
}else{
ptr = head;
while(ptr->next != NULL){
ptr = ptr->next;
}
ptr->next = tmp;
tmp->prev = ptr;
}
printf("\nInserted new node at end successfully");
display();
}
/*delete node at beginning */
void del_beg(){
struct node *tmp;
if(head == NULL){
printf("\nList is empty");
}else{
tmp = head;
head = head->next;
printf("Delete node at bigging successfully");
printf("\nDeleted node is: %d",tmp->data);
free(tmp);
display();
}
}
/*delete node at end*/
void del_end(){
struct node *tmp, *ptr;
if(head == NULL){
printf("\nList is empty");
}else{
tmp = head;
if(tmp->next == NULL){
printf("\nDeleted node is: %d",tmp->data);
head = NULL;
free(tmp);
printf("\nDeleted node at end successfully");
display();
}else{
while(tmp->next != NULL){
ptr = tmp;
tmp = tmp->next;
}
ptr->next = NULL;
printf("\nDeleted node is: %d",tmp->data);
printf("\nDeleted node at end successfully");
free(tmp);
display();
}
}
}
/*inserting new node at position wise*/
void ins_pos(int x, int pos){
struct node *tmp, *ptr, *q;
int i;
tmp = (struct node*)malloc(sizeof(struct node));
tmp->next = NULL;
tmp->prev = NULL;
tmp->data = x;
if(head == NULL){
head = tmp;
tmp->next = head;
}else{
if(pos == 1){
tmp->next = head;
head->prev = tmp;
head = tmp;
printf("\nInsered node at %d position successfully");
display();
}else{
ptr = head;
for(i=1; i<= pos-2; i++){
ptr = ptr->next;
}
q = ptr->next;
tmp->next = q;
ptr->next = tmp;
q = ptr->prev;
tmp->prev = ptr;
ptr->prev = tmp;
printf("\nInserted new node at %d position successfully");
display();
}
}
}
/*deleting node at position wise*/
void del_pos(int pos){
struct node *tmp, *ptr, *q;
int i;
if(head == NULL){
printf("\nList is empty");
}else{
if(pos == 1){
tmp = head;
head = head->next;
printf("\nDeleted node at %d position successfully");
printf("\nDeleted node: %d",tmp->data);
free(tmp);
display();
}else{
ptr = head;
for(i = 1; i<=pos-2; i++){
ptr = ptr->next;
}
tmp = ptr->next;
q = tmp->next;
ptr->next = q;
q = ptr->prev;
printf("\nDeleted node at %d position successfully",pos);
printf("\nDeleted node is: %d",tmp->data);
free(tmp);
display();
}
}
}
/*main function*/
int main(){
int ch, data, pos;
while(1){
printf("\n1.ins_beg();\n2.display();\n3.ins_last();\n4.del_beg();\n5.del_end();\n6.ins_pos();\n7.del_pos();\n8.exit();\nEnter your choice: ");
scanf("%d",&ch);
switch(ch){
case 1:
printf("\nEnter new node: ");
scanf("%d",&data);
ins_beg(data);
break;
case 2:
display();
break;
case 3:
printf("\nEnter new node: ");
scanf("%d",&data);
ins_last(data);
break;
case 4:
del_beg();
break;
case 5:
del_end();
break;
case 6:
printf("\nEnter new node: ");
scanf("%d",&data);
printf("\nEnter position: ");
scanf("%d",&pos);
ins_pos(data, pos);
break;
case 7:
printf("\nEnter position: ");
scanf("%d",&pos);
del_pos(pos);
break;
case 8:
exit(1);
break;
default:
printf("\nWrong choice, try again...");
break;
}
}
}
Comments
Post a Comment