Chap 3 Linked Lists. Vocabulary Linear List 线性表 Linked List 链表 Retrieval 检索 Traversal 遍历 Node 结点 Circularly Linked Lists 循环链表 Doubly Linked Lists 双向链表.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Data Structures Through C
Linked Lists.
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Stacks, Queues, and Linked Lists
Linked Lists.
Data Structures: A Pseudocode Approach with C
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
Data Structures Intro/Linked List Review CIS 237 – Data Structures.
Ceng-112 Data Structures I Chapter 5 Queues.
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
COMP103 - Linked Lists (Part B)1 Chapter 17 Linked List as Objects.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Data Structures Using C++ 2E
Data Structures( 数据结构 ) Course 8: Search Trees. 2 西南财经大学天府学院 Chapter 8 search trees Binary search trees and AVL trees 8-1 Binary search trees Problem:
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Reference: Vinu V Das, Principles of Data Structures using C and C++
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Ceng-112 Data Structures I Figure 9-1 The heap is guaranteed to hold the largest node of the tree in the root. The smaller nodes of a heap can be.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Linked List by Chapter 5 Linked List by
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
Subject Name : Data Structure Using C Title : Linked Lists
CS2006- Data Structures I Chapter 5 Linked Lists III.
Ceng-112 Data Structures ISerap ATAY, Ph. D. 1 Chapter 3 – Part 2 Linear Lists.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Stacks Chapter 3 Objectives Upon completion you will be able to
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
Data Structures( 数据结构 ) Chapter3:Linked List. 2 西南财经大学天府学院 Vocabulary Linear List 线性表 Linked List 链表 Retrieval 检索 Traversal 遍历 Node 结点 Circularly Linked.
Chapter 16: Linked Lists.
Lecture 6 of Computer Science II
Data Structure By Amee Trivedi.
Queues Chapter 4.
Stacks and Queues Chapter 4.
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
Binary Search Trees Chapter 7 Objectives
UNIT-3 LINKED LIST.
Queues Chapter 4.
Data Structures: A Pseudocode Approach with C
DATA STRUCTURE QUEUE.
Arrays and Linked Lists
Data Structures & Algorithms
LINEAR DATA STRUCTURES
Presentation transcript:

Chap 3 Linked Lists

Vocabulary Linear List 线性表 Linked List 链表 Retrieval 检索 Traversal 遍历 Node 结点 Circularly Linked Lists 循环链表 Doubly Linked Lists 双向链表 Multilinked Lists 多重链表

3.1 Linear List Definition: A linear list is a list in which each element has a unique successor. Array is a typical linear list structure. Property: sequential Element 1Element 2Element 3Element 4

Classification Linear Lists General Restricted Unordered Ordered FIFO (queue) LIFO (stack)

Operation Insertion: Depending on the type of general list, an insertion can be made at the beginning of the list, in the middle of the list, or at the end of the list. If data is inserted into ordered lists, the ordering of the list must be maintained. Maintaining the order may require a search algorithm to determine where the data are to be placed data 25 Insertion Inserted Data list

Deletion: Deletion from a general list requires that the list be searched to locate the data being deleted. Any sequential search algorithm can be used to locate the data. When data are deleted from a random array, the data following the deleted item must be shifted to replace the empty element. bluegreen yellow red Deletion blue green redyellow Delete element identified search list

Retrieval: List retrieval requires that data be located in a list and presented to the calling module without changing the contents of the list. dog Retrieval cat goldfishdog zebra list cat goldfishdog zebra Retrieved element identified by search

Traversal: List traversal is a special case of retrieval in which all elements are retrieval in sequence. List traversal requires a looping algorithm rather than a search. Each execution of the loop processes one element in the list. The loop terminates when all elements have been processed.

3.2 Linked list Definition: A linked list is an ordered collection of data in which each element contains the location of the next element; that is, each element contains two parts: data and link. The data part holds the useful information, the data to be processed. The link is used to chain the data together. It contains a pointer that identified the next element in the list. a singly linked list: a linked list contains only one link to a single successor.

