0% found this document useful (0 votes)
45 views4 pages

Doubly Linked List: Deletion

The document discusses doubly linked lists and array implementation of linked lists. Doubly linked lists allow traversal in both directions using two pointers in each node. Array implementation uses an array to store nodes and links them together to emulate a linked list, allowing flexible insertion and deletion while using static memory allocation.

Uploaded by

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

Doubly Linked List: Deletion

The document discusses doubly linked lists and array implementation of linked lists. Doubly linked lists allow traversal in both directions using two pointers in each node. Array implementation uses an array to store nodes and links them together to emulate a linked list, allowing flexible insertion and deletion while using static memory allocation.

Uploaded by

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

Deletion

Doubly Linked List


Simple linked lists only allow making search from the
beginning to end.
Doubly linked lists allow searches in both directions
(while keeping a single pointer)

pCur
pHead

Each node contains two pointers, one to the next node, one
to the preceding node.
struct dllNode {
int data;
struct dllNode *left;
struct dllNode *right;
}

Node Structure:
left

data

right

pHead

Advantage:
insertion and deletion can be easily done with a single
pointer.

pCur->left->right = pCur->right;
pCur->right->left = pCur->left;

(Assuming pCur->left and pCur->right are not NULL)

Array Implementation of Linked Lists

Insertion

The idea is to begin with a large array and regard the array
as our allocation of unused space. We then set up our own
procedures to keep track of which parts of the array are
unused and to link entries of the array together in the
desired order.

pCur
pHead

Dynamic memory:

pNew

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

Disadvantage of Doubly Linked Lists:


extra space for extra link fields
maintaining extra link during insertion and deletion

By using an array well lose the flexibility of dynamic


allocation of storage. But all the remaining advantages
of linked lists such as insertions and deletions
anywhere in the list will still apply.
We can easily rearrange items without moving them.

Implementation:

Example
struct
int
int
};
struct

#define NIL

element{
data;
next;

/* array nodes */
struct element grades[10];
int head;
int avail;

element grades[10];

avail

// keeps head of list


// gives the index of the first
// available element in array.

/* Set up a list of available space and write


* functions to obtain a new node and return a
* node t o available space.
*/

data
0
1
2
3
4
5
6
7
8
9

-1

70
80
62
95
85
50
-

next
3
4
0
8
6
7
9
2
-1
-1

/* Initially */
avail = 0;
head = NIL;

head
5

An array of struct element

Sorted Linked List: in ascending order of grades


Add 65 at position 1.

/* Function to obtain a new node location.


* Returns the index of the next available entry
* if there is one
*/
int newNode(struct element List[], int * avail)
{
int loc;
if (*avail == -1)
return 1;
else{
loc = *avail;
*avail = List[*avail].next;
return loc;
}
}

int releaseNode(struct element List[], int loc,


int *avail)
{
List[loc].next = *avail;
*avail = loc;
}

void trave rse(struct element List[], int head)


{
int c;

//
//
//
//

Given the head, the predecessor (pre) and the


data to be inserted (item), we must allocate
space in the array for the new node (new)
and adjust the indices.

void insertNode(struct element List[], int *head,


int pre, int item, int* avail)
{
int new;
new = newNode(List, avail);
if (new == -1)
printf(No more space, item cannot be inserted\n);
else {
List[new].data = item;
if (pre == NIL){
List[new].next = pre;
*head = new;
}
else {
List[new].next = List[pre].next;
List[pre].next = new;
}
}

c = head;
while (c != NIL){
printf(%d\n, List[c].data);
c = List[c].next;
}
}

You might also like