Introduction to Systems Programming - COMP 1002, 1402

Similar documents
1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

DATA STRUCTURES USING C

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Data Structure [Question Bank]

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

10CS35: Data Structures Using C

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Analysis of a Search Algorithm

Data Structures and Data Manipulation

Introduction to Data Structures and Algorithms

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Lecture 12 Doubly Linked Lists (with Recursion)

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

ADTs,, Arrays, Linked Lists

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

CSE 211: Data Structures Lecture Notes VII

Data Structures. Level 6 C Module Descriptor

Linked Lists Linked Lists, Queues, and Stacks

Common Data Structures

Data Structures Using C++ 2E. Chapter 5 Linked Lists

PES Institute of Technology-BSC QUESTION BANK

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Basic Data Structures and Algorithms

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

LINKED DATA STRUCTURES

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Data Structures Using C++ 2E. Chapter 5 Linked Lists

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Data Structures Fibonacci Heaps, Amortized Analysis

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

System Software Prof. Dr. H. Mössenböck

Node-Based Structures Linked Lists: Implementation

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Universidad Carlos III de Madrid

Computer Programming using C

Introduction to Object-Oriented Programming

Sequential Data Structures

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Lecture Notes on Binary Search Trees

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Sample Questions Csci 1112 A. Bellaachia

recursion, O(n), linked lists 6/14

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Unordered Linked Lists

Algorithms and Data Structures

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Data Structure with C

Cpt S 223. School of EECS, WSU

Lecture Notes on Binary Search Trees

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

INTERNATIONAL JOURNAL OF PURE AND APPLIED RESEARCH IN ENGINEERING AND TECHNOLOGY

Linear ADTs. Restricted Lists. Stacks, Queues. ES 103: Data Structures and Algorithms 2012 Instructor Dr Atul Gupta

EE2204 DATA STRUCTURES AND ALGORITHM (Common to EEE, EIE & ICE)

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Binary Search Trees CMPSC 122

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

BCS2B02: OOP Concepts and Data Structures Using C++

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Stacks. Data Structures and Data Types. Collections

STACKS,QUEUES, AND LINKED LISTS

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Data Structures Using C++

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

Algorithms Chapter 12 Binary Search Trees

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING LESSON PLAN

Chapter 3: Restricted Structures Page 1

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Stacks. Linear data structures

St S a t ck a ck nd Qu Q eue 1

Analysis of Algorithms I: Binary Search Trees

Data Structures UNIT III. Model Question Answer

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Converting a Number from Decimal to Binary

Introduction to data structures

Java Software Structures

List, Stack and Queue. Tom Chao Zhou CSC2100B Data Structures Tutorial 3

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Symbol Tables. Introduction

Ordered Lists and Binary Trees

1 Abstract Data Types Information Hiding

Chapter 13: Query Processing. Basic Steps in Query Processing

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Data Structures and Algorithms Stacks and Queues

Useful Java Collections

Abstract Data Types. Chapter 2

7.1 Our Current Model

Binary Search Trees (BST)

Data Structures and Algorithms(5)

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

Transcription:

LINKED LISTS Introduction to Systems Programming - COMP 1002, 1402

Outline Linked list Adding and removing data to a link list Linked list search

Common Data Structures in C and C++ 3 Linked lists One-way Doubly-linked Circular Trees Binary Multiple branches Hash Table Combine arrays and linked list Especially for searching for objects by value

Arrays Properties Elements are consecutive in memory Easy to determine location in memory Access a single element quickly But CANNOT change size declare new space Copy all elements (at times) May not correspond to amount of data

Alternative Properties Easily grows to an arbitrary size Correspond to amount of data Less rigid Possible alternative are lists Singly linked list Doubly linked list Trees (binary trees, 2-3 trees, finger trees, B-trees )

Lists Lists can be viewed as chains Each piece of information is a link Links are independent of each other Links are somehow connected to each other Properties Elements are in arbitrary location Must view all predecessors to find one Grows to arbitrary size Massive re-orderings possible quickly

