chapter 16 1 linked structures
Download
Skip this Video
Download Presentation
Chapter 16-1 Linked Structures

Loading in 2 Seconds...

play fullscreen
1 / 37

Chapter 16-1 Linked Structures - PowerPoint PPT Presentation


  • 97 Views
  • Uploaded on

Chapter 16-1 Linked Structures. Dale/Weems. Chapter 16 Topics. Meaning of a Linked List Meaning of a Dynamic Linked List Traversal, Insertion and Deletion of Elements in a Dynamic Linked List Specification of a Dynamic Linked Sorted List

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Chapter 16-1 Linked Structures' - eliana-middleton


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
chapter 16 topics
Chapter 16 Topics
  • Meaning of a Linked List
  • Meaning of a Dynamic Linked List
  • Traversal, Insertion and Deletion of Elements in a Dynamic Linked List
  • Specification of a Dynamic Linked Sorted List
  • Insertion and Deletion of Elements in a Dynamic Linked Sorted List
what is a list
What is a List?
  • A list is a varying-length, linear collection of homogeneous elements
  • Linear means that each list element (except the first) has a unique predecessor and each element (except the last) has a unique successor
to implement the list adt
To implement the List ADT

The programmer must

1) choose a concrete data representation for the list, and

2) implement the list operations

recall 4 basic kinds of adt operations
Recall:4 Basic Kinds of ADT Operations
  • Constructors -- create a new instance (object) of an ADT
  • Transformers -- change the state of one or more of the data values of an instance
  • Observers-- allow client to observe the state of one or more of the data values of an instance without changing them
  • Iterators -- allow client to access the data values in sequence
list operations
List Operations

Transformers

  • Insert
  • Delete
  • Sort

Observers

  • IsEmpty
  • IsFull
  • Length
  • IsPresent

change state

observe state

6

adt list operations
ADT List Operations

Iterator

    • Reset
    • GetNextItem
  • Reset prepares for the iteration
  • GetNextItem returns the next item in sequence
  • No transformer can be called between calls to GetNextItem (Why?)

Iteration Pair

slide8
Array-based class List

SelSort

IsEmpty

Private data:

length

data [0]

[1]

[2]

[MAX_LENGTH-1]

currentPos

IsFull

Length

Insert

Delete

IsPresent

Reset

GetNexItem

slide9
// Specification file array-based list (“list.h”)

const int MAX_LENGTH = 50;

typedef int ItemType;

class List// Declares a class data type

{

public: // Public member functions

List(); // constructor

bool IsEmpty () const;

bool IsFull () const;

int Length () const; // Returns length of list

void Insert (ItemType item);

void Delete (ItemType item);

bool IsPresent(ItemType item) const;

void SelSort ();

void Reset ();

ItemType GetNextItem ();

private: // Private data members

int length; // Number of values currently stored

ItemType data[MAX_LENGTH];

int CurrentPos; // Used in iteration

};

9

implementation structures
Implementation Structures
  • Use a built-in array stored in contiguous memory locations, implementing operations Insert and Delete by moving list items around in the array, as needed
  • Use a linked list in which items are not necessarily stored in contiguous memory locations
  • A linked list avoids excessive data movement from insertions and deletions
implementation possibilities for a list adt
Implementation Possibilities for a List ADT

List

Linked list

Built-in array

Built-in

dynamic data

and pointers

Built-in array

of structs

array representation of a linked list
Array Representation of a Linked List

component

link

Node[0]

head

Node[1]

Node[2]

Node[3]

Node[4]

Node[5]

Node[6]

Node[7]

data structure of array based linked list
Data Structure of Array Based Linked List

struct NodeType

{

int component;

int link;

};

NodeType node[1000]; // Max. 1000 nodes

int head;

insert a new node into a linked list
Insert a New Node into a Linked List

component

link

Node[0]

head

Insert 25,

Setting link to 7

Node[1]

Node[2]

Node[3]

Node[4]

Change link

from 7 to 1

Node[5]

Node[6]

Node[7]

delete a node from a linked list
Delete a Node from a Linked List

component

link

Node[0]

head

Node[1]

Node[2]

Node[3]

Deleted

Node[4]

Node[5]

Node[6]

Change the link

From 4 to 0

Node[7]

a linked list
‘X’ ‘C’ ‘L’

head

A Linked List
  • A linked list is a list in which the order of the components is determined by an explicit link member in each node
  • Each node is a struct containing a data member and a link member that gives the location of the next node in the list
dynamic linked list
head

“Ted” “Irv” “Lee”

Dynamic Linked List
  • A dynamic linked list is one in which the nodes are linked together by pointers and an external pointer (or head pointer) points to the first node in the list
nodes can be located anywhere in memory
3000 5000 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

Nodes can be located anywhere in memory
  • The link member holds the memory address of the next node in the list

