You are on page 1of 16

ASSIGNMENT 14

CREATION OF A DOUBLY LINKED LIST AND INSERTION AND


DELETION OF NODES, AND NODE COUNTING BY USING THE
LINKED LIST
STATEMENT:

In computer science, a doubly linked list is a linked data structure that consists of a set of
sequentially linked record called nodes. Each node contains two fields, called links, that are
references to the previous and to the next node in the sequence of nodes. The beginning and
ending nodes' previous and next links, respectively, point to some kind of terminator, typically a
sentential node or null, to facilitate traversal of the list. If there is only one sentinel node, then
the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked list
formed from the same data items, but in opposite sequential orders.

A technique known as XOR-linking allows a doubly linked list to be implemented using a single link field
in each node. However, this technique requires the ability to do bit operations on addresses, and therefore
may not be available in some high-level languages. Double-linked lists require more space per node
(unless one uses XOR-linking), and their elementary operations are more expensive; but they are often
easier to manipulate because they allow sequential access to the list in both directions. In a doubly linked
list, one can insert or delete a node in a constant number of operations given only that node's address. To
do the same in a singly linked list, one must have the address of the pointer to that node, which is either
the handle for the whole list (in case of the first node) or the link field in the previous node. Some
algorithms require access in both directions. On the other hand, doubly linked lists do not allow tail-
sharing and cannot be used as persistent data structures.

ALGORITHM:

INPUT: The number of elements to create the list and the elements which are to be inserted.

OUTPUT: The list after creating it.

The linked list after inserting an element at any position of the list.

The linked list after deleting an element from any position of the list.

The number of elements or the number of nodes of the linked list.

PROCESS:

Step 1: Define Null0

Step 2: Define a structure named ‘dnode’ which has two parts−

 The data part of integer type.


 The ‘left’ part (points to it’s left node) and the ‘right’ part (points to it’s right node)
which is a structure (dnode) type pointer.

Step 3: Globally declare a variable ‘head’(a ‘dnode’ type pointer as defined in Step 2)

Step 4: Create a function “void createlist()”

Step 4.1: Create a new node in ‘head’(a ‘dnode’ type pointer as defined in Step 2) by using ‘malloc’
function

Set left[head] Null and right[head]  Null

Step 4.2: Take the number of elements in an integer type variable ‘n’ to create the list

Step 4.3: For i=0 to i<n repeat Step 4.4 to Step 4.7

Step 4.4: Take the element in an integer type variable ‘x’ to insert into the list

Step 4.5: Create a new node in ‘p’(a ‘dnode’ type pointer as defined in Step 2) by using ‘malloc’ function.

Set data[p] x

Set left[p] Null and Set right[p]  Null

Step 4.6: If(i=0) then

Set right[head] p and left[head]  p

Ser ptr p

[End of ‘If’]

Step 4.7: Else

Set right[ptr]  p

Set left[p]  ptr

Set ptr p

Set right[head]  ptr

[End of ‘Else’]

[End of Step 4.3 ‘For’ loop]

[End of the function “void createlist()”]

Step 5: Create the function “void displaylist()”

Step 5.1: Set ptrleft[head]

Step 5.2: While(ptr≠Null) then repeat Step 5.3 and Step 5.4

Step 5.3: Print data[ptr]

Step 5.4: Set ptrright[ptr]


[End of Step 5.2 ‘While’ loop]

[End of the function “void displaylist()”]

Step 6: Create the function “void nodecount()”

Step 6.1: Set ptrleft[head]

Step 6.2: While(ptr≠NULL) then repeat Step 6.3 and Step 6.4

Step 6.3: Set n n+1

Step 6.4: Set ptrright[ptr]

[End of Step 6.2 ‘While’ loop]

Step 6.5: Print the number of nodes i.e ‘n’ which is an integer type variable

[End of the function “void nodecount()”]

Step 7: Create the function “void insertanyposition()”

Step 7.1: If(head=Null) then print “List does not exist” and return.

[End of ‘If’]

Step 7.2: Set ptr1 left[head]

Step 7.3: While(ptr1≠Null) then repeat Step 7.4 and Step 7.5

Step 7.4: Set n n+1

Step 7.5: Set ptr1right[ptr1]

[End of Step 7.3 ‘While’ loop]

Step 7.6: Take the position from the user in an integer type variable ‘pos’ at which position the
element will be inserted.

Step 7.7: If(pos=1) then

