Top Banner
Linked Lists ENGR. SHAFIYA QADEER MEMON
42

Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

May 20, 2019

Download

Documents

doquynh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Linked Lists

ENGR. SHAFIYA QADEER MEMON

Page 2: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Outline

• Introduction

• Insertion Description

• Deletion Description

• Basic Node Implementation

• Conclusion

Page 3: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Outline

• Introduction

• Insertion Description

• Deletion Description

• Basic Node Implementation

• Conclusion

Page 4: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Introduction

• Definitions

– Lists and arrays

– Nodes and pointers

– Single Linked Lists

– Double Linked Lists

– Circular Lists

• Advantages

Page 5: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Introduction

• Definitions

– Lists and arrays

– Nodes and pointers

– Single Linked Lists

– Double Linked Lists

– Circular Lists

• Advantages

Page 6: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Lists and arrays

• Lists:

Page 7: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

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 - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Introduction

• Definitions

– Lists and arrays

– Nodes and pointers

– Single Linked Lists

– Double Linked Lists

– Circular Lists

• Advantages

Page 9: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Nodes and pointers

Page 10: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Introduction

• Definitions

– Lists and arrays

– Nodes and pointers

– Single Linked Lists

– Double Linked Lists

– Circular Lists

• Advantages

Page 11: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Single linked lists

Page 12: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Introduction

• Definitions

– Lists and arrays

– Nodes and pointers

– Single Linked Lists

– Double Linked Lists

– Circular Lists

• Advantages

Page 13: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Double Linked Lists

Page 14: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Introduction

• Definitions

– Lists and arrays

– Nodes and pointers

– Single Linked Lists

– Double Linked Lists

– Circular Lists

• Advantages

Page 15: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Circular Lists

Page 16: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Introduction

• Definitions

– Lists and arrays

– Nodes and pointers

– Single Linked Lists

– Double Linked Lists

– Circular Lists

• Advantages

Page 17: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

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 - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Outline

• Introduction

• Insertion Description

• Deletion Description

• Basic Node Implementation

• Conclusion

Page 19: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

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 - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

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 - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Insertion at the top

Steps:

• Create a Node

• Set the node data Values

• Connect the pointers

Page 22: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Insertion Description

• Follow the previous steps and we get

48 17 142head //

head 93

Step 1 Step 2

Step 3

Page 23: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

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 - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Insertion at the end

Steps:

• Create a Node

• Set the node data Values

• Connect the pointers

Page 25: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Insertion Description

• Follow the previous steps and we get

48 17 142head //

Step 1 Step 2

Step 3

Page 26: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

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 - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Insertion in the middle

Steps:

• Create a Node

• Set the node data Values

• Break pointer connection

• Re-connect the pointers

Page 28: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Insertion Description

Step 1 Step 2

Step 3

Step 4

Page 29: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Outline

• Introduction

• Insertion Description

• Deletion Description

• Basic Node Implementation

• Conclusion

Page 30: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

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 - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

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 - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Deleting from the top

Steps

• Break the pointer connection

• Re-connect the nodes

• Delete the node

Page 33: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Deletion Description

4 17

head

426

4 17

head

426

4 17

head

42

Page 34: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

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 - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Deleting from the end

Steps

• Break the pointer connection

• Set previous node pointer to NULL

• Delete the node

Page 36: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Deletion Description

4 17

head

426

4 17

head

426

4 176

head

Page 37: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

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 - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Deleting from the Middle

Steps

• Set previous Node pointer to next node

• Break Node pointer connection

• Delete the node

Page 39: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Deletion Description

4 17 42

head

4 17

head

42

4

head

42

Page 40: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Outline

• Introduction

• Insertion Description

• Deletion Description

• Basic Node Implementation

• Conclusion

Page 41: Linked Lists - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

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 - mishayy.files.wordpress.com fileIntroduction • Definitions –Lists and arrays –Nodes and pointers –Single Linked Lists –Double Linked Lists –Circular Lists

Outline

• Introduction

• Insertion Description

• Deletion Description

• Basic Node Implementation

• Conclusion