­
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...

Java Tutorial For Beginners: Part 2.

Problem: Java Program To Solve Tower Of Hanoi Problem Using Stacks import java.util.*; public class TowerOfHanoiUsingStacks { public static int N; @SuppressWarnings("unchecked") public static Stack<Integer>[] tower = new Stack[4]; public static void main(String[] args) { Scanner scan = new Scanner(System.in); tower[1] = new Stack<Integer>(); tower[2] = new Stack<Integer>(); tower[3] = new Stack<Integer>(); System.out.print("Enter The Number Of Disks: "); int num = scan.nextInt(); N = num; toh(num); scan.close(); } /* Function to push disks into stack */ public static void toh(int n) { for (int d = n; d > 0; d--) tower[1].push(d); display(); move(n, 1, 2, 3); }

How Are The Web Pages Written?

Web pages are written in HTML, the web programming language that tells web browsers how to structure and present content on a web page. In other words, HTML provides the basic building blocks for the web. And for a long time, those building blocks were pretty simple and static: lines of text, links, and images. Today, we expect to be able to do things like play online chess or seamlessly scroll around a map of our neighborhood, without waiting for the entire page to reload for every chess move or every map scroll. The idea of such dynamic web pages began with the invention of the scripting language JavaScript. JavaScript support in major web browsers meant that web pages could incorporate more meaningful real-time interactions. For example, if you have filled out an online form and hit the “submit” button, the web page can use JavaScript to check your entries in real-time and alert you almost instantly if you had filled out the form incorrectly. But the dynamic web as we ...

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;