a) Set ptrleft[head]
b) Take the element in an integer type variable ‘x’ to insert into the list
c) Create a new node in ‘p’ (a ‘dnode’ type pointer as defined in Step 2) by using
‘malloc’ function.

Set data[p]  x

Set left[p] Null and Set right[p]  Null

d) Set right[p]  ptr


e) Set left [ptr]  p
f) Set left[head]  p

[End of ‘If’]
Step 7.8: Else if(pos=(n+1)) then

a) Set ptrright[head]
b) Take the element in an integer type variable ‘x’ to insert into the list
c) Create a new node in ‘p’(a ‘dnode’ type pointer as defined in Step 2) by using
‘malloc’ function.
Set data[p]  x
Set left[p] Null and Set right[p]  Null
d) Set right[ptr]  p
e) Set left[p]  ptr
f) Set right[head]  p

[End of ‘Else if’]

Step 7.9: Else

a) Set ptrleft[head]
b) While(pos>c) then repeat

Set c c+1

Set ptr1 ptr

Set ptrright[ptr]

[End of ‘While’]

c) Take the element in an integer type variable ‘x’ to insert into the list

d) Create a new node in ‘p’(a ‘dnode’ type pointer as defined in Step 2) by using
‘malloc’ function.

Set data[p]  x

Set left[p] Null and Set right[p]  Null

e) Set right[ptr1]  p

f) Set left[p]  ptr1

g) Set left[ptr]  p

h) Set right[p]  ptr

[End of ‘Else’]

Step 7.10: Call the function “displaylist()”

[End of the function “void insertanyposition()”]

Step 8: Create the function “void delfromanyposition()”

Step 8.1: If(head=Null) then print “List does not exist” and return.

[End of ‘If’]

Step 8.2: Set ptr1left[head]


Step 8.3: While(ptr1≠Null) then repeat Step 7.4 and Step 7.5

Step 8.4: Set n n+1

Step 8.5: Set ptr1right[ptr1]

[End of Step 8.3 ‘While’ loop]

Step 8.6: Take the position from the user in an integer type variable ‘pos’ at which position the
element will be inserted.

Step 8.7: If(pos=1) then

a) Set ptrleft[head]
b) Set left[head] right[ptr]
c) Set left[right[ptr]]  Null
d) Set right[ptr]  Null
e) Free ptr.
[End of ‘If’]

Step 8.8: Else if(pos=n) then

a) Set ptrright[head]
b) Set right[head] left[ptr]
c) Set right[left[ptr]]  Null
d) Set left[ptr]  Null
e) Free ptr

[End of ‘Else if’]

Step 8.9: Else

a) Set ptrleft[head]
b) While(pos>c) then repeat

Set c c+1

Set ptr1 ptr

Set ptrright[ptr]

[End of ‘While’]

c) Set right[ptr1] right[ptr]


d) Set left[right[ptr]]  ptr1
e) Set left[p] Null and Set right[p]  Null
f) Free ptr

[End of ‘Else’]

Step 8.10: Call the function “displaylist()”.

[End of the function “void delfromanyposition()”]

Step 9: Create the function “void main()”


[Creating the program as a ‘menu driven’ program]

Step 9.1: Print the menu to take input from user.

Step 9.2: Do from Step 9.3 to Step 9.6

Step 9.3: Take an input in an ‘integer’ type variable ‘ch’.

Step 9.4: Switch(ch) follow Step 9.5

Step 9.5: Call the functions ‘1. createlist()’ , ‘ 2. displaylist()’ , ‘3. nodecount()’ ,
‘4.insertanyposition()’, ‘5. delfromanyposition()’ , ‘6. Exit’ depending on the input ‘ch’.

[End of the function ‘Switch’]

Step 9.6: While (1) go to Step 9.2

[End of Step 9.2 ‘Do-while’ loop]

[End of the function “void main( )”]

Step 10: Stop.

PROGRAM CODE:
#include<stdio.h>

#include<stdlib.h>

#include<alloc.h>

#define NULL 0

// * Define a structure named ‘dnode’ * //

typedef struct dnode

int data;

struct dnode *left,*right;

}dnode;

// * Declaring a global variable ‘head’ (a ‘dnode’ type pointer) * //

dnode *head;

// * Create the linked list * //

void createlist()

dnode *ptr,*p;

int i,n,x;
head =(dnode*)malloc(sizeof(dnode));

head->left=head->right=NULL;

printf("Enter the number of elements of the list: ");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("Enter value: ");

