0% found this document useful (0 votes)
22 views37 pages

SE233 Linked Lists

Chapter 3 of SE233 discusses Linked Lists as a data structure consisting of nodes, each containing data and a pointer to the next node. It covers operations such as creating, inserting, deleting, and finding nodes, along with efficiency considerations for these operations. The chapter also outlines static and dynamic implementations of linked lists and introduces different types of linked lists, concluding with an assignment related to implementing a bus network system.

Uploaded by

sifen.abebe
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)
22 views37 pages

SE233 Linked Lists

Chapter 3 of SE233 discusses Linked Lists as a data structure consisting of nodes, each containing data and a pointer to the next node. It covers operations such as creating, inserting, deleting, and finding nodes, along with efficiency considerations for these operations. The chapter also outlines static and dynamic implementations of linked lists and introduces different types of linked lists, concluding with an assignment related to implementing a bus network system.

Uploaded by

sifen.abebe
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/ 37

SE233 : Data Structures

&
Algorithm Analysis

Chapter 3: Linked List (ADT)


Different data structures

 Linked List
 Stack
 Queue
 Tree
 Network
 Graph
Linked List

Linked list is a collection of elements conventionally


called nodes. Each node contains a data portion and
a pointer that points to the “next” node. The entire
list is referenced by a separate pointer,
conventionally called head, that points to the first
element of the list.
Linked List

Head

Data Data Data


Operations on List

 Create (): Construct an empty list –


a list with no nodes yet!

 insertNode(): Insert a new node


into a linked list.
Operations on List …

 deleteNode(): Delete a node from a


linked list.

 findNode(): search for a particular


node
Operations on List …

 isFull(): It returns true if the list is


full; false otherwise.

 isEmpty(): It returns true if the list


is empty; false otherwise.

 Destroy(): Destroy the list.


Efficiency consideration of List
 Insertion and deletion:
 O(1)

 Finding/searching:
 O(n)

 Exercise: The above searching


performance is the same as the
performance of the sequential search we
have seen in chapter 2. Could binary
searching be adopted to Linked List?
Implementation of List

 Static Implementation

 Dynamic Implementation
