Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures: A Pseudocode Approach with C

Similar presentations


Presentation on theme: "Data Structures: A Pseudocode Approach with C"— Presentation transcript:

1 Data Structures: A Pseudocode Approach with C
Chapter 5 List Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear list Implement a linear list using a linked list structure Understand the operation of the linear list ADT Write application programs using the linear list ADT Design and implement different link-list structures Data Structures: A Pseudocode Approach with C

2 Data Structures: A Pseudocode Approach with C
5-1 Basic Operations We begin with a discussion of the basic list operations. Each operation is developed using before and after figures to show the changes. Insertion Deletion Retrieval Traversal Data Structures: A Pseudocode Approach with C

3 Data Structures: A Pseudocode Approach with C
Figure 5-1: A linear list Data Structures: A Pseudocode Approach with C

4 Data Structures: A Pseudocode Approach with C
Linear List Concepts General Lists Data can be inserted and deleted anywhere No restrictions on the operations that can be used to process the list Random Lists or Ordered Lists Data Structures: A Pseudocode Approach with C

5 Linear List Concepts (2)
Random Lists: No ordering of the data Ordered Lists: Data are arranged according to a key A key is one or more fields within a structure that are used to identify the data or otherwise control its use Data Structures: A Pseudocode Approach with C

6 Linear List Concepts (3)
2. Restricted Lists Data can only be added or deleted at the ends of the structure and processing is restricted to operations on the data at the ends of the list LIFO (Last-In, First-Out) : Stack FIFO (First-In, First-Out) : Queue Data Structures: A Pseudocode Approach with C

7 Data Structures: A Pseudocode Approach with C
Figure 5-2: Types of lists Data Structures: A Pseudocode Approach with C

8 Data Structures: A Pseudocode Approach with C
Insertion General lists: begining of the list middle of the list the end of the list Random lists (unordered): no restrictions generally, insert data at the end of the list sometime called chronological lists Ordered lists: when the data is inserted, the ordering of the list is maintained Data Structures: A Pseudocode Approach with C

9 Data Structures: A Pseudocode Approach with C

10 Data Structures: A Pseudocode Approach with C
Deletion General lists: the list is searched to locate the data being deleted any sequential search algorithm can be used to locate the data the data are removed from the list Restricted lists: vary depending on which list is being used discuss in chapters 4 (Stack) and 5 (Queue) Data Structures: A Pseudocode Approach with C

11 Data Structures: A Pseudocode Approach with C

12 Data Structures: A Pseudocode Approach with C
Retrieval Requires that data be located in a list Presented to the calling module without changing the contents of the list Any sequential search algorithm can be used to located the data to be retrieved from a general list Data Structures: A Pseudocode Approach with C

13 Data Structures: A Pseudocode Approach with C

14 Data Structures: A Pseudocode Approach with C
Traversal A special case of retrieval in which all elements are retrieved in sequence require a looping algorithm rather that a search each execution of the loop processes one element in the list the loop terminates when all elements have processed Data Structures: A Pseudocode Approach with C

15 Data Structures: A Pseudocode Approach with C
Linked lists An ordered collection of data in which each element contains the location on the next element each element contained: data & link data = useful information link = chain the data together Data Structures: A Pseudocode Approach with C

16 Data Structures: A Pseudocode Approach with C
Linked lists (2) Singly linked list: contains only one link to a single successor advantage of the linked list: data are easily inserted & deleted not necessary to shift elements of a linked list to make room for a new element or to delete an element an empty linked list = a single pointer containing a null pointer Data Structures: A Pseudocode Approach with C

17 Data Structures: A Pseudocode Approach with C

18 Data Structures: A Pseudocode Approach with C
Nodes The elements in a linked list a node’s structure has at least 2 fields: data pointer to the address of the next node in the sequence Data Structures: A Pseudocode Approach with C

19 Data Structures: A Pseudocode Approach with C
Figure 5-6: A linked list Data Structures: A Pseudocode Approach with C

20 Data Structures: A Pseudocode Approach with C
Figure 5-7: Nodes Data Structures: A Pseudocode Approach with C

21 Linked List Data Structure
There is not a physical relationship between the nodes they are not stored contiguously without a physical relationship between the nodes, we need pointers to distinguish the beginning of the list Data Structures: A Pseudocode Approach with C

22 Linked List Data Structure
head node: head pointer the pointer to the beginning of the list metadata: a node contains data about data in the list Data nodes : data name data type Data Structures: A Pseudocode Approach with C

23 Data Structures: A Pseudocode Approach with C

24 Linked List Algorithms
Define 10 operations for a linked list create list insert node delete node search list retrieve node empty list full list list count traverse list destroy list Data Structures: A Pseudocode Approach with C

25 Data Structures: A Pseudocode Approach with C

26 Data Structures: A Pseudocode Approach with C

27 Data Structures: A Pseudocode Approach with C

28 Data Structures: A Pseudocode Approach with C

29 Data Structures: A Pseudocode Approach with C

30 Data Structures: A Pseudocode Approach with C

31 Data Structures: A Pseudocode Approach with C

32 Data Structures: A Pseudocode Approach with C

33 Data Structures: A Pseudocode Approach with C

34 Data Structures: A Pseudocode Approach with C

35 Data Structures: A Pseudocode Approach with C

36 Data Structures: A Pseudocode Approach with C

37 Data Structures: A Pseudocode Approach with C

38 Data Structures: A Pseudocode Approach with C

39 Data Structures: A Pseudocode Approach with C

40 Data Structures: A Pseudocode Approach with C

41 Data Structures: A Pseudocode Approach with C

42 Data Structures: A Pseudocode Approach with C

43 Data Structures: A Pseudocode Approach with C

44 Data Structures: A Pseudocode Approach with C

45 Data Structures: A Pseudocode Approach with C

46 Data Structures: A Pseudocode Approach with C

47 Data Structures: A Pseudocode Approach with C

48 Data Structures: A Pseudocode Approach with C

49 Data Structures: A Pseudocode Approach with C
Figure 5-18: Design for build linked list Data Structures: A Pseudocode Approach with C

50 Algorithm : Build a linked list
algorithm buildLinkedList This program builds a linked list that can be modified or printed by the user. 1 print (Welcome to exploring linked lists) 2 pList = createList 3 loop (option not quit) 1 option = menu () 2 if (option add) 1 addNode (pList, dataIn) 3 elseif (option delete) 1 print (Enter key of data to be deleted.) 2 read (deleteKey) 3 removeNode (pList, deleteKey) 4 else 1 printList (pList) 5 destroyList (pList) 6 print (Exploration complete. Thank you) end buildLinkedList Data Structures: A Pseudocode Approach with C

51 Algorithm: Menu for build a linked list
Display a menu and read user option. Pre Nothing. Return Valid choice. 1 print (……MENU……) 2 print (A: Add new data. ) 3 print (D: Delete data. ) 4 print (P: Print list. ) 5 print (Q: Quit ) 6 valid = false 7 loop (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 <A, D, P, Q>) 8 return choice end menu Data Structures: A Pseudocode Approach with C

52 Data Structures: A Pseudocode Approach with C
Algorithm : Add node to linked list algorithm addNode (val pList <head pointer>, (val dataIn <dataType>) Add data to a linked list. Pre pList is a pointer to the head of the list dataIn are data to be inserted into list Post data have been inserted into list in key sequence 1 Found = searchList (pList, pPre, pLoc, dataIn.key) 2 if (found) Error: Key already exists 1 print (Error: Data already in list. Not added.) 3 else 1 success = insertNode (pList, pPre, dataIn) 4 if (success false) 1 print (Error: Out of memory. Program quitting) 2 abort algorithm 5 return end addNode Data Structures: A Pseudocode Approach with C

53 Data Structures: A Pseudocode Approach with C
Algorithm : Remove data from linked list algorithm removeNode (val pList <head pointer>, val key <keyType>) This algorithm deletes a node from the linked list. Pre List is a pointer to the head structure of the list key is the key to be locate and deleted Post the node has been deleted -or- a warning message printed if not found found = searchList (pList, pPre, pLoc, key) if (found) deletedNode (pList, pPre, pLoc,deletedata) else print (Error : Key not in list.) Return end removeNode Data Structures: A Pseudocode Approach with C

54 Data Structures: A Pseudocode Approach with C
5-2 Implementation We begin with a discussion of the data structure required to support a list. We then develop the 10 basic algorithms required to build and use a list. In developing the insertion and deletion algorithms, we use extensive examples to demonstrate the analytical steps used in developing algorithms. Data Structure Algorithms Data Structures: A Pseudocode Approach with C

55 Data Structures: A Pseudocode Approach with C

56 Data Structures: A Pseudocode Approach with C

57 Data Structures: A Pseudocode Approach with C

58 Data Structures: A Pseudocode Approach with C

59 Data Structures: A Pseudocode Approach with C

60 Data Structures: A Pseudocode Approach with C

61 Data Structures: A Pseudocode Approach with C

62 Data Structures: A Pseudocode Approach with C

63 Data Structures: A Pseudocode Approach with C

64 Data Structures: A Pseudocode Approach with C

65 Data Structures: A Pseudocode Approach with C

66 Data Structures: A Pseudocode Approach with C

67 Data Structures: A Pseudocode Approach with C

68 Data Structures: A Pseudocode Approach with C

69 Data Structures: A Pseudocode Approach with C

70 Data Structures: A Pseudocode Approach with C

71 Data Structures: A Pseudocode Approach with C

72 Data Structures: A Pseudocode Approach with C

73 Data Structures: A Pseudocode Approach with C

74 Data Structures: A Pseudocode Approach with C

75 Data Structures: A Pseudocode Approach with C

76 Data Structures: A Pseudocode Approach with C

77 Data Structures: A Pseudocode Approach with C

78 Data Structures: A Pseudocode Approach with C

79 Data Structures: A Pseudocode Approach with C

80 Data Structures: A Pseudocode Approach with C

81 Data Structures: A Pseudocode Approach with C

82 Data Structures: A Pseudocode Approach with C
5-3 Applications Appended linked lists - two linked lists are built - and then append the second on to the end of the first one Array of linked lists Data Structures: A Pseudocode Approach with C

83 Data Structures: A Pseudocode Approach with C
Figure 5-19: Appended linked lists Data Structures: A Pseudocode Approach with C

84 Algorithm : Append two linked lists
algorithm appendTwoLists Creates two linked lists and then appends the second to the first. print (This program creates two lists and then appends them) print (Enter first file name) read (fileName) open (filename) build (pList1, filename) printList (pList1) print (Enter second file name) read (fileName) open (fileName) Data Structures: A Pseudocode Approach with C

85 Algorithm : Append two linked lists (2)
build (pList2, fileName) printList (pList2) append (pList1, pList2) The lists are now appended. Print to prove success. printList (pList1) return end appendTwoLists Data Structures: A Pseudocode Approach with C

86 Algorithm : Build two linked lists
algorithm build (ref pList <head pointer>, val file <data file>) This algorithm builds a linked list from data in a file. Pre pList is a pointer for list to be built file must exist. Post List has been built and address in pList. pList = createList loop (not end of file) 1 read (file into dataIn) 2 searchList (pList, pPre, pLoc, dataIn.key) 3 addNode (pList, pPre, dataIn) return end build Data Structures: A Pseudocode Approach with C

87 Algorithm : Append linked lists
algorithm append (val pList1 <head pointer>, val pList2 <head pointer>) This algorithm appends the second list to the end of the first Pre pList1 and pList2 are pointers to valid lists. Post Second list appended to the first list. 1. if (pList1->count zero) 1 pList1->head = pList2->head 2 else 1 pLoc = pList1->head 2 loop (pLoc->link not null) 1 pLoc = pLoc->link 3 pLoc->link = pList2->head 3 pList1->count = pList1->count + pList2->count 4 return end append Data Structures: A Pseudocode Approach with C

88 Circularly-linked Lists
The last node’s link points to the first node of the list allow access to nodes in the middle of the list without starting at the beginning Data Structures: A Pseudocode Approach with C

89 Circularly-linked Lists (2)
Generally, the link in the last node can point either to the header node or the first node How to stop for searching in the circularly-linked lists? loop (target not equal to ploc->data.key AND ploc-> link not equal to startAddress) Data Structures: A Pseudocode Approach with C

90 Data Structures: A Pseudocode Approach with C

91 Data Structures: A Pseudocode Approach with C
Doubly-linked Lists A linked-list structure in which each node has a pointer to both its successor and its predecessor a header node consists of 3 parts of metadata a count a position pointer for traversals a rear pointer Each node, including a header node, contains 2 parts a backward pointer a forward pointer Data Structures: A Pseudocode Approach with C

92 Data Structures: A Pseudocode Approach with C

93 The pseudocode for the header structure in a doubly-linked list
node share structure back <pointer> fore <pointer> variant structure metadata count <integer> pos <pointer> rear <pointer> user data key <keyType> - end node Data Structures: A Pseudocode Approach with C

94 Data Structures: A Pseudocode Approach with C

95 Data Structures: A Pseudocode Approach with C

96 Data Structures: A Pseudocode Approach with C
(continued) Data Structures: A Pseudocode Approach with C

97 Data Structures: A Pseudocode Approach with C

98 Data Structures: A Pseudocode Approach with C

99 Data Structures: A Pseudocode Approach with C
Multilinked Lists A list with two or more logical key sequences Data Structures: A Pseudocode Approach with C

100 Data Structures: A Pseudocode Approach with C
President Year First Wife Washington, George 1789 Custis,Martha Dandridge Adams, John 1797 Smith, Abigail Jefferson,Thomas 1801 Skelton,Martia Wayles Madison, James 1809 Todd, Dorothy Payne Monroe, James 1817 Kortright, Elizabeth Adams, John Quincy 1825 Johnson, Louisa Catherine Jackson,Andrew 1829 Robards, Rachel Donelson Van Buren,Martin 1837 Hoes, Hannah Harrison, William H 1841 Symmes ,Anna Tyler , John Christian, Letitia Table 5-2: First ten presidents of the United States Data Structures: A Pseudocode Approach with C

101 Data Structures: A Pseudocode Approach with C


Download ppt "Data Structures: A Pseudocode Approach with C"

Similar presentations


Ads by Google