0% found this document useful (0 votes)
110 views

Doubly-Linked List Implementation Issues in C

The document discusses issues related to implementing a doubly-linked list in C. It describes the basic structure of a doubly-linked list node with pointers to the previous and next nodes. It then covers common operations on a doubly-linked list like adding nodes to an empty list, the middle, or end of an existing list. It also discusses deleting and searching for nodes as well as traversing the entire list.

Uploaded by

Dedi Bob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views

Doubly-Linked List Implementation Issues in C

The document discusses issues related to implementing a doubly-linked list in C. It describes the basic structure of a doubly-linked list node with pointers to the previous and next nodes. It then covers common operations on a doubly-linked list like adding nodes to an empty list, the middle, or end of an existing list. It also discusses deleting and searching for nodes as well as traversing the entire list.

Uploaded by

Dedi Bob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Doubly-Linked List Implementation Issues in C

• A node in a doubly-linked list is a structure that


has at least three fields. One of the fields is a data
field; the two are pointers that contains the
address of the logical predecessor node and
logical successor node in the sequence.

• An example doubly-linked list node with one data field:


struct dllnode{
int data;
struct dllnode *left; left data right
struct dllnode *right;
};

Page 1 1
Basic Operations on a Doubly-Linked List

1. Add a node.
2. Delete a node.
3. Search for a node.
4. Traverse (walk) the list. Useful for counting
operations or aggregate operations.

• Note: the operations on a doubly-linked list are


exactly the same that are required for a singly-linked
list. As we discussed before, the reasons for using a
doubly-linked list are application dependent for the
most part. Page 2 2
Adding Nodes to a Doubly-Linked List
Adding a Node
 
There are four steps to add a node to a doubly-linked list:
 
• Allocate memory for the new node.
• Determine the insertion point to be after (pCur).
• Point the new node to its successor and predecessor.
• Point the predecessor and successor to the new node.
 
Current node pointer (pCur) can be in one of two states:
• it can contain the address of a node (i.e. you are adding
somewhere after the first node – in the middle or at the end)
• it can be NULL (i.e. you are adding either to an empty list or at the
beginning of the list)

Page 3 3
Adding Nodes to an Empty Doubly-Linked List
Initial: Code:
pNew = (struct node *) /*create node*/
pNew malloc(sizeof(struct39
dllnode));
pNew -> data = 39;
pNew -> right = pHead;
pHead
pNew -> left = pHead;
pHead = pNew;

pCur

After:

pNew 39

pHead

pCur

Page 4 4
Adding a Node to the Middle of a Doubly-Linked List

Before:

pNew 64

55 124

pCur
After:

pNew 64

55 124

pCur

Page 5 5
Adding a Node to the Middle of a Doubly-Linked List
The Code

pNew = (struct node *) malloc(sizeof(struct dllnode));


pNew -> data = 64;
pNew -> left = pCur;
pNew -> right = pCur -> right;
pCur -> right -> left = pNew;
pCur -> right = pNew;
 

Page 6 6
Adding a Node to the End of a Doubly-Linked List

Before:

pNew 84

55 74

pCur
After:

pNew 84

55 74

pCur

Page 7 7
Adding a Node to the end of a Doubly-Linked List

The Code
pNew = (struct node *) malloc(sizeof(struct dllnode));
pNew -> data = 84;
pNew -> left = pCur;
pNew -> right = pCur -> right;
pCur -> right = pNew;

Page 8 8
Inserting a Node Into a Doubly-Linked List
//insert a node into a linked list
struct node *pNew;
pNew = (struct node *) malloc(sizeof(struct node));
pNew -> data = item;
if (pCur == NULL){
//add before first logical node or to an empty list
pNew -> left = pHead;
pNew -> right = pHead;
pHead = pNew;
}
else {
if (pCur -> right == NULL) {
//add at the end
pNew -> left = pCur;
pNew -> right = pCur -> right;
pCur -> right = pNew;
}
else { //add in the middle
pNew -> left = pCur;
pNew -> right = pCur -> right;
pCur -> right -> left = pNew;
pCur -> right = pNew;
}
}
}
Page 9 9
Deleting a Node from a Doubly-Linked List

• Deleting a node requires that we logically remove the


node from the list by changing various links and then
physically deleting the node from the list (i.e., return it
to the heap).
• Any node in the list can be deleted. Note that if the
only node in the list is to be deleted, an empty list will
result. In this case the head pointer will be set to
NULL.
• To logically delete a node:
– First locate the node itself (pCur).
– Change the predecessor’s and succesor’s link fields to point
each other (see example).
– Recycle the node using the free() function.
Page 10 10
Deleting the First Node from a Doubly-Linked List

Before: Code:
pHead = pCur -> right;
pHead
pCur ->right -> left = NULL;
75 124
free(pCur);
pCur

After:
pHead Recycled 124

pCur

Page 11 11
Deleting a Node from a Linked List – General Case

Before:

75 23 46 12477

pCur

After:

23 Recycled 77
75 124

pCur

Page 12 12
Deleting a Node From a Doubly-Linked List
The Code
//delete a node from a linked list
if (pCur -> left == NULL){
//deletion is on the first node of the list
pHead = pCur -> right;
pCur -> right -> left = NULL;
{
else {
//deleting a node other than the first node of the list
pCur -> left -> right = pCur -> right;
pCur -> right -> left = pCur -> left;
}
free(pCur).

Page 13 13
Searching a Doubly-Linked List
• Notice that both the insert and delete operations on a linked list must
search the list for either the proper insertion point or to locate the node
corresponding to the logical data value that is to be deleted.

//search the nodes in a linked list


pCur = pHead;
//search until the target value is found or the end of the list is reached
while (pCur != NULL && pCur -> data != target) {
pCur = pCur -> right;
}
//determine if the target is found or ran off the end of the list
if (pCur != NULL)
found = 1;
else
found = 0;

Page 14 14
Traversing a Doubly-Linked List
• List traversal requires that all of the data in the list be
processed. Thus each node must be visited and the data value
examined.
• Notice that this is identical code to the singly-linked list except
for the name of the pointer link.

//traverse a linked list


Struct dllnode *pWalker;
pWalker = pHead;
printf(“List contains:\n”);
while (pWalker != NULL){
printf(“%d ”, pWalker -> data);
pWalker = pWalker -> right;
}

Page 15 15

You might also like