The major advantage of the linked list: the data of the linked list can be easily inserted and deleted. Nodes: The elements in a linked list are called nodes. pHead datalinkdatalink datalink A linked list with a head pointer: pHead pHead An empty linked list

Linked List Data Structure Head Node Structure: It usually contains two parts: a pointer and metadata which are data about data in the list. Data Node Structure: The data type for the list depends entirely on the application. A typical data type is like: dataType key field1 field2 … fieldN End dataType dataType key field1 field2 … fieldN End dataType count head Head structure data link Data node structure metadata List count head End List Node data link End Node

3.3 Linked List Algorithms Create List: it receives the head structure and initializes the metadata for the list. The pseudocode algorithms: Algorithm createList(ref list ) Initializes metadata for a linked list. Pre list is metadata structure passed by reference Post metadata initialized 1 list.head = null 2 list.count = 0 3 return End createList Algorithm createList(ref list ) Initializes metadata for a linked list. Pre list is metadata structure passed by reference Post metadata initialized 1 list.head = null 2 list.count = 0 3 return End createList list.head = null list.count = 0 (a) before create ? ? list count head (b) after create 0 list count head

Insert Node: 1.Allocate memory for the new node and insert data. 2.Point the new node to its successor. 3.Point the new node’s predecessor to the new node. We discuss four situation : insert into empty list insert at beginning insert in middle insert at end

pNew->link = list.head Set link to null pointer List.head = pNew Point list to first node pNew->link = list.head Set link to null pointer List.head = pNew Point list to first node 0 count head 75 pNew (a) before add 1 count head 75 pNew (b) after add Insert into empty list:

Insert at beginning: pNew->link = list.head List.head = pNew pNew->link = list.head List.head = pNew 1 39 pNew 75 count head Before add After add 1 39 pNew count head 75 Logically,inserting into an empty list is the same as inserting at the beginning of a list.

Insert in middle: pNew->link = pPre->link pPre->link = pNew pNew->link = pPre->link pPre->link = pNew 2 pNew 39 count head Before add pPre After add 3 52 pNew count head 3975 pPre

Insert at end: pNew->link = pPre->link pPre->link = pNew pNew->link = pPre->link pPre->link = pNew 3 count head pNew 134 pPre Before add After add 3 count head pPre pNew 134

Insert node algorithm: Algorithm insertNode (ref list, val pPre, val dataIn ) 1allocate(pNew) 2if (memory overflow) 1 return false 3end if 4pNew->data=dataIn 5if (pPre null) 1 pNew-.link =list.head 2 list.head = new 6else 1 pNew->link = pPre->link 2 pPre->link = pNew 7end if 8list.count = list.count+1 9Return ture end insertNode Algorithm insertNode (ref list, val pPre, val dataIn ) 1allocate(pNew) 2if (memory overflow) 1 return false 3end if 4pNew->data=dataIn 5if (pPre null) 1 pNew-.link =list.head 2 list.head = new 6else 1 pNew->link = pPre->link 2 pPre->link = pNew 7end if 8list.count = list.count+1 9Return ture end insertNode Insert data into a new node in the linked list Pre List is metadata structure to a valid list pPre is pointer to data ’ s logical predecessor dataIn contains data to be inserted Post data have been inserted in sequence Return true if successful, false is memory overflow

Delete Node: this algorithm logically removes a node from the linked list by: 1. changing various link pointers and then 2. physically deleting the node from dynamic memory. We discuss two situation : d elete first node (delete the only node in the list) g eneral delete case (delete the middle or last node)

Delete first node: list.head = pLoc->link recycle(pLoc) list.head = pLoc->link recycle(pLoc) Before delete pLoc pPre count head After delete pLoc pPre (Recyled) count head

General delete case: pPre->link = pLoc->link recycle(pLoc) pPre->link = pLoc->link recycle(pLoc) pPre 3 Before delete pLoc counthead After delete pPre 3 pLoc counthead (Recycled)

