Data Structures: A Pseudocode Approach with C

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

Lists CS 3358.
Data Structures: A Pseudocode Approach with C
DATA STRUCTURES USING C++ Chapter 5
Data Structures Intro/Linked List Review CIS 237 – Data Structures.
Ceng-112 Data Structures I Chapter 5 Queues.
M180: Data Structures & Algorithms in Java
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Data Structures Using C++ 2E
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.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Subject Name : Data Structure Using C Title : Linked Lists
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.
Chap 3 Linked Lists. Vocabulary Linear List 线性表 Linked List 链表 Retrieval 检索 Traversal 遍历 Node 结点 Circularly Linked Lists 循环链表 Doubly Linked Lists 双向链表.
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To introduce the basic concepts of linked lists ❏ To introduce the basic concepts.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
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.
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.
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.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Chapter 16: Linked Lists.
CSE 1342 Programming Concepts
Lecture 6 of Computer Science II
Unit – I Lists.
C++ Programming:. Program Design Including
Cpt S 122 – Data Structures Abstract Data Types
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
Big-O notation Linked lists
Review Deleting an Element from a Linked List Deletion involves:
CE 221 Data Structures and Algorithms
Chapter 15 Lists Objectives
Binary Search Trees Chapter 7 Objectives
UNIT-3 LINKED LIST.
Stacks and Queues.
Lecture 22 Binary Search Trees Chapter 10 of textbook
Queues Chapter 4.
Chapter 15 Lists Objectives
Chapter 18: Linked Lists.
Data Structures ADT List
Graphs Chapter 11 Objectives Upon completion you will be able to:
Linked Lists.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Pointers & Dynamic Data Structures
Chapter 4 Unordered List.
Data Structures & Algorithms
Stacks, Queues, and Deques
Chapter 9 Linked Lists.
LINEAR DATA STRUCTURES
Presentation transcript:

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

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

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

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

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

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

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

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

Data Structures: A Pseudocode Approach with C

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

Data Structures: A Pseudocode Approach with C

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

Data Structures: A Pseudocode Approach with C

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

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

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

Data Structures: A Pseudocode Approach with C

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

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

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

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

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

Data Structures: A Pseudocode Approach with C

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

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

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

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

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

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

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) 1 deletedNode (pList, pPre, pLoc,deletedata) else 1 print (Error : Key not in list.) Return end removeNode Data Structures: A Pseudocode Approach with C

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

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

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

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

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

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

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. 1 pList = createList 2 loop (not end of file) 1 read (file into dataIn) 2 searchList (pList, pPre, pLoc, dataIn.key) 3 addNode (pList, pPre, dataIn) 3 return end build Data Structures: A Pseudocode Approach with C

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

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

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

Data Structures: A Pseudocode Approach with C

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

Data Structures: A Pseudocode Approach with C

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

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

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

Data Structures: A Pseudocode Approach with C

Data Structures: A Pseudocode Approach with C

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

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

Data Structures: A Pseudocode Approach with C