head

declarations for a dynamic linked list
‘A’6000

.component .link

Declarations for a Dynamic Linked List

// Type declarations

struct NodeType

{

char component;

NodeType* link;

}

typedef NodeType* NodePtr;

// Variable DECLARATIONS

NodePtr head;

NodePtr ptr;

19

pointer dereferencing and member selection
ptr

.Component .link

ptr

‘A’ 6000

.component .link

*ptr

ptr

‘A’ 6000

.component .link

(*ptr).component

ptr->component

Pointer Dereferencing and Member Selection

‘A’ 6000

ptr

20

ptr is a pointer to a node
ptr

.component .link

ptr is a pointer to a node

‘A’ 6000

ptr

21

ptr is the entire node pointed to by ptr
ptr*ptr is the entire node pointed to by ptr

‘A’ 6000

.component .link

*ptr

22

ptr component is a node member
ptr

.component .link

ptr->componentis a node member

‘A’ 6000

ptr->component

(*ptr).component // Equivalent

23

ptr link is a node member
ptr

.component .link

ptr->linkis a node member

‘A’ 6000

ptr->link

(*ptr).link // Equivalent

24

traversing a dynamic linked list
ptr

3000 5000 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

head

Traversing a Dynamic Linked List

// Pre: head points to a dynamic linked list

ptr = head;

while (ptr != NULL)

{

cout << ptr->component

// Or, do something else with node *ptr

ptr = ptr->link;

}

traversing a dynamic linked list1
ptr 3000

3000 5000 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

head

Traversing a Dynamic Linked List

// Pre: head points to a dynamic linked list

ptr = head;

while (ptr != NULL)

{

cout << ptr->component;

// Or, do something else with node *ptr

ptr = ptr->link;

}

traversing a dynamic linked list2
ptr 3000

3000 5000 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

head

Traversing a Dynamic Linked List

// Pre: head points to a dynamic linked list

ptr = head;

while (ptr != NULL)

{

cout << ptr->component;

// Or, do something else with node *ptr

ptr = ptr->link;

}

traversing a dynamic linked list3
ptr 3000

3000 5000 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

head

Traversing a Dynamic Linked List

// Pre: head points to a dynamic linked list

ptr = head;

while (ptr != NULL)

{

cout << ptr->component;

// Or, do something else with node *ptr

ptr = ptr->link;

}

traversing a dynamic linked list4
ptr 5000

3000 5000 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

head

Traversing a Dynamic Linked List

// Pre: head points to a dynamic linked list

ptr = head;

while (ptr != NULL)

{

cout << ptr->component;

// Or, do something else with node *ptr

ptr = ptr->link;

}

traversing a dynamic linked list5
ptr 5000

3000 5000 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

head

Traversing a Dynamic Linked List

// Pre: head points to a dynamic linked list

ptr = head;

while (ptr != NULL)

{

cout << ptr->component;

// Or, do something else with node *ptr

ptr = ptr->link;

}

traversing a dynamic linked list6
ptr 5000

3000 5000 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

head

Traversing a Dynamic Linked List

// Pre: head points to a dynamic linked list

ptr = head;

while (ptr != NULL)

{

cout << ptr->component;

// Or, do something else with node *ptr

ptr = ptr->link;

}

traversing a dynamic linked list7
ptr 2000

3000 5000 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

head

Traversing a Dynamic Linked List

// Pre: head points to a dynamic linked list

ptr = head;

while (ptr != NULL)

{

cout << ptr->component;

// Or, do something else with node *ptr

ptr = ptr->link;

}

traversing a dynamic linked list8
ptr 2000

3000 5000 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

head

Traversing a Dynamic Linked List

// Pre: head points to a dynamic linked list

ptr = head;

while (ptr != NULL)

{

cout << ptr->component;

// Or, do something else with node *ptr

ptr = ptr->link;

}

traversing a dynamic linked list9
ptr 2000

3000 5000 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

head

Traversing a Dynamic Linked List

// Pre: head points to a dynamic linked list

ptr = head;

while (ptr != NULL)

{

cout << ptr->component;

// Or, do something else with node *ptr

ptr = ptr->link;

}

traversing a dynamic linked list10
ptr NULL

3000 5000 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

head

Traversing a Dynamic Linked List

// Pre: head points to a dynamic linked list

ptr = head;

while (ptr != NULL)

{

cout << ptr->component;

// Or, do something else with node *ptr

ptr = ptr->link;

}

traversing a dynamic linked list11
ptr NULL

3000 5000 2000

3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

head

Traversing a Dynamic Linked List

// Pre: head points to a dynamic linked list

ptr = head;

while (ptr != NULL)

{

cout << ptr->component;

// Or, do something else with node *ptr

ptr = ptr->link;

}

ad