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

Data Structures and Algorithms Linked List

This document provides an overview of linked lists in 3 sentences: Linked lists arrange dynamically allocated structures into a chain by connecting nodes using pointers, with each node containing a data element and a pointer to the next node. A linked list has a head pointer pointing to the first node, and each subsequent node's next pointer points to the next in the chain until the last node which has a null next pointer. Common linked list operations include insertion and deletion of nodes at the head, end, or within the list by manipulating the pointers between nodes.

Uploaded by

Muhammed khaled
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Data Structures and Algorithms Linked List

This document provides an overview of linked lists in 3 sentences: Linked lists arrange dynamically allocated structures into a chain by connecting nodes using pointers, with each node containing a data element and a pointer to the next node. A linked list has a head pointer pointing to the first node, and each subsequent node's next pointer points to the next in the chain until the last node which has a null next pointer. Common linked list operations include insertion and deletion of nodes at the head, end, or within the list by manipulating the pointers between nodes.

Uploaded by

Muhammed khaled
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Data Structures and Algorithms

Linked List
Lecture 3

1
◼ Arrange dynamically allocated structures
into a new structure called a linked list
◼ Think of a set of children’s pop beads
◼ Connecting beads to make a chain
◼ You can move things around and re-connect
the chain
◼ We use pointers to create the same effect

2
◼ A sequence of nodes linked by pointers:

e
head First Last NULL
next cursor

◼ First node pointed to by head. Contains a data


element (e) and a next pointer to next node.
◼ Last node’s next is NULL.
◼ A cursor points to the current node. It can advance
in one way only to next node, e.g. to traverse whole
list.
3
◼ Suppose each node is to contain a word from the
dictionary, and the number of times such word
occurs in a document.

struct elType // specify data element


{ string word; int count};
struct node // specify node structure
{ elType e; node *next; };
node *p, *q; // pointers to nodes of type node

4
◼ Each of the pointers p, q can point to a
struct of type node:
◼ e.word (string)
◼ e.count (int)
◼ next (pointer to next node)
Struct of type node
word count next

String Integer Address


5
◼ Allocate storage of 2 nodes
p = new node;
q = new node;

◼ Assign data to nodes


elType el1 , el2;
el1.word = “hat”; el1.count = 2;
el2.word = “top”; el2. count = 3;
p->e = el1; q->e = el2;

6
p hat 2 ?

q top 3 ?

7
Suppose the address in q is stored in next field of node
pointed to by p and NULL is stored in the last next field:

p->next = q; q->next = NULL;

p hat 2 next

q top 3 NULL

8
◼ Insertion at head of list
◼ Inserting a node after a given node
◼ Insert at end of list
◼ Delete a head node
◼ Delete a non-head node

9
First Last

hat 2 top 3
head
3 2 elType el;
New el.word = “if”;
el.count = 4;
if 4 p = new node;
p-> e = el;
1 p->next = head;
p head = p;

10
cursor
head

if 4 hat 2 top 3

3 2
el.word = “the”;
el.count = 5;
p = new node; p the 5
p-> e = el; New
p-> next = cursor-> next; 1
cursor->next = p;
11
cursor

Last

hat 2 top 3
3

New
p = new node;
2
if 4 p->e = el;
p->next = NULL;
1 cursor->next = p;
p
12
1 cursor
head

if 4 hat 2 the 5 top 3


3

2
cursor = head;
head = head->next;
delete cursor;

13
cursor cursor
prev q
Successor
1
hat the top
3
2 Pre:
cursor points to node
prev points to
node *q;
q = cursor;
predecessor node
cursor = cursor->next;
prev->next = cursor;
delete q;
14
http://www.cosc.canterbury.ac.nz/people/
mukundan/dsal/LinkListAppl.html

15
The Circular List:

head tail
cursor

Notice that tail->next == head


16
The Doubly Linked List

back next

cursor

To advance: cursor = cursor->next;


To back : cursor = cursor->back;
17
◼ The Circular Doubly Linked List

◼ The 2-D List:

18

You might also like