Skip to main content

C Program To Implement Doubly Linked List.

Doubly-linked list is a more sophisticated form of linked list data structure. Each node contains two fields, called links that are references to the previous and to the next node in the sequence of nodes. The two node links allow traversal of the list in either direction. The previous link of the first node and the next link of the last node points to NULL.

While adding or removing a node in a doubly linked list requires changing more links than the same operations on a singly linked list, the operations are simpler and potentially more efficient (for nodes other than first nodes) because there is no need to keep track of the previous node during traversal or no need to traverse the list to find the previous node, so that its link can be modified.

Example: A doubly linked list whose nodes contain three fields of integer value, the link to the next node, and the link to the previous node.

doubly linked list operation

Source code of Doubly Linked List:
#include <stdio.h>
#include <stdlib.h>

struct node {
struct node *prev;

int n;
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;

void insert1();
void insert2();
void insert3();
void delete1();
void traversebeg();
void traverseend(int);
void sort();
void search();
void update();
int count = 0;

main(void) {
int ch;
h = NULL;
temp = temp1 = NULL;

printf("\n 1 - Insert At Beginning");
printf("\n 2 - Insert At End");
printf("\n 3 - Insert At Any Position");
printf("\n 4 - Delete From Any Position");
printf("\n 5 - Display The List");
printf("\n 6 - Reverse The List");
printf("\n 7 - Search For Element");
printf("\n 8 - Sort The List");
printf("\n 9 - Update The List");
printf("\n 10 - Exit");

while (1) {
printf("\n\n Enter Your Choice : ");
scanf("%d", &ch);

switch (ch) {
case 1: insert1(); break;
case 2: insert2(); break;
case 3: insert3(); break;
case 4: delete1(); break;
case 5: traversebeg(); break;

case 6: temp2 = h;
if (temp2 == NULL)
printf("\n Error : List Empty To display.\n");

else {
printf("\n Reverse Order Of Linked List Is : ");
traverseend(temp2->n);
printf("\n"); }
break;

case 7: search(); break;
case 8: sort(); break;
case 9: update(); break;
case 10: exit(0);

default: printf("\n Error : Wrong Menu Choice!\n");
}}}

/* To create an empty node */
void create()
{
int data;
temp =(struct node *)malloc(1*sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;

printf("\n Enter A Value To List : ");
scanf("%d", &data);
temp->n = data;
count++;
}

/*  To insert at beginning */
void insert1()
{
if (h == NULL) {
create();
h = temp;
temp1 = h;
}
else {
create();
temp->next = h;
h->prev = temp;
h = temp;
}}

/* To insert at end */
void insert2()
{
if (h == NULL) {
create();
h = temp;
temp1 = h;
}
else {
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}}

/* To insert at any position */
void insert3() {
int pos, i = 2;

printf("\n Enter Position To Be Inserted : ");
scanf("%d", &pos);
temp2 = h;

if ((pos < 1) || (pos >= count + 1)) {
printf("\n Error : Position Out Of Range To Insert.\n");
return;
}

if ((h == NULL) && (pos != 1)) {
printf("\n Error : Empty List, You Cannot Insert Other Than 1st Position.\n");
return;
}

if ((h == NULL) && (pos == 1))
{
create();
h = temp;
temp1 = h;
return;
}
else {
while (i < pos) {
temp2 = temp2->next;
i++;
}
create();
temp->prev = temp2;
temp->next = temp2->next;
temp2->next->prev = temp;
temp2->next = temp;
}}

/* To delete an element */
void delete1() {
int i = 1, pos;

printf("\n Enter Position To Be Deleted : ");
scanf("%d", &pos);
temp2 = h;

if ((pos < 1) || (pos >= count + 1)) {
printf("\n Error : Position Out Of Range To Delete.\n");
return;
}

if (h == NULL) {
printf("\n Error : Empty List! No Element To Delete.\n");
return;
}
else
{
while (i < pos) {
temp2 = temp2->next;
i++;
}
if (i == 1) {
if (temp2->next == NULL)
{
printf("\n Node Deleted From List.\n");
free(temp2);
temp2 = h = NULL;
return;
}}

if (temp2->next == NULL) {
temp2->prev->next = NULL;
free(temp2);

printf("\n Node Deleted From List.\n");
return;
}
temp2->next->prev = temp2->prev;

if (i != 1)
temp2->prev->next = temp2->next;    /* Might not need this statement if i == 1 check */

if (i == 1)
h = temp2->next;

printf("\n Node Deleted From List.\n");
free(temp2);
}
count--;
}

/* Traverse from beginning */
void traversebeg() {
temp2 = h;

if (temp2 == NULL) {
printf("\n Error : List Empty To Display.\n");
return;
}
printf("\n Linked List Elements From Begining : ");

while (temp2->next != NULL) {
printf(" %d ", temp2->n);
temp2 = temp2->next;
}
printf(" %d ", temp2->n);
printf("\n");
}

/* To traverse from end recursively */
void traverseend(int i) {
if (temp2 != NULL)
{
i = temp2->n;
temp2 = temp2->next;
traverseend(i);
printf(" %d ", i);
}}

/* To search for an element in the list */
void search() {
int data, count = 0;
temp2 = h;

if (temp2 == NULL) {
printf("\n Error : List Empty To Search For Data.\n");
return;
}
printf("\n Enter Value To Search : ");
scanf("%d", &data);

while (temp2 != NULL)
{
if (temp2->n == data) {
printf("\n Data Found In %d Position.\n",count + 1);
return;
}
else
temp2 = temp2->next;
count++;
}
printf("\n Error : %d Not found In List.\n", data);
}

/* To update a node value in the list */
void update() {
int data, data1;

printf("\n Enter The Data To Be Updated : ");
scanf("%d", &data);
printf("\n Enter New Data : ");
scanf("%d", &data1);

temp2 = h;
if (temp2 == NULL) {
printf("\n Error : List Empty, No node To Update.\n");
return;
}

while (temp2 != NULL)
{
if (temp2->n == data) {
temp2->n = data1;
traversebeg();
return;
}
else
temp2 = temp2->next;
}
printf("\n Error : %d Not Found In List To Update.\n", data);
}

/* To sort the linked list */
void sort() {
int i, j, x;
temp2 = h;
temp4 = h;

if (temp2 == NULL) {
printf("\n Error : List Empty To Sort.\n");
return;
}

for (temp2 = h; temp2 != NULL; temp2 = temp2->next) {
for (temp4 = temp2->next; temp4 != NULL; temp4 = temp4->next) {
if (temp2->n > temp4->n) {
x = temp2->n;
temp2->n = temp4->n;
temp4->n = x;
}}}
traversebeg();
}

Popular posts from this blog

Screenshots from Windows 1.01

Windows 1.0 is a graphical personal computer operating environment developed by Microsoft, released on November 20, 1985, as the first version of the Microsoft Windows line. Version 1.01 , also released in 1985, was the first point-release after Windows 1.00.   Screenshots from Windows 1.01: ⇰ Desktop  First Run Empty Desktop Desktop With Applications ⇰  Office Applications Notepad Text Editor Calculator Calendar Clock Address Book ⇰  Multimedia Applications Media player, CD player, Volume level, and Sound: This GUI doesn’t have these features. ⇰  Networking Applications Terminal Phone Dialer: This GUI doesn’t have this feature. ⇰  Internet Applications Browser, and Mail: This GUI doesn’t have these features. ⇰  Accessibility Applications Keyboard Map:  This GUI doesn’t have this feature. ⇰  Settings Desktop themes,  Display,  S...

Introduction To Algorithms, 3rd Edition

Before there were computers, there were algorithms. But now that there are computers, there are even more algorithms, and algorithms lie at the heart of computing. This book provides a comprehensive introduction to the modern study of computer algorithms. It presents many algorithms and covers them in considerable depth, yet makes their design and analysis accessible to all levels of readers. In this book, the authors tried to keep explanations elementary without sacrificing depth of coverage or mathematical rigor. Each chapter presents an algorithm, a design technique, an application area, or a related topic. Algorithms are described in English and in a pseudocode designed to be readable by anyone who has done a little programming. The book contains 244 figures — many with multiple parts — illustrating how the algorithms work. It also includes careful analysis of the running times of all algorithms. In this third edition, the entire book once again updated including changes cove...

C++ Program To Implement Casino Number Guessing Game.

#include <iostream> #include <string> #include <cstdlib> #include <ctime> using namespace std; void drawLine(int n, char symbol); void rules(); int main() { string playerName; int amount; int bettingAmount; int guess; int dice; char choice; srand(time(0)); drawLine(70,'_'); cout << "\n\n\n\t\tCASINO GAME\n\n\n\n"; drawLine(70,'_'); cout << "\n\nEnter Your Name : "; getline(cin, playerName); cout << "\n\nEnter Deposit Amount To Play Game : $"; cin >> amount;

Java: The Complete Reference, 9th Edition

This is Herb's most popular book on Java, fully updated and expanded to cover Java SE 8 (JDK 8).    Whether you're an experienced pro or just starting out, this one-stop guide will help you master this important language.  Inside you'll find comprehensive coverage of the Java language, its keywords, syntax, and fundamental programming principles.  Of course, descriptions of Java's newest features, such as lambda expressions, default interface methods, and the stream API are included. This lasting resource also describes key elements of the Java API library, such as the Collections Framework, concurrency, applets, servlets, Beans, event handling,  AWT,  Swing, and more. Coverage of JavaFX, Java's newest GUI framework, is also included. *** TO REVIEW BOOK ***  (click below) *** TO REVIEW SOURCE CODE PROBLEM  SOLUTIONS, VISIT   THIS   LINK ***