scanf("%d",&x);

p=(dnode*)malloc(sizeof(dnode));

p->left=p->right=NULL;

p->data=x;

if(i==0)

head->right=head->left=p;

ptr=p;

else

ptr->right=p;

p->left =ptr;

ptr=p;

head->right=ptr;

// * Display the linked list *//

void displaylist()

dnode *ptr;

ptr=head->left;
printf("\n");

while(ptr!=NULL)

printf("%d->",ptr->data);

ptr=ptr->right;

printf("\n");

//* Counting the number of node *//

void nodecount()

int n=0;

dnode *ptr;

ptr=head->left;

while(ptr!=NULL)

n++;

ptr=ptr->right;

printf("\nThe number of node is %d",n);

// * Insert an element at any position of the linked list *//

void insertanyposition()

int x,pos,c=1,n=0;

dnode *p,*ptr,*ptr1;

if(head==NULL)

printf("\nList does not exist\n");

return;
}

ptr1=head->left;

while(ptr1!=NULL)

n++;

ptr1=ptr1->right;

printf("\nEnter the position: ");

scanf("%d",&pos);

if(pos==1)

ptr=head->left;

printf("\nEnter element to insert: ");

scanf("%d",&x);

p=(dnode*)malloc(sizeof(dnode));

p->data=x;

p->left=p->right=NULL;

p->right=ptr;

ptr->left=p;

head->left=p;

else if(pos==(n+1))

ptr=head->right;

printf("\nEnter the element to insert: ");

scanf("%d",&x);

p=(dnode*)malloc(sizeof(dnode));

p->data=x;

p->left=p->right=NULL;

ptr->right=p;
p->left=ptr;

head->right=p;

else

ptr=head->left;

while(pos>c)

c++;

ptr1=ptr;

ptr=ptr->right;

printf("\n Enter the element to insert: ");

scanf("%d",&x);

p=(dnode*)malloc(sizeof(dnode));

p->data=x;

p->left=p->right=NULL;

ptr1->right=p;

p->left=ptr1;

ptr->left=p;

p->right=ptr;

printf("\nAfter inserting at the %dth position the list is: ",pos);

displaylist();

//* Delete an element from any position of the linked list *//

void delfromanyposition()

int pos,c=1,n=0;

dnode *ptr,*ptr1;
if(head==NULL)

printf("List does not exist");

return;

ptr1=head->left;

while(ptr1!=NULL)

n=n+1;

ptr1=ptr1->right;

printf("\nEnter the position: ");

scanf("%d",&pos);

if(pos==1)

ptr=head->left;

head->left=ptr->right;

(ptr->right)->left=NULL;

ptr->right=NULL;

free(ptr);

else if(pos==n)

ptr=head->right;

head->right=ptr->left;

(ptr->left)->right=NULL;

ptr->left=NULL;

free(ptr);

else
{

ptr=head->left;

while(pos>c)

c++;

ptr1=ptr;

ptr=ptr->right;

ptr1->right=ptr->right;

(ptr->right)->left=ptr1;

ptr->left=ptr->right=NULL;

free(ptr);

printf("\nAfter deleting from the %dth position the list is: ",pos);

displaylist();

void main()

int ch;

do

printf("\n1. Create List\n2.Display List\n3.Count the no. of node\n4.Insert at any


position\n5.Delete from any position\n6:Exit\nEnter your choice: ");

scanf("%d",&ch);

switch(ch)

case 1:printf("\nCreate list:\n");

createlist();

break;
case 2:printf("\n Display list:\n");

printf(“\nThe linked list is: \n“);

displaylist();

break;

case 3:printf("\nCount the no. of node:\n");

nodecount();

break;

case 4:printf("\nInsert at any position:\n");

insertanyposition();

break;

case 5:printf("\nDelete from any position:\n");

delfromanyposition();

break;

case 6:exit(0);

default:printf("You are pressing the wrong key");

}while(1);

OUTPUT:
DISCUSSION:
The first and last nodes of a doubly linked list are immediately accessible (i.e., accessible without
traversal, and usually called head and tail) and therefore allow traversal of the list from the
beginning or end of the list, respectively: e.g., traversing the list from beginning to end, or from
end to beginning, in a search of the list for a node with specific data value. Any node of a doubly
linked list, once obtained, can be used to begin a new traversal of the list, in either direction
(towards beginning or end), from the given node.

The two node links allow traversal of the list in either direction. 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.

You might also like