Static Implementation of List
Node Definition List Definition
 public class Node {  public class LList {
 private double data;  private int head;
 private int next;  private Node[]list;
public Node(){}  public LList(int size){
 public Node(double  list = new Node[size];
data){  head= -1;
 this.data = data;  }
 next = -1;  /*getters and Setters go
 } here */
 /*getters and Setters go  }
here */
 }
Example: Use of List to maintain
real numbers in ascending order
Example
# Data Next
0
1
Let us see the effect
2
of inserting the
3
following numbers on
4 the List:
5
24, 25.6, -29.4, 21,
6
210.5, -23, 22.3, -
… 26.2

99
Example: Create the list
# Data Next
0
1  At first, List shall be
2 “created” as empty.
3 Hence, head should point
to “null”.
4
5  How shall “null” be
6 represented in the array
… implementation?

 head = -1;
99
Example: Inserting a new element
# Data Next
0 24 -1
1
 Inserting 24
2
3
4  List[0].data = 24;
5  List[0].next = -1;
6
…  head  0

99
Example: Inserting a new element
# Data Next
0 24 1
1 25.6 -1
 Inserting 25.6
2
3
4  List[1].data = 25.6;
5  List[1].next = -1;
6  List[0].next = 1;
…  head  0 (unchanged!)

99
Example: Inserting a new element
# Data Next
0 24 1
1 25.6 -1
 Inserting -29.4
2 -29.4 0
3
 List[2].data = -29.4;
4
5
6  List[2].next = 0;

 head  2 (changed!)
99
Example: Inserting a new element
# Data Next
0 24 1
1 25.6 -1
2 -29.4 3  Inserting 21
3 21 0
 List[3].data = 21;
4
 List[3].next = 0;
5
6
 List[2].next = 3;;

 head  2 (unchanged!)
99
Example: Inserting a new element
# Data Next
0 24 1
1 25.6 4
 Inserting 210.5
2 -29.4 3
3 21 0
 List[4].data = 210.5;
4 210.5 -1
 List[4].next = -1;
5
6
 List[1].next = 4;

 head  2 (unchanged!)
99
Example: Inserting …
# Data Next
0 24 1
1 25.6 4
2 -29.4 7  This is what the List
3 21 6 looks like when all
4 210.5 -1 the data are inserted.
5 -23 3
6 22.3 0  head  2
7 -26.2 5

99
Example: Deleting an element
# Data Next
0 24 1
1 25.6 4
2 -29.4 7  Deleting -23
3 21 6
4 210.5 -1  List[7].next = 3;
5 -23 3
6 22.3 0  head  2 (unchanged!)
7 -26.2 3
 Is there anything that can
… be done to the node “-23”?
99
Example: Deleting an element
# Data Next
0 24 1
1 25.6 4
 Deleting 22.3
2 -29.4 7
3 21 0  List[3].next = 0;
4 210.5 -1
5 -23 3  head  2 (unchanged!)
6 22.3 0
7 -26.2 3  Is there anything that
can be done to the node
… “22.3”?
99
Example: Deleting an element
# Data Next
0 24 1
1 25.6 4
2 -29.4 7  Deleting -29.4
3 21 0
4 210.5 -1  head = 7 (changed!)
5 -23 3
6 22.3 0  Is there anything
7 -26.2 3 that can be done to
… the node “-29.4”?
99
Example: Deleting an element
# Data Next
0 24 1
1 25.6 -1
 Deleting 210.5
2 -29.4 7
3 21 0  List[1].next = -1;
4 210.5 -1
5 -23 3  head = 7 (unchanged!)
6 22.3 0
7 -26.2 3  Is there anything that
can be done to the node
… “210.5”?
99
Example: Insertion again …
# Data Next
0 24 1
1 25.6 -1
2 -29.4 7  Inserting 23.4
3 21 0
4 210.5 -1  Where shall it be
5 -23 3 inserted?
6 22.3 0
7 -26.2 3

99
Insertion again … Two options
# Data Next
0 24 1
1 25.6 -1  Inserting 23.4
2 -29.4 7
3 21 0 1. To insert it at the next
4 210.5 -1 free space (ie. at 8).
5 -23 3
6 22.3 0 2. To insert it at one of
7 -26.2 3 the deleted spaces (ie.
among 2, 4, 5, 6).

99
Option 1: Insert at the next free space
# Data Next
0 24 1
1 25.6 -1
2 -29.4 7 This option does not
3 21 0 help us manage the
210. deleted spaces. It
4 -1
5 simply continues to
5 -23 3 provide us free
6 22.3 0 spaces until the
7 -26.2 3 array is exhausted.

99
Option 2: Insert at one of the deleted
spaces
# Data Next
0 24 1
1 25.6 -1
In this option, we
2 -29.4 7 need to define a
3 21 0 mechanism to
4 210.5 -1 identify the deleted
5 -23 3 spaces. Besides, we
6 22.3 0 still need to use the
7 -26.2 3 free spaces as well.

99
Option 3: Devise a mechanism that manages
free and deletes spaces
# Data Next
0 24 1
1 25.6 -1
One possible mechanism is
2 -29.4 7 to maintain the free spaces
3 21 0 (including the deleted
ones) in a “separate”
4 210.5 -1 linked list inter woven in
5 -23 3 the same array. In other
words, we manage the
6 22.3 0 linked list of floating
7 -26.2 3 numbers and the linked list
… of free spaces (ie. indices)
both in the same array.
99
Option 3: Two linked lists …
# Data Next
0
1
Linked list of floating numbers
2
Floating #
3
4
5 Linked list of free spaces
6
7

99
Initially … the linked list of floating numbers
# Data Next
0
1
2 The linked list of floating #s is
3 empty. Hence:
head = -1
4
5
6
7

99
Initially … the linked list of free spaces
# Data Next
0 1
1 2
2 3 We can create a linked list of free
3 4 spaces as follows:
for(int i=0; i<=size-2; i++){
4 5
List[i] = new Node();
5 6
List[i].setNext(i+1);
6 7 }
7 8 List[size-1] = new Node();
… List[size-1].setNext(-1);
99 -1 setAvail(0);
setHead(-1);
Static Implementation for a
Linked List
//Code Goes Here
Dynamic Implementation of List
Node Definition List Definition
 public class Node {  public class LList {

 private double data;  private Node head;


 private Node next;  public LList(){

 public Node(double  //head= null;


data){  }
 this.data = data;  /*getters and Setters go
 this.next = null; here */
 }  }
 /*getters and Setters go
here */
 }
Dynamic Implementation for a
Linked List
//Code Goes Here
Types of Linked List

 Singly Linked List


 Doubly Linked List
 Circular Linked List
Assignment on Linked List

 Implement a metropolitan bus


network system for Addis Ababa
City Administration.
 End of Third Chapter

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy