Linked Lists Mohammed Almashat CS 561 26/03/2006.

42
Linked Lists Mohammed Almashat CS 561 26/03/2006

Transcript of Linked Lists Mohammed Almashat CS 561 26/03/2006.

Page 1: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Linked Lists

Mohammed Almashat

CS 561

26/03/2006

Page 2: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Outline

• Introduction

• Insertion Description

• Deletion Description

• Basic Node Implementation

• Conclusion

Page 3: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Outline

• Introduction

• Insertion Description

• Deletion Description

• Basic Node Implementation

• Conclusion

Page 4: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Introduction

• Definitions– Lists and arrays– Nodes and pointers– Single Linked Lists– Double Linked Lists– Circular Lists

• Advantages

Page 5: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Introduction

• Definitions– Lists and arrays– Nodes and pointers– Single Linked Lists– Double Linked Lists– Circular Lists

• Advantages

Page 6: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Lists and arrays

• Lists:

Page 7: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Lists and arrays

• Arrays: {Size of the following array is = 4}

Index 0 1 2 3

Value 44 5 96 3

Page 8: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Introduction

• Definitions– Lists and arrays– Nodes and pointers– Single Linked Lists– Double Linked Lists– Circular Lists

• Advantages

Page 9: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Nodes and pointers

Page 10: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Introduction

• Definitions– Lists and arrays– Nodes and pointers– Single Linked Lists– Double Linked Lists– Circular Lists

• Advantages

Page 11: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Single linked lists

Page 12: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Introduction

• Definitions– Lists and arrays– Nodes and pointers– Single Linked Lists– Double Linked Lists– Circular Lists

• Advantages

Page 13: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Double Linked Lists

Page 14: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Introduction

• Definitions– Lists and arrays– Nodes and pointers– Single Linked Lists– Double Linked Lists– Circular Lists

• Advantages

Page 15: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Circular Lists

Page 16: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Introduction

• Definitions– Lists and arrays– Nodes and pointers– Single Linked Lists– Double Linked Lists– Circular Lists

• Advantages

Page 17: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Advantages

The Linked List advantages are collected because of the array disadvantages, array disadvantages are:

1. Array Size

2. Memory allocation

3. Insertion and Deletion

Page 18: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Outline

• Introduction

• Insertion Description

• Deletion Description

• Basic Node Implementation

• Conclusion

Page 19: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Insertion Description

• Insertion at the top of the list

• Insertion at the end of the list

• Insertion in the middle of the list

Page 20: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Insertion Description

• Insertion at the top of the list

• Insertion at the end of the list

• Insertion in the middle of the list

Page 21: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Insertion at the top

Steps:

• Create a Node

• Set the node data Values

• Connect the pointers

Page 22: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Insertion Description

• Follow the previous steps and we get

48 17 142head //

head 93

Step 1 Step 2

Step 3

Page 23: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Insertion Description

• Insertion at the top of the list

• Insertion at the end of the list

• Insertion in the middle of the list

Page 24: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Insertion at the end

Steps:

• Create a Node

• Set the node data Values

• Connect the pointers

Page 25: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Insertion Description

• Follow the previous steps and we get

48 17 142head //

Step 1 Step 2

Step 3

Page 26: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Insertion Description

• Insertion at the top of the list

• Insertion at the end of the list

• Insertion in the middle of the list

Page 27: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Insertion in the middle

Steps:

• Create a Node

• Set the node data Values

• Break pointer connection

• Re-connect the pointers

Page 28: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Insertion Description

Step 1 Step 2

Step 3

Step 4

Page 29: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Outline

• Introduction

• Insertion Description

• Deletion Description

• Basic Node Implementation

• Conclusion

Page 30: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Deletion Description

• Deleting from the top of the list

• Deleting from the end of the list

• Deleting from the middle of the list

Page 31: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Deletion Description

• Deleting from the top of the list

• Deleting from the end of the list

• Deleting from the middle of the list

Page 32: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Deleting from the top

Steps

• Break the pointer connection

• Re-connect the nodes

• Delete the node

Page 33: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Deletion Description

4 17

head

426

4 17

head

426

4 17

head

42

Page 34: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Deletion Description

• Deleting from the top of the list

• Deleting from the end of the list

• Deleting from the middle of the list

Page 35: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Deleting from the end

Steps

• Break the pointer connection

• Set previous node pointer to NULL

• Delete the node

Page 36: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Deletion Description

4 17

head

426

4 17

head

426

4 176

head

Page 37: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Deletion Description

• Deleting from the top of the list

• Deleting from the end of the list

• Deleting from the middle of the list

Page 38: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Deleting from the Middle

Steps

• Set previous Node pointer to next node

• Break Node pointer connection

• Delete the node

Page 39: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Deletion Description

4 17 42

head

4 17

head

42

4

head

42

Page 40: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Outline

• Introduction

• Insertion Description

• Deletion Description

• Basic Node Implementation

• Conclusion

Page 41: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Basic Node Implementation

The following code is written in C++:

Struct Node

{int data; //any type of data could be another struct

Node *next; //this is an important piece of code “pointer”

};

Page 42: Linked Lists Mohammed Almashat CS 561 26/03/2006.

Outline

• Introduction

• Insertion Description

• Deletion Description

• Basic Node Implementation

• Conclusion