Definitions 7 Linked List A data structure in which each element is dynamically allocated and in which elements point to each other to define a linear relationship Singly- or doubly-linked Stack, queue, circular list Tree A data structure in which each element is dynamically allocated and in which each element has more than one potential successor Defines a partial order

Processing a List Lists can be viewed as chains Begin at the beginning of the list (chain) firstelement is the first link second element is the second link thirdelement is the third link Could be arbitrary length

Singly Linked Lists

What is in a NODE? Each element (NODE) in a list contains: Data a pointer to another NODE How will we define the NODE type?

Single Link per Element

Linked List struct listitem { type ; struct listitem *; }; 12

Linked List (continued) 13 Items of list are usually same type Generally obtained from malloc() Each item points to item Last item points to null Need head to point to first item! Payload of item may be almost anything A single member or multiple members Any type of object whose size is known at compile time Including struct, union, char * or other pointers Also arrays of fixed size at compile time

Usage of Linked Lists 14 Not massive amounts of data Linear search is okay Sorting not necessary or sometimes not possible Need to add and delete data on the fly Even from middle of list Items often need to be added to or deleted from the ends

Linked List (continued) struct listitem { type ; struct listitem *; }; struct listitem *head; 15

Maintaining a List Access the list Keep a pointer to the first NODE (for now) NODE * plist; /* plist is a pointr to first node */ or NODE *head; /* head is a pointer to first node */ All functions modifying a list Must have access to the list (Take plist as a parameter) e.g.: NODE *insertnode(node *plist...);

Insert a Node 1. Allocate memory 2. Locate predecessor (A pointer, ppre) 3. Point new node to its successor 4. Point predecessor to new node

Meanings of ppre

Insert into Empty List

Insert Node at Beginning

Insert Node in Middle

Adding an Item to a List struct listitem *p, *q; Add an item pointed to by q after item pointed to by p Neither p nor q is NULL 22

Adding an Item to a List listitem *addafter(listitem *p, listitem *q){ q -> = p -> ; p -> = q; return p; } 23

Adding an Item to a List listitem *addafter(listitem *p, listitem *q){ q -> = p -> ; p -> = q; return p; } 24

Adding an Item to a List listitem *addafter(listitem *p, listitem *q){ q -> = p -> ; p -> = q; return p; } Question: What to do if we cannot guarantee that p and q are non-null? 25

Adding an Item to a List (continued) listitem *addafter(listitem *p, listitem *q){ if (p && q) { q -> = p -> ; p -> = q; } return p; } 26

What about Adding an Item before another Item? struct listitem *p; Add an item before item pointed to by p (p!= NULL) 27

What about Adding an Item before another Item? 28 Answer: Need to search list from beginning to find previous item Add new item after previous item This is needed Insert item after earlier event times and before later ones

Insert Node at End

Deleting a Node Still requires the predecessor s location! Remove the node Free the memory space!

Delete General Case

Searching Through a List Search should return: If a match exists A pointer to the node containing the key If there is no match NULL A pointer to the largest element that is smaller or equal to the key (for a sorted list)

Traverse Linked Lists printlist, averagelist,

searchlist int searchlist (NODE *plist, NODE **ppre, NODE **pcur, KEY_TYPE target) {... }

Inserting a Node NODE * insertnode(node *plist, NODE *ppre, DATA data) {... }

Deleting a Node NODE * deletenode(node *plist, NODE *ppre) {... }

Doubly-Linked List struct listitem { type ; listitem *prev; listitem *; }; struct listitem *head, *tail; prev prev prev prev 37

Other Kinds of List Structures 38 Queue FIFO (First In, First Out) Items added at end Items removed from beginning Stack LIFO (Last In, First Out) Items added at beginning, removed from beginning Circular list Last item points to first item Head may point to first or last item Items added to end, removed from beginning

Circular List Optional: struct listitem *head; struct listitem *tail; listitem *addafter (listitem *p, listitem *tail){ if (p && tail) { p -> = tail -> ; tail = p; } else if (p) { tail p -> = p; } return tail; } 39

QUESTIONS? 40