Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

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.
Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
Data Structures: A Pseudocode Approach with C
DATA STRUCTURES USING C++ Chapter 5
Data Structures Intro/Linked List Review CIS 237 – Data Structures.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
Ceng-112 Data Structures I Chapter 5 Queues.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
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.
Ceng-112 Data Structures I Chapter 8 Search Trees.
Pointer Review. Node { int value; Node * next; Node( int val =-1, Node * n = NULL) {value = val; next = n;} } Given a ordered linked list pointed to.
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.
©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Data Structure Data structure uses collection of related variables that can be accessed.
Self Referential Structure. A structure may not contain a member of its own type. struct check { int item; struct check n; // Invalid };
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:
Searching Chapter 2.
Ceng-112 Data Structures ITurgut Kalfaoglu 1 Chapter 3 Stacks.
Reference: Vinu V Das, Principles of Data Structures using C and C++
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
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.
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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
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 Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Chap 3 Linked Lists. Vocabulary Linear List 线性表 Linked List 链表 Retrieval 检索 Traversal 遍历 Node 结点 Circularly Linked Lists 循环链表 Doubly Linked Lists 双向链表.
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.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
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.
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
© 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.
(1-3) Basics of a Linked List I Instructor - Andrew S. O’Fallon CptS 122 (June 9, 2016) Washington State University.
Chapter 16: Linked Lists.
Pointers and Linked Lists
Queues Chapter 4.
Pointers and Linked Lists
Stacks and Queues Chapter 4.
Introduction to Linked Lists
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
Binary Search Trees Chapter 7 Objectives
UNIT-3 LINKED LIST.
Queues Chapter 4.
(2-1) Data Structures & The Basics of a Linked List I
Data Structures: A Pseudocode Approach with C
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Introduction to Linked Lists
(2-1) Data Structures & The Basics of a Linked List I
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Presentation transcript:

Ceng-112 Data Structures I 1 Chapter 3 Linear Lists

Ceng-112 Data Structures I 2 Linear Lists Arrays are inefficient whenever sequenced data need to be inserted or deleted. The linked list efficiently handles insertions and deletions. But it is inefficient for search and retrieval.

Ceng-112 Data Structures I 3 Figure 3-1 Linear Lists

Ceng-112 Data Structures I 4 Figure 3-2 Linear Lists Operations are; 1.Insertion 2.Deletion 3.Retrieval 4.Traversal (exception for restricted lists).

Ceng-112 Data Structures I 5 Figure 3-3 Linear Lists Insertion

Ceng-112 Data Structures I 6 Figure 3-4 Linear Lists Deletion

Ceng-112 Data Structures I 7 Figure 3-5 Linear Lists Retrieval

Ceng-112 Data Structures I 8 Figure 3-6 Linked Lists

Ceng-112 Data Structures I 9 Linked Lists Advantage of linked lists; –Data are easily inserted or deleted. It is not necessary to shift elements of a linked list. Disadvantage of linked lists; –We are limited to sequential searches.

Ceng-112 Data Structures I 10 Figure 3-7 The elements in a linked list are called nodes. The nodes in a linked list are called self-referential structures. In a self referential structure, each instance of the structure contains a pointer to another instance of the same structural type. Linked Lists

Ceng-112 Data Structures I 11 Figure 3-8

Ceng-112 Data Structures I 12 Figure 3-9 Create List

Ceng-112 Data Structures I 13 Create List Algorithm createList Allocate dynamic memory for a linked list head node and returns its address to caller. PRE Nothing POST Head node allocated or error returned RETURN head node pointer or null if memory overflow 1. if (memory available) 1. allocate (pNew) 2. pNew  head =null pointer 3. pNew  count = 0 2. else 1. pNew = null pointer 3. return pNew end createList

Ceng-112 Data Structures I 14 Insert Node Adds data to a linked list. 1.Allocate memory for the new node and insert data. 2.We need to know the location of the node that precedes the new node. 1.If predecessor is null, it means that there is no predecessor to the data being added or adding to an empty list or at the beginning of the list. 2.Otherwise, adding to middle of the list or end of the list. 3.Point the new node’s predecessor to it.

Ceng-112 Data Structures I 15 Figure 3-10 Insert Node

Ceng-112 Data Structures I 16 Figure 3-11 Insert Node

Ceng-112 Data Structures I 17 Figure 3-12 Insert Node

Ceng-112 Data Structures I 18 Figure 3-13 Insert Node

Ceng-112 Data Structures I 19 Algorithm insertNode(val pList, val pPre, val dataIn ) Insert data into a new node in the linked list. PRE pList is a pointer to a valid list head structure pPre is a pointer to data’s logical predecessor dataIn contains data to be inserted POST data have been inserted in sequence RETURN true if seccessful, false if memory overflow Insert Node

Ceng-112 Data Structures I allocate (pNew) 2. if (memory overflow) return false 3. pNew  data = dataIn 4. if (pPre null) Adding before first node or to empty list. 1. pNew  link = pList  head 2. pList  head = pNew 5. else Adding in middle or at end. 1. pNew  link = pPre  link 2. pPre  link = pNew 6. pList  count = pList  count return true end insertNode Insert Node

Ceng-112 Data Structures I 21 Figure 3-14 Delete Node

Ceng-112 Data Structures I 22 Figure 3-15 Delete Node

Ceng-112 Data Structures I 23 Algorithm deleteNode(val pList, val pPre, val pLoc, ref dataOut ) Deletes data from a linked list and returns it to calling module. PRE pList is a pointer to a valid list head structure pPre is a pointer to predecessor node pLoc is a pointer to node to be deleted dataOut is address to pass deleted data to calling module POST data have been deleted and return to caller Delete Node

Ceng-112 Data Structures I dataOut = pLoc  data 2. if (pPre null) Deleting firt node. 1. pList  head = pLoc  link 3. else Deleting in middle or at end. 1. pPre  link = pLoc  link 4. pList  count = pList  count – 1 5. release pLoc 6. return end deleteNode Delete Node

Ceng-112 Data Structures I 25 Figure 3-16 Search List

Ceng-112 Data Structures I 26 Algorithm searchList(val pList, val pPre, val pLoc, ref target ) Searches list and passes back address of node containing target. PRE pList is a pointer to a valid list head structure pPre is a pointer variable to receive predecessor node pLoc is a pointer to receive current node target is the key being sought POST pLoc points to first note with equal or greater than target key or null if target > key of last node pPre points to largest node smaller than key or null if target < key of first node Search List

Ceng-112 Data Structures I 27 1.pPre = null 2.pLoc = pList  head 3.loop (pLoc not null AND target > pLoc  data.key 1.pPre = pLoc 2.pLoc = pLoc  link Set return value 4. if (pLoc is null) 1. found = false 5. else 1. if (target equal pLoc  data.key) 1. found = true 2. else 1. found = false 6. return found end searchList Search List

Ceng-112 Data Structures I 28 Figure 3-17 Traverse List pWalker = pList  head loop (pWalker not null) process(pWalker  data) pWalker = pWalker  link

Ceng-112 Data Structures I 29 Algorithm traverse(val pList, val fromWhere, ref dataPtr ) Traverses a linked list. Each call returns the location of an element in the list. PRE pList is a pointer to a valid list head structure fromWhere is 0 to start at the first element dataPtr is the address of a pointer to data POST address places in dataPtr and returns true or if end of list, returns false. RETURN true if next element located, false if end of list. Traverse List

Ceng-112 Data Structures I 30 1.if (fromWhere is 0) start from first 1. if (pList  count is zero) return false 2. else 1. pList  pos = pList  head 2. dataPtr = address(pList  pos  data) 3. return true 2. else start from pos 1. if (pList  pos  link is null) end of list 1. return false 2. else 1. pList  pos = pList  pos  link 2. dataPtr = address(pList  pos  data) 3. return true end traverse Traverse List

Ceng-112 Data Structures I 31 Sample Definition typedef struct node {int number; struct NODE *link; }NODE; typedef struct {int count; NODE *pos; NODE *head; NODE *rear; }LIST;

Ceng-112 Data Structures I 32 HW-3 Create node structure to process the student information. The student number should be the key info. Follow the all remarks and instructions in your text book pages from 91 to 97 and build the your C codes to satisfy the defined requirements. Load your HW-3 to FTP site until 29 Mar. 07 at 17:00.