Link Lists In this section of notes you will learn how to create and manage a dynamic list. Link Lists in Pascal.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Stacks, Queues, and Linked Lists
Linked Lists.
James Tam Linked Lists in Pascal Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
DATA STRUCTURES USING C++ Chapter 5
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
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.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James TamThe Bubble Sort in Pascal Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
James Tam Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
James Tam Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
James Tam Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
James Tam Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.
James Tam Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
James Tam Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam program investors (input, output, investorList, updatedInvestorList); const MAXCLIENTS = 10; NAMELENGTH = 30; LENGTH = 30; type Client =
James Tam Getting Started With Pascal Programming What is the basic structure of a Pascal Program Variables in Pascal Performing input and output with.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
Lists Lists as an abstract data type (ADT)
James Tam Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
James Tam Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
J. Michael Moore From James Tam's material Records CSCE 110.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Recursion You will learn what is recursion as well as how and when to use it in your programs.
James Tam Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
James Tam Records You will learn in this section of notes what is a record and how to use them in Pascal.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
James Tam Pointers In this section of notes you will learn about a third type of variable that stores addresses rather than data.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
James Tam Pointers In this section of notes you will learn about another type of variable that stores addresses rather than data.
J. Michael Moore Recursion CSCE 110 From James Tam’s material.
Data Structures Using C++ 2E
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Pointers OVERVIEW.
CSC 211 Data Structures Lecture 13
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Pascal Programming Pointers and Dynamic Variables.
Linked Structures, LinkedStack
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
CCSA 221 Programming in C CHAPTER 11 POINTERS ALHANOUF ALAMR 1.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
LINKED LISTS.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
History of Computing – Pascal
Chapter 16: Linked Lists.
Lecture - 6 On Data Structures
UNIT-3 LINKED LIST.
Pascal Prof. Steven A. Demurjian
Linked Lists Chapter 10.
Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
Presentation transcript:

Link Lists In this section of notes you will learn how to create and manage a dynamic list. Link Lists in Pascal

Arrays Easy to use but suffer from a number of drawbacks: 1) Fixed size 2) Adding/Deleting elements can be awkward

Arrays: Fixed Size The size of the array must be stated when the program is compiled The following example won't work: program notAllowed (input, output); var size : integer; arr : array [1..size] of integer; begin write('Enter size of array: '); readln(size); end. The workaround is to allocate more space than you need

Arrays: Fixed Size The size of the array must be stated when the program is compiled The following example won't work: program notAllowed (input, output); var size : integer; arr : array [1..size] of integer; begin write('Enter size of array: '); readln(size); end. The workaround is to allocate more space than you need The size of the array must be predetermined!

Arrays: Adding Elements In The Middle 123 125 135 155 161 165 166 167 167 169 177 178

Arrays: Deleting Elements From The Middle 123 125 135 155 161 166 167 167 169 177 178

Alternative To Arrays: Link Lists More complex coding may be required Some list management functions are more elegant (and faster) Data Ptr Data Ptr Node Link List

Some Link List Functions Declaring a link list Creating a new list Traversing the list Adding a node to the list Searching the list Deleting a node from the list Note: These list functions will be illustrated by portions of an example program. This program is the investors program but implemented as a link list rather than as array. The complete program can be found in Unix under: /home/231/examples/link_lists/investors.p

Declaring A Link List Syntax: type Name of the list data = Type of the list data; Name of the list pointer = ^ Node; Node = Record data : Name of the list data; nextPointer : Name of the list pointer; Name of link list = Name of list pointer;

Declaring A Link List (2) Example: type Client = record firstName : array [1..NAMELENGTH] of char; lastName : array [1..NAMELENGTH] of char; income : real; email : array [1..EMAILLENGTH] of char; end; (* Declaration of record Client *) ListData = Client; ListPointer = ^ Node; Node = record data : ListData; nextPointer : ListPointer; end; (* Declaration of record Node *) ClientList = ListPointer;

Creating A New List Algorithm: Example: The pointer to the beginning of the list is passed into the procedure as a variable parameter and initialized to NIL signifying that the new list is empty. Example: procedure createNewList (var tamjClientList : ClientList); begin tamjClientList := NIL; end; (* Procedure *)

Traversing The List Algorithm: Steps 1. Start by initializing a pointer to the beginning of the list. 2. If the pointer is NIL then show message indicating that there are no nodes to display and stop. 3. Process the node. 4. Move on to the next node by following the node's nextPointer (set pointer to point to the next node). 5. Repeat 3 – 4 until the end of the list is reached (pointer is NIL).

Traversing The List (2) Example: procedure displayList ( tamjClientList : ClientList); var currentNode : ListPointer; begin currentNode := tamjClientList; writeln('CLIENT LIST':20); if (currentNode = NIL) then writeln; writeln('List is empty, no clients to display'); end; (* if-then *)

Traversing The List (3) while (currentNode <> NIL) do begin writeln('First name: ':20, currentNode^.data.firstName); writeln('Last Name: ':20, currentNode^.data.lastName); writeln('Income $':20, currentNode^.data.income:0:2); writeln('Email: ':20, currentNode^.data.email); writeln; currentNode := currentNode^.nextPointer; end; (* while *) end; (* displayList *)

