Download presentation

Presentation is loading. Please wait.

Published byGlen Hayles Modified over 2 years ago

1
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008

2
Outline linked lists implementation An Array-Based Implementation of Linked Lists

3
List Implantation by Linked list In any structure used to store the elements of a list, it must be possible to perform at least the following operation: 1. Locate the First element 2. Given the location of any list element, find its successor 3. Locate the end of the list

4
Linked List Linked list nodes contain Data part – stores an element of the list Next part – stores link/pointer to next element (when no next element, null value)

5
Design the list class Should contain at least the following function members Constructor empty() insert() delete() display()

6
Construction To construct an empty list, we simply make first a null link to indicate that it does not refer to any node: first = null_value ;

7
Empty We can then perform the Empty operation--- determining whether a list is empty, simply by checking whether first is null: first == null_value ?

8
Traverse We begin by initializing an auxiliary variable ptr to point to the first node: Initialize a variable ptr to point to first node Process data where ptr points

9
Traverse Traverse (ctd) set ptr = ptr->next, process ptr->data Continue until ptr == null

10
Insertion To insert a new data value into a linked list, we must first obtain a new node and store the value in its data part The second step is to connect this new node to existing list Two cases in this situation: (1) insertion after some element in the list and (2) insertion at the beginning of the list

11
Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr predptr

12
Insertion Note: insertion also works at end of list pointer member of new node set to null Insertion at the beginning of the list predptr must be set to first pointer member of newptr set to that value (Where first points to) first set to value of newptr In all cases, no shifting of list elements is required !

13
Deletion For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list

14
Deletion Delete node containing 22 from list. Suppose ptr points to the node to be deleted predptr points to its predecessor (the 17) Do a bypass operation : Set the next pointer in the predecessor to point to the successor of the node to be deleted Deallocate the node being deleted. predptr ptr To free space

15
Deletion The second case is easier Just set the first points to the second node in the list and then returning the deleted node to the storage pool

16
Linked Lists - Advantages Access any item as long as external link to first item maintained Insert new item without shifting Delete existing item without shifting Can expand/contract as necessary

17
Linked Lists - Disadvantages No longer have direct access to each element of the list Many sorting algorithms need direct access Binary search needs direct access Access of n th item now less efficient must go through first element, and then second, and then third, etc.

18
Outline linked lists implementation An Array-Based Implementation of Linked Lists

19
Pointer Based Node Since each node has two different parts, a data part and a next part, it is nature to have a node class with two data members. class Node { ElementType data; Node *next; }

20
Pointer Based Node To declare a pointer to a node: Node *ptr To allocate a new node pointer to by ptr: ptr = new Node; ptr = new Node(data_nalue); ptr = new Node(data_value, link_value); To access the data nad next part of the node pointed to by ptr: ptr -> data ptr -> next

21
Array-Based Implementation of Linked Lists Given a list with names Implementation would look like this

22
How to do it??? First of all, define a 2D array int array[10][2];

23
How to do it??? Constructor( ) we make “first” variable equals to 7 to indicate we start with position 7 first ==7; How to choose a start point??

24
How to do it??? Put 88 into data position and set next position to NULL array[first][0]=88; array[first][1]=-1; size==1;

25
How to do Insertion??? Find a empty position (in this example it’s node 1) Insert(array, 1) { ptr==1; // find a empty position pre_ptr==7; // you need a function to find pre_ptr array[new_node][0]==54; array[new_node][1]==array[pre_ptr][1]; array[pre_ptr][1]==ptr; size++; }

26
Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr predptr

27
How to do Deletion???

28
Implementation Details For Insertion, there are also two cases to consider: insertion after some element in the list and insertion at the beginning of the list For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list

Similar presentations

Presentation is loading. Please wait....

OK

DATA STRUCTURES USING C++ Chapter 5

DATA STRUCTURES USING C++ Chapter 5

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on self development preschool Ppt on diode circuits application Ppt on business entrepreneurship and management Ppt on biopotential signals Ppt on review writing examples Ppt on major physical features of india Ppt on varactor diodes Download best ppt on global warming Free download ppt on relationship between science technology and engineering Ppt on teachers day wishes