Delete node algorithm: Algorithm insertNode (ref list, val pPre, val pLoc, ref dataOut ) 1Dataout = pLoc->data 2if (pPre null) 1 list.head = pLoc->link 3else 1 pPre->link = pLoc->link 4 end if 5 list.count = list.count recycle(pLoc) 7 Return end deleteNode Algorithm insertNode (ref list, val pPre, val pLoc, ref dataOut ) 1Dataout = pLoc->data 2if (pPre null) 1 list.head = pLoc->link 3else 1 pPre->link = pLoc->link 4 end if 5 list.count = list.count recycle(pLoc) 7 Return end deleteNode Deletes data from a linked list and returns it to calling module. Pre List is metadata structure to a valid list pPre is pointer to predecessor node dataIn is variable to received deleted data Post data have been deleted and returned to caller

Search list: it is used by several algorithms to locate data in a list. When we insert or delete or retrieve data from a list, we need to search the list and find the data. Algorithm searchList (val list ref pPre ref pLoc ref target 1pPre = null 2 pLoc = list.head 3Loop (pLoc not null AND target > pLoc->data.key) 1 pPre =pLoc 2 pLoc = pLoc->link 4end loop 5If (pLoc null) 1 found = false Algorithm searchList (val list ref pPre ref pLoc ref target 1pPre = null 2 pLoc = list.head 3Loop (pLoc not null AND target > pLoc->data.key) 1 pPre =pLoc 2 pLoc = pLoc->link 4end loop 5If (pLoc null) 1 found = false 6 else 1 if (target equal pLoc->data.key) 1 found = true 2 else 1 found = false 3 end if 7end if 8return found end searchList 6 else 1 if (target equal pLoc->data.key) 1 found = true 2 else 1 found = false 3 end if 7end if 8return found end searchList Searches list and passes back address of node containing target and its logical predecessor. Pre List is metadata structure to a valid list pPre is pointer to predecessor node pLoc is pointer variable for current node target is the being sought Post pLoc point to first node with equal /greater key -or- null if targer > key of last node pPre points to largest node smaller than key -or- null if targer < key of first node Return true if found, false if not found

Unordered List Search: The problem with unordered searches is that multiple elements often satisfy the search criteria. One simple solution is to return a list of all elements that satisfy the criteria. Retrieve Node: 1. Use search algorithm to locate the data in the list. 2. If the data are found, move the data to the output area in the calling module and returns true. 3. If the data are not found, return false.

Empty list: algorithm retrieveNode (val list, val key, ref dataOut ) 1found = searchList(List, pPre, pLoc, key) 2If (found) 1 dataOut = pLoc->data 3End if 4Return found End retreiveNode algorithm retrieveNode (val list, val key, ref dataOut ) 1found = searchList(List, pPre, pLoc, key) 2If (found) 1 dataOut = pLoc->data 3End if 4Return found End retreiveNode Algorithm emtpyList (val list ) 1 return (list.count equal to zero) End emptyList Algorithm emtpyList (val list ) 1 return (list.count equal to zero) End emptyList

Full list: List count: Algorithm fullList (val list ) 1Allocate (pNew) 2If (allocation successful) 1 recycle (pNew) 2 return false 3End if 4Return ture End fullList Algorithm fullList (val list ) 1Allocate (pNew) 2If (allocation successful) 1 recycle (pNew) 2 return false 3End if 4Return ture End fullList Algorithm listCount (val list ) 1Return (list.count) End listCount Algorithm listCount (val list ) 1Return (list.count) End listCount

Traverse list: Algorithms of this kind start at the first node and examine each node in succession until the last node has been processed. Traverse list is used in changing a value in each node, printing the list, summing a field in the list and so on. step: 1. Set the walking pointer to the first node in the list. 2. Use a loop calling a process module and passes it the data and then advances the walking pointer to the next node. 3. When the last node is processed, the walking pointer becomes null and the loop terminates. pWalker = list.head Loop (pWalker not null) process(pWalker ->data) pWalker = pWalker ->link End loop pWalker = list.head Loop (pWalker not null) process(pWalker ->data) pWalker = pWalker ->link End loop

A approach in designing the traverse list in this approach, the user controls the loop, calling traverse to get next element in the list moredata = getNext (list, o, dataout) Loop (moredata true) process(dataout) moredata = getNext (list, o, dataout) End loop moredata = getNext (list, o, dataout) Loop (moredata true) process(dataout) moredata = getNext (list, o, dataout) End loop N Count pos head … 95100

Algorithm getNext ( ref list, val fromWhere, ref dataOut ) 1If (fromwhere is 0) 1 if (list.count is zero) 1 success = false 2 Else 1 list.pos = list.head 2 dataOut = list.pos->data 3 success = true 2Else 1 if (list.pos->link null) 1 success = false 2 else 1 list.pos = list.pos->link 2 dataOut = true 3 end if 3End if 4Return success End getNext Algorithm getNext ( ref list, val fromWhere, ref dataOut ) 1If (fromwhere is 0) 1 if (list.count is zero) 1 success = false 2 Else 1 list.pos = list.head 2 dataOut = list.pos->data 3 success = true 2Else 1 if (list.pos->link null) 1 success = false 2 else 1 list.pos = list.pos->link 2 dataOut = true 3 end if 3End if 4Return success End getNext Traverses a linked list, each call return the location of an element in the list Pre List is metadata structure to a valid list fromWhere is 0 start at the first element dataOut is variable to receive data Post dataOut contains data and true returned -or- if end of list returns false Return true if next element located, false if end of list

Destroy list: It deletes any nodes still in the list and recycles their memory, then sets the metadata to a null list condition.. Algorithm destroyList (ref pList ) 1Loop (list.count not zero) 1 dltPtr = list.head 2 list.head = dltPtr->link 3 list.count = list.count – 1 4 recycle (dltPtr) 2End loop 3list.pos = null 4Return End destroyList Algorithm destroyList (ref pList ) 1Loop (list.count not zero) 1 dltPtr = list.head 2 list.head = dltPtr->link 3 list.count = list.count – 1 4 recycle (dltPtr) 2End loop 3list.pos = null 4Return End destroyList Delete all data in list Pre List is metadata structure to a valid list Post A ll data deleted Return

3.4 Processing A Linked list LinkedList createListdestroyList getDataaddNoderemoveNodeprintList menu searchList insertNode searchList deleteNode getNext (+)

Algorithm buildLinkedList 1Print (welcome to exploring linked lists.) 2CrateList (list) 3Loop (option not to quit) 1 option = menu () 2 if (option add) 1 dataIn = getData() 2 addNode (list, dataIn) 3 elseif (option delete) 1 print (Enter key of data to be deleted.) 2 read (deleteKey) 3 removeNode (list, deleteKey) 4 elseif (option print) 1 printList (list) 5 endif 4End loop 5DestroyList (list) 6Print (Exploration complete. Thank you.) End buildLinkedList Algorithm buildLinkedList 1Print (welcome to exploring linked lists.) 2CrateList (list) 3Loop (option not to quit) 1 option = menu () 2 if (option add) 1 dataIn = getData() 2 addNode (list, dataIn) 3 elseif (option delete) 1 print (Enter key of data to be deleted.) 2 read (deleteKey) 3 removeNode (list, deleteKey) 4 elseif (option print) 1 printList (list) 5 endif 4End loop 5DestroyList (list) 6Print (Exploration complete. Thank you.) End buildLinkedList This program builds a linked list that can be modified or printed by the user.

Algorithm menu 1Print ( …… MENU …… ) 2Print (A: Add new data.) 3Print (D: Delete data.) 4Print (P: Print list.) 5 Print (Q: Quit.) 6Valid = false 7Loop ( valid false) 1 print ( Enter your choice: ’’ ) 2 read (choice) 3 if (choice equal ‘ A ’ or ‘ D ’ or ‘ P ’ or ‘ Q ’ ) 1 valid = true 4 else 1 print ( Invalid choice. Choices are ) 5 endif 8End loop 9 Return choice End menu Algorithm menu 1Print ( …… MENU …… ) 2Print (A: Add new data.) 3Print (D: Delete data.) 4Print (P: Print list.) 5 Print (Q: Quit.) 6Valid = false 7Loop ( valid false) 1 print ( Enter your choice: ’’ ) 2 read (choice) 3 if (choice equal ‘ A ’ or ‘ D ’ or ‘ P ’ or ‘ Q ’ ) 1 valid = true 4 else 1 print ( Invalid choice. Choices are ) 5 endif 8End loop 9 Return choice End menu Display a menu and read user option. Pre Nothing Return Valid choice

Algorithm addNode ( ref list, val dataIn ) 1Found = searchList (list, pPre, pLoc, dataIn.key) 2If (found) 1 print (Error: Data already in the list. Not added.) 3Else 1 success = insertNode (list, pPre, dataIn) 2 if (success false) 1 print (Error: Out of memory. Program quitting.) 2 abort algorithm 3 end if 4 Return End addNode Algorithm addNode ( ref list, val dataIn ) 1Found = searchList (list, pPre, pLoc, dataIn.key) 2If (found) 1 print (Error: Data already in the list. Not added.) 3Else 1 success = insertNode (list, pPre, dataIn) 2 if (success false) 1 print (Error: Out of memory. Program quitting.) 2 abort algorithm 3 end if 4 Return End addNode Add data to a linked list Pre List is metadata structure to a valid list dataIn are data to be inserted into list Post D ata have been inserted into list in key sequence

Algorithm removeNode (ref list, val key ) 1Found = searchList (list, pPre, pLoc, key) 2If (found) 1 deleteNode (list, pPre, pLoc, deleteData) 3Else 1 print (Error: Key not in list.) 4End if 5Return End removeNode Algorithm removeNode (ref list, val key ) 1Found = searchList (list, pPre, pLoc, key) 2If (found) 1 deleteNode (list, pPre, pLoc, deleteData) 3Else 1 print (Error: Key not in list.) 4End if 5Return End removeNode This algorithm deletes a node from the linked list Pre List is metadata structure to a valid list key is the key to be located and deleted Post the node has been deleted -or- a warning message printed if not found

Algorithm printList ( val list ) 1If (emptyList (list)) 1 print (No data in list.) 2Else 1 print (**** Begin Data Print ****) 2 count = 0 3 moreData = getNext (list, 0, dataPtr) 4 loop (moreData true) 1 count = count print (count, dataPtr->key) 3 moreData = getNext (list, 1, dataPtr) 5 end loop 3 End if 4Return End printlist Algorithm printList ( val list ) 1If (emptyList (list)) 1 print (No data in list.) 2Else 1 print (**** Begin Data Print ****) 2 count = 0 3 moreData = getNext (list, 0, dataPtr) 4 loop (moreData true) 1 count = count print (count, dataPtr->key) 3 moreData = getNext (list, 1, dataPtr) 5 end loop 3 End if 4Return End printlist This algorithm traverses a linked list and prints the key in each node. Pre List is metadata structure to a valid list Post A ll key have been printed

3.5 List Applications Append Lists: 5 count pos head count pos head count pos head count pos head list1 list2 list1 list2 Before Append After Append

Algorithm appendTwolists 1Pirint (This program creates two lists and then appends them) 2Print (Enter first file name) 3Read (fileName) 4Open (fileName) 5Build (list1, fileName) 6printList (list1) 7Print (Enter secondt file name) 8Read (fileName) 9Open (fileName) 10Build (list2, fileName) 11printList (list2) 12Append (list1, list2) 13printList (list1) 14Return End appendTwoLists Algorithm appendTwolists 1Pirint (This program creates two lists and then appends them) 2Print (Enter first file name) 3Read (fileName) 4Open (fileName) 5Build (list1, fileName) 6printList (list1) 7Print (Enter secondt file name) 8Read (fileName) 9Open (fileName) 10Build (list2, fileName) 11printList (list2) 12Append (list1, list2) 13printList (list1) 14Return End appendTwoLists

Algorithm build ( ref list, val file ) 1CreateList (list) 2Loop (not end of file) 1 read (file into dataIn) 2 searchList (list, pPre, pLoc, dataIn.key) 3 insertNode (list, pPre, dataIn) 3End loop 4Return End build Algorithm build ( ref list, val file ) 1CreateList (list) 2Loop (not end of file) 1 read (file into dataIn) 2 searchList (list, pPre, pLoc, dataIn.key) 3 insertNode (list, pPre, dataIn) 3End loop 4Return End build

Algorithm append ( ref list1, val list2 ) 1If (list1.count zero) 1 list1.head = list2.head 2Else 1 pLoc = list1.head 2 loop(pLoc->link not null) 1 pLoc = pLoc->link 3 end loop 4 pLoc->link = list2.head 3End if 4List1.count = list1.count + list2.count 5Return End append Algorithm append ( ref list1, val list2 ) 1If (list1.count zero) 1 list1.head = list2.head 2Else 1 pLoc = list1.head 2 loop(pLoc->link not null) 1 pLoc = pLoc->link 3 end loop 4 pLoc->link = list2.head 3End if 4List1.count = list1.count + list2.count 5Return End append

Array of lists (count, pos, and head)

Algorithm arrayOf Lists 1print (Begin array of linked lists) 2Print (How many list do you want?) 3Read (numLists) 4buildArys (listArray, numLists) 5printArys (listArray, numLists) 6Print (End of arry of linked lists) End arryOfLists Algorithm arrayOf Lists 1print (Begin array of linked lists) 2Print (How many list do you want?) 3Read (numLists) 4buildArys (listArray, numLists) 5printArys (listArray, numLists) 6Print (End of arry of linked lists) End arryOfLists

Algorithm buildArrays (ref listArray, val numLists ) 1row = 0 2Loop (row < numLists) 1 print (Enter file name) 2 read (fileName) 3 open (fileName) 4 build (listArray[row], fileName) 5 close (fileName) 6 row = row + 1 3End loop 4Return End buildArrays Algorithm buildArrays (ref listArray, val numLists ) 1row = 0 2Loop (row < numLists) 1 print (Enter file name) 2 read (fileName) 3 open (fileName) 4 build (listArray[row], fileName) 5 close (fileName) 6 row = row + 1 3End loop 4Return End buildArrays

Algorithm printArys (val listArray, val numLists ) 1Row = 0 2Loop (row < numLists) 1 printList (listArray[row]) 2 row = row + 1 3End loop 4Return End printAry Algorithm printArys (val listArray, val numLists ) 1Row = 0 2Loop (row < numLists) 1 printList (listArray[row]) 2 row = row + 1 3End loop 4Return End printAry

3.6 Complex Linked List Structures Circularly Linked Lists: In this structure, the last node’s link points to the first node of the list. N count pos rear link

The problem on searching doubly linked list: What is the target does not exit? The solution: if we start at the fitst node, use the rear pointer; if we start at a middle node, we save the starting node’s address, use the address

Doubly Linked: In this structure, each node has a pointer to both its successor and its predecessor. count head rear BF BF BF List Doubly Linked List Insertion: – Follow the basic patter. – Need to connect both the forward and backward pointers. B: Backward pointer F: Forward pointer

pPre 20 pNew Before After Insert into null list or before first node pNew Before After Insert between two nodes

Algorithm insertDbl (ref list, val dataIn ) 1If (full list) 1 return 0 2End if 3Found = searchList (list, Pre, pSucc, dataIn.key) 4If (not found) 1 allocate (pNew) 2 pNew->data = dataIn 3 if (pPre is null) 1 pNew->back = null 2 pNew->fore = list.head 3 list.head = pNew 4 else 1 pNew->fore = pPre->fore 2 pNew->back = pPre 5 end if Algorithm insertDbl (ref list, val dataIn ) 1If (full list) 1 return 0 2End if 3Found = searchList (list, Pre, pSucc, dataIn.key) 4If (not found) 1 allocate (pNew) 2 pNew->data = dataIn 3 if (pPre is null) 1 pNew->back = null 2 pNew->fore = list.head 3 list.head = pNew 4 else 1 pNew->fore = pPre->fore 2 pNew->back = pPre 5 end if 6 if (pPre->fore null) 1 list.rear = pNew 7 else 1 pSucc->back = pNew 8 end if 9 pFore->fore = pNew 10 list.count = list.count return (1) 5End if 6Return (2) End insertDbl 6 if (pPre->fore null) 1 list.rear = pNew 7 else 1 pSucc->back = pNew 8 end if 9 pFore->fore = pNew 10 list.count = list.count return (1) 5End if 6Return (2) End insertDbl

pPred pDltpSucc Before delete After deleting (Recycled) Doubly Linked List deletion:

Algorithm deleteDbl ( ref list, val pDlt ) 1If (pDlt null) 1 abort (Impossible condition in delete double) 2End if 3List.count = list.count + 1 4If (pDlt->backk not null) 1 pPred = pDlt->back 2 pPred->fore = pDlt->fore 5Else 1 list.head = pDlt->fore 6End if 7If (pDlt->fore not null ) 1 pSucc = pDlt->fore 2 pSucc->back = pDlt->back 8Else 1 list.rear = pDlt->back 9End if 10Recycle (pDlt) 11Return End deleteDbl Algorithm deleteDbl ( ref list, val pDlt ) 1If (pDlt null) 1 abort (Impossible condition in delete double) 2End if 3List.count = list.count + 1 4If (pDlt->backk not null) 1 pPred = pDlt->back 2 pPred->fore = pDlt->fore 5Else 1 list.head = pDlt->fore 6End if 7If (pDlt->fore not null ) 1 pSucc = pDlt->fore 2 pSucc->back = pDlt->back 8Else 1 list.rear = pDlt->back 9End if 10Recycle (pDlt) 11Return End deleteDbl

Multilinked Lists: It is a list with two or more logical key sequences. 3 Pres rear Sp rear Spouse link President link Washington1789Cutis Admas,J1779Smith Jefferson1801Skelton

Multilinked List Insert: Build Multilinked getData insert Build Node Insert Pres Insert Spouse Search Pres Search Spouse

Multilinked List Delete: – The major variation for multilinked list delete: need to reconnect the pointers for each logical list. – One solution: use the spouse’s name from the president search and then search the spouse list to find the pointers that need to be updated, but doing so can be very inefficient. – The standard solution: use a doubly linked list for the spouse links.

3.9 Summary A linear list is a list in which each element has a unique successor. Linear list can be divided into tow categories: general and restricted. – In a general list, data can be inserted and deleted anywhere and there are no restrictions on the operations that can be used to process the list. – General lists can be further diveded into random lists and ordered list. – In a random list, there is no ordering of the data.

– In an ordered list, the data dare arranged according to a key,which is one or more fields used to identify the data or control their use. – In a restricted list, data,can be added or deleted at the end of the structure and processing is restricted to the operations on the data at then ends of the list. – Tow common restricted list structures are stacks, (last in-fist out [LIFO] lists) and queues (fist in-first out [FIFO] lists). Four common operations are associated with linear lists: insertion, deletion, retrieval, and traversal. A linked list is an ordered collection of data

in which each element contains the location (address) of the next element; that is, each element contains two parts: data and link. A head node is a data structure that contains metadata about the list, such as a count, a head pointer to the first node, and a rear pointer to the last node. It may contain any other general list data required by the use of the structure. The node in s singly linked list contains only one link to a single successor unless it is the last, in which case it is not linked to any other node. When we want to insert into a linked list contains only one link to a single successor unless it is the

last, in which case it is not linked to any other node. When we want to insert into a linked list, we must consider four cases: adding to the empty list, adding at the beginning, adding to the middle,, and adding at the end. When we want to delete a node from a list, we must consider two case: delete the first node or delete any other node. To search a linked list for an item, we use the ordered list search. Traversing a linked list means going through the list, node by node, and processing each node. Three examples of list traversals are counting the

number of the nodes, printing the contents of nodes, and summing the values of one or more fields. A header node contains the same metadata found in the head structure and shares the pointer structure with the data node. It is physically positioned so that it is always the first node in the linked list. A circularly linked list is a list in which the last node’s link points to the first node of the list. A doubly linked list is a linked list in which each node has pointer to both its successor and its predecessor. A multilinked list is a linked list with two or more logical list.