linked lists.ppt

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

Upload: sarbagya

Post on 11-Feb-2016

222 views

Category:

Documents


0 download

DESCRIPTION

linke list

TRANSCRIPT

Page 1: Linked Lists.ppt

Linked Lists

Mohammed AlmashatCS 561

26/03/2006

Page 2: Linked Lists.ppt

Outline

• Introduction• Insertion Description• Deletion Description• Basic Node Implementation• Conclusion

Page 3: Linked Lists.ppt

Outline

• Introduction• Insertion Description• Deletion Description• Basic Node Implementation• Conclusion

Page 4: Linked Lists.ppt

Introduction

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

• Advantages

Page 5: Linked Lists.ppt

Introduction

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

• Advantages

Page 6: Linked Lists.ppt

Lists and arrays

• Lists:

Page 7: Linked Lists.ppt

Lists and arrays

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

Index 0 1 2 3Value 44 5 96 3

Page 8: Linked Lists.ppt

Introduction

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

• Advantages

Page 9: Linked Lists.ppt

Nodes and pointers

Page 10: Linked Lists.ppt

Introduction

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

• Advantages

Page 11: Linked Lists.ppt

Single linked lists

Page 12: Linked Lists.ppt

Introduction

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

• Advantages

Page 13: Linked Lists.ppt

Double Linked Lists

Page 14: Linked Lists.ppt

Introduction

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

• Advantages

Page 15: Linked Lists.ppt

Circular Lists

Page 16: Linked Lists.ppt

Introduction

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

• Advantages

Page 17: Linked Lists.ppt

AdvantagesThe Linked List advantages are collected because of the

array disadvantages, array disadvantages are:1. Array Size2. Memory allocation3. Insertion and Deletion

Page 18: Linked Lists.ppt

Outline

• Introduction• Insertion Description• Deletion Description• Basic Node Implementation• Conclusion

Page 19: Linked Lists.ppt

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.ppt

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.ppt

Insertion at the top

Steps:• Create a Node• Set the node data Values• Connect the pointers

Page 22: Linked Lists.ppt

Insertion Description

• Follow the previous steps and we get

48 17 142head //

head 93

Step 1 Step 2

Step 3

Page 23: Linked Lists.ppt

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.ppt

Insertion at the end

Steps:• Create a Node• Set the node data Values• Connect the pointers

Page 25: Linked Lists.ppt

Insertion Description

• Follow the previous steps and we get

48 17 142head //

Step 1 Step 2

Step 3

Page 26: Linked Lists.ppt

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.ppt

Insertion in the middle

Steps:• Create a Node• Set the node data Values• Break pointer connection• Re-connect the pointers

Page 28: Linked Lists.ppt

Insertion Description

Step 1 Step 2

Step 3

Step 4

Page 29: Linked Lists.ppt

Outline

• Introduction• Insertion Description• Deletion Description• Basic Node Implementation• Conclusion

Page 30: Linked Lists.ppt

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.ppt

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.ppt

Deleting from the top

Steps• Break the pointer connection• Re-connect the nodes• Delete the node

Page 33: Linked Lists.ppt

Deletion Description

4 17

head

426

4 17

head

426

4 17

head

42

Page 34: Linked Lists.ppt

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.ppt

Deleting from the end

Steps• Break the pointer connection• Set previous node pointer to NULL• Delete the node

Page 36: Linked Lists.ppt

Deletion Description

4 17

head

426

4 17

head

426

4 176

head

Page 37: Linked Lists.ppt

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.ppt

Deleting from the Middle

Steps• Set previous Node pointer to next node• Break Node pointer connection• Delete the node

Page 39: Linked Lists.ppt

Deletion Description

4 17 42

head

4 17

head

42

4

head

42

Page 40: Linked Lists.ppt

Outline

• Introduction• Insertion Description• Deletion Description• Basic Node Implementation• Conclusion

Page 41: Linked Lists.ppt

Basic Node ImplementationThe 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.ppt

Outline

• Introduction• Insertion Description• Deletion Description• Basic Node Implementation• Conclusion