This is the program for Creating a Doubly Linked List in C.
#include<stdio.h> #include<stdlib.h> struct node { int numb; struct node*next; struct node*prev; }; struct node* head=NULL,*newnode; void append() { struct node*temp; struct node*newnode=(struct node*)malloc(sizeof(struct node)); printf("Enter Node : \n"); scanf("%d", &newnode->numb); newnode->next=NULL; newnode->prev=NULL; if(head==NULL) { head = temp = newnode; } else { temp->next=newnode; newnode->prev=temp; temp=newnode; } } void display() {//optional --- temp=(struct node*)malloc(sizeof(struct nod struct node*temp; temp=head; if(temp==NULL) { printf("Empty\n"); } else { while(temp!=NULL) { printf("-->%d\n", temp->numb); temp=temp->next; } } } void exit() { printf("Exit"); } int main() { int ch; while(1) { printf("1. Append\n"); printf("2. Display\n"); printf("3. Exit\n"); printf("Enter your choice : \n"); scanf("%d",&ch); switch(ch) { case 1: append(); break; case 2: display(); break; case 3: exit(1); break; default : printf("Invalid choice !!!\n"); break; } } }
0 Comments