Adding A Node To The The List Algorithm: Steps 1. Assign a pointer to the front of the list. 2. If the pointer is NIL then the list is empty and add the node to the front of the list and stop. 3. Otherwise traverse the list with two pointers, one pointer (current pointer) goes to the end of the list, the other stays one node behind it (previous pointer). 4. Attach the new node to the last node in the list (the one reached by the previous pointer). 5. The next pointer of the new node becomes NIL (indicating that this is the end of the list).

Adding A Node To The List (2) Example: procedure addToList (var tamjClientList: ClientList; newNode : ListPointer); var currentNode : ListPointer; previousNode : ListPointer; begin (* Empty list add new node to front *) if (tamjClientList = NIL) then tamjClientList := newNode; newNode^.nextPointer := NIL; end

Adding A Node To The List (3) else begin currentNode := tamjClientList; while (currentNode <> NIL) do previousNode := currentNode; currentNode := currentNode^.nextPointer; end; (* while *) previousNode^.nextPointer := newNode; newNode^.nextPointer := NIL; end; (* else *) end; (* Procedure *)

Searching The List Algorithm: The procedure is run in order to find a node(s) that has a field which matches some desire value. Either the node or nodes will be found in the list or else the procedure will have searched every node in the list and have found no matches. A flag will be set to true or false indicating the success or failure of the search. Variables There are two pointers to the list: a. Current pointer – traverses the list from beginning to end b. Previous to first pointer – points to the node that occurs just prior to the first successful match. Note: The second pointer does not directly come into play when the user only wants to search the list. They are essential when the person wishes to erase a node from the list. Since the erase procedure calls the search procedure, it is is passed in but it's value is not used when the person just wishes to perform a search without performing a deletion. 2. A boolean that indicates the status of the search.

Searching The List (2) Steps Current pointer starts at the beginning of the list. Since the search has not yet begin, previous is set to NIL and the flag is set to false. A check is performed to determine if the node is a match. If this is the case and the flag is still false (indicating that we haven't found a previous node that was a successful match) set the flag to true (since a match was found). Since the search function requires a list of all matches (and not just the first instance) don't stop searching the list. If the flag is set to false then set the previous pointer to point to the current node. If the flag is set to true then don't change the value of flag (the previous pointer tracks the node just prior to the node which first meets the search criteria). Move on to the next node (by setting the current pointer to the current node's next pointer). Continue step 2 – 4 until the end of the list is reached (current node is NIL).

Searching The List (3) Example: procedure search ( tamjClientList : ClientList; desiredName : NameArray; var isFound : boolean; var previousFirst : ListPointer ); var currentNode : ListPointer; begin currentNode := tamjClientList; previousFirst := NIL; isFound := False;

Searching The List (4) while (currentNode <> NIL) do begin if (desiredName = currentNode^.data.lastName) then writeln('Found contact':20); writeln('First name :':20, currentNode^.data.firstName); writeln('Last name :':20, currentNode^.data.lastName); writeln('Income $':20, currentNode^.data.income:0:2); writeln('Email :':20, currentNode^.data.email); writeln; if (isFound = False) then isFound := True; end; (* if-then *)

Searching The List (5) if (isFound = False) then previousFirst := currentNode; currentNode := currentNode^.nextPointer; end; (* while *) writeln('Contact not found in list'); end; (* search *)

Deleting A Node From The List Algorithm: Steps Search the list (by calling the search procedure) to determine if there exists a node that matches the necessary criteria for deletion. Check the flag to determine if the search was successful or not. If the flag is false then there is no matching node in the list. Stop. There is no matching node to delete. Check to see if the node to be deleted is the first node in the list or not by determining if the previous node is NIL. If the node to be deleted is the first node then have a temporary pointer point to the first element and make the front of the list the second element. If the node to be deleted is not the first node then have a temporary pointer point to the node to be deleted. Set the next pointer of the previous node point to the node after the node to be deleted (bypassing this node) For steps 4 & 5 free up the memory allocated by the node to be deleted.

Deleting A Node From The List (2) Example: procedure erase (var tamjClientList : ClientList ); var desiredName : NameArray; previousFirst : ListPointer; temp : ListPointer; isFound : boolean; begin write('Enter last name of client to delete: '); readln(desiredName); search (tamjClientList, desiredName, isFound, previousFirst); if (isFound = True) then

Deleting A Node From The List (3) begin writeln('Deleting first instance of ', desiredName); if (previousFirst = NIL) then temp := tamjClientList; tamjClientList := tamjClientList^.nextPointer; end (* if-then *) else temp := previousFirst^.nextPointer; previousFirst^.nextPointer := temp^.nextPointer; end; (* else *) dispose(temp); end; (* if-then *) end; (* Procedure *)

Summary You should now know how to perform some common link list operations: Declaration of a link list Creation of a new list Traversal of the list Adding a new node to the list Deleting an existing node from the list Searching the list for a node that meets some criteria