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

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 Check Whether A Number Is Palindrome Or Not.

This program takes an integer from user and the integer is reversed. If the reversed integer is equal to the integer entered by user then that number is a palindrome. If not that number is not a palindrome.   #include <stdio.h> int main()  { int num, temp, remainder, reverse = 0; printf("Enter an integer: "); scanf("%d", &num); /*  original number is stored at temp */ temp = num; while (num > 0)  { remainder = num % 10; reverse = reverse * 10 + remainder; num /= 10;   }

The Basics Of C Programming - Part 3.

There are a number of different C input commands, the most useful of which is the scanf command. To read a single integer value into the variable called a you can use: scanf("%d",&a); When the program reaches the scanf statement it pauses to give the user time to type something on the keyboard and continues only when users press Enter or Return, to signal that he, or she, has finished entering the value. Then the program continues with the new value stored in a. In this way, each time the program is run the user gets a chance to type in a different value to the variable and the program also gets the chance to produce a different result! The final missing piece in the jigsaw is using the printf function, the one we use to print the value currently being stored in a variable. To display the value stored in the variable a you can use: printf("The value stored in a is %d",a); Note: the scanf function does not prompt for an input. You should ge...

C++ Program To Implement Bank Management System.

#include<iostream> #include<fstream> #include<cctype> #include<iomanip> #include <cstdlib> using namespace std; class account { int acno; char name[50]; int deposit; char type; public: void create_account(); //function to get data from user void show_account() const; //function to show data on screen void modify(); //function to add new data void dep(int); //function to accept amount and add to balance amount void draw(int); //function to accept amount and subtract from balance amount void report() const; //function to show data in tabular format int retacno() const; //function to return account number int retdeposit() const; //function to return balance amount char rettype() const;  //function to return type of account }; void account::create_account() { cout<<"\nEnter The Account No. : "; cin>>acno; cout<<"\n\nEnter The Name Of The Account Holder : "; cin.ig...

Java: A Beginner's Guide, 6th Edition

This is Herb's step-by-step introduction to Java, updated for Java SE 8 (JDK 8). If you are just learning Java, then this is the book for you.  It starts at the beginning, explaining the history of Java, why it's important to the Web, and how it relates to the world of programming at large.  You then learn how to obtain the Java Development Kit (JDK) and write your first Java program. Next, it's on to the Java fundamentals, including data types, operators, control statements, classes, objects, and methods.  You'll then progress to more advanced topics, such as inheritance, exception handling, the I/O system, multithreading,  applets, and lambda expressions. Also included is coverage of some of Java's most powerful features, such as generics, autoboxing, enumerations, and static import.  An introduction to JavaFX, Java's newest GUI framework, is also included. *** TO REVIEW BOOK *** (click below) *** TO REVIEW SOURCE CODE PROBLEM  SO...