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

## Presentation on theme: "Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008."— Presentation transcript:

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

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

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)

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

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 ;

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

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

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

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

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

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 !

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

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

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

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

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.

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; }

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

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

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

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??

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;

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++; }

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

How to do Deletion???

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

Download ppt "Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008."

Similar presentations