• No results found

Linked Lists

N/A
N/A
Protected

Academic year: 2020

Share "Linked Lists"

Copied!
39
0
0

Loading.... (view fulltext now)

Full text

(1)

Linked Lists

(2)

Abstract Data Type (ADT)

a set of objects

• a set of operations

Same set of objects ,

different sets of operations =>

different ADTs

ADTs are implemented using classes,

hiding implementation details:

(3)

LIST ABSTRACTION

Definition:

A linear configuration of

elements, called nodes.

(4)

Basic operations

To create/destroy a list

To expand/shrink the list

Read/Write operations

Changing the current node (moving along the

list)

To report current position in the list

(5)

Specifications for the ADT List

Operations on lists

▫ Add new entry – at end, or anywhere

▫ Remove an item

▫ Remove all items

▫ Replace an entry

▫ Look at any entry

▫ Look for an entry of a specific value

▫ Count how many entries

▫ Check if list is empty, full

▫ Display all the entries

(6)

Limitations of Arrays

Limitations of an array are:

•Size of array is fixed; even dynamically

allocated arrays' size cannot be changed.

•Adding element to front or middle of

(7)

Potential Problem Operations

add

, remove, replace work OK when

valid position given

Remove

not meaningful on empty lists

A list could become full, what happens to

(8)

A collection of connected nodes

with a set of operations.

Nodes are divided into to bit of

information:

1.

Data

2.

Pointer

(9)

Characteristics

Insert and delete nodes in any order

The nodes are connected

Each node has two components

Information (data)

Link to the next node

The nodes are accessed through the links

between them

(10)

Understanding the linked list

• Linked list is not a continuous memory location as array so each node in linked list is stored at different memory location , in order to access the whole list or to weave them in one list each node should have the address of the next respective node.

• And to access the list we should always keep safe the address of first node so that we can access the whole list with the help of the first node, and if in case we lose the address of the first node then the whole list will become a garbage as we cannot find or access this list again .

• So before we create any node we first create a pointer which will hold the address of the first node in the list

(11)

Linked list consists of

1) A sequence of nodes used to hold individual data Elements

2) Each node will have two Sections a) The value to hold

b) The Address of next node in the list

(12)

Head Predecessor

of X Node X

Success-o

r of X

tail

For each node the node that is in front

of it is called predecessor.

The node that is after it is called

(13)

Terminology

Head (front, first node):

The node without predecessor, the node that

starts the lists.

Tail (end, last node):

The node that has no successor, the last node

in the list.

Current node:

The node being processed.

From the current node we can access the next node.

(14)

ADT List Notation

L

- list

e

- item of the same type as the

information part of an element

(a node) in the list

(15)

Operations in ADT Notation

Insert(L,e)

Inserts a node with information e before the

current position

Delete(L)

Deletes the current node in L , the current

position indicates the next node.

RetrieveInfo(L)

e

(16)

A. Insertion

To insert a node X between the

nodes A and B:

.Create a link from X to B.

.Create a link from A to X,

(17)

Insertion

X

(18)

B. Deletion

To delete a node X between A and B:

• Create a link from A to B,

(19)

Deletion

(20)

Keep It Simple

• Let us understand the Linked Structure with the help of this example Following picture depicts linked list holding sequence of numbers (87, 42, 53, 4):

Each node include: • The data element

• The link or address to the next node

87

.

42

.

53

.

4

/

/ means Null ,this is used to specify that this is the last node in the list

(21)

Defining the structure of the linked list

Structure of node in linked list here is implemented as class in C++ Here the Structure of a node will have 2 parts

1) The Data

2) The Pointer to next node ( i.e the address of the next node) Let us understand with an example:

class Node {

public:

int data; // the value to be stored Node* next; // pointer to nest node };

Here the Data is defined as integer and named as data

and the pointer to the next node is declared as a pointer and Named as next

(22)

Starting with the linked Structure

• Linked list is a collection of many individual nodes

• But before we actually create the nodes we should first create the

pointer which will hold the address of the first node in the list so that with this pointer we can actually track the whole list

With the following line of code we will create a pointer variable to point towards the first node in the list

Example: Node* List;

At this point our list is empty- it has no elements

and it is represented by a NULL pointer, i.e. a pointer whose value is 0 (signified by / OR NULL)

/

Lis t

(23)

Starting with the linked Structure

Example: List = new Node;

Here with this one line of code we did two things 1) we created a new first node with the new operator,

2) and we also saved the address of this new node in the pointer variable knows as List ( one can give any name )

Lis t

.

(24)

Starting with the linked Structure

Now we will insert data into this new node with the following code

List->data = 87; Lis t

.

87 This part is List data This part is List next

We have till now successfully created the structure for the node and added the first node and a pointer which will hold the address of the first node

(25)

Implementing a Linked Stack

• If we are implementing a linked stack then the stack does all the operations from the front side so the deletion and insertion is from the front of the list.

• Appending the value 42 into the linked stack :

Initially we have a pointer named List and our first node with 87 value

Lis

t

.

87

/

(26)

• We will be adding a node to the linked Stack

Node * temp = new Node ;

Adding a new node at the front of

the linked list

Lis t

.

87

With the above code we create a new node, and assign its address to the temporary pointer variable temp

.

temp

(27)

Adding a node to the front

Assign value to the newly added node

temp data=43; Lis t

.

87 temp

.

43

/

(28)

Adding a node to the front

Set next data member of new node to point to first node in original list

temp -> next = List;

Lis t

.

87 temp

.

43

/

.

(29)

Adding a node to the front

Now we can Update original List pointer to point to the new first node as the List pointer always points towards first node in the list List = temp; Lis t

.

87 temp

.

43

/

.

(30)

Adding a node to the front

Summarized, as a function:

void add2front( int val, Node* &List) {

Node *temp = new Node; temp -> data = val;

temp -> next = List; List = temp;

(31)

Adding a node at end

• To add a new node at the back we need to reach at the last node and to go to the last node we will assign a new temp pointer which will hop through each node till it reaches the node which is having a null value. It will check each node if it is == Null, until Null is found.

87

.

42

.

53

/

Lis t

.

temp

.

(32)

Adding a node at end

while(temp->next!=NULL)

{

ptr = ptr -> next;

// with this line the temp pointer checks each node for Null value and when it reached the last node it will find the Null value ,

so by the time the control comes out of this while loop the pointer temp will have the address of the last node

}

temp->next= new Node;

// now we are at the last node here insert a node at the last

temp = temp->next; // pointer Points to the inserted node temp->next =NULL; // sets the next of this new node to null temp->data = 90; // put value in the new inserted node

(33)

Node Linking

1.Single linked lists :

Each node contains two links - to the previous

and to the next node

2.Double linked lists :

Each node contains a link only to the next node

3.Circular lists:

(34)
(35)
(36)
(37)

List Implementation

Static

using an array

(38)

Array Implementation

Two

parallel arrays

are used

:

∙Index array

-

the number stored in

the i-th element shows the index of

the "next" node , i.e. node that

follows the i-th node

∙Data array

-

used to store the

(39)

References

Related documents

A doubly linked list is like a linked list, but each node also has a previous field, which points to the nodes predecessor..

ALWAYS use proper riding techniques to avoid vehicle overturns on hills and rough terrain and in turns; avoid paved surfaces - pavement may seriously affect handling and

• Once the linked list has been constructed and head points to the first node of

• A doubly-linked list or multi-list is a data structure with multiple pointers in each node.. • In a doubly-linked list the two pointers create

Globally distributed software development Project Cloud Source code Bug tracking Requirements Management Reporting Testing ….?. IDE IDE SVN Email

Introduction to linear data structures- Arrays, String, representation & Operations, Linked List: Representation of linked list in memory, different implementation of

Import javaio Java program to implement a Singly Linked List public class LinkedList Node head head of list Linked list Node This inner class is.. The list gives access to the

Conflict management Transactional memory Locking order Doubly-linked list... $ % & "