ENERGY 211 / CME 211 Lecture 12 October 17, 2008.

Slides:



Advertisements
Similar presentations
M180: Data Structures & Algorithms in Java
Advertisements

Linked Lists in C and C++ CS-2303, C-Term Linked Lists in C and C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
More on Dynamic Memory Allocation Seokhee Jeon Department of Computer Engineering Kyung Hee University 1 Illustrations, examples, and text in the lecture.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
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.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2002 Pearson Education, Inc. Slide 1.
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.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
UNIT-II Topics to be covered Singly linked list Circular linked list
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Chapter 16: Linked Lists.
STACKS & QUEUES for CLASS XII ( C++).
Lecture 6 of Computer Science II
Cpt S 122 – Data Structures Abstract Data Types
Week 4 - Monday CS221.
Data Structure By Amee Trivedi.
Dynamic Allocation Review Structure and list processing
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Introduction to Linked Lists
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
Linked List Stacks, Linked List Queues, Dequeues
MEMORY REPRESENTATION OF STACKS
More Linking Up with Linked Lists
Lecture No.06 Data Structures Dr. Sohail Aslam
Data Structure Dr. Mohamed Khafagy.
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
UNIT-3 LINKED LIST.
Data Structures and Algorithms
Chapter 4 Linked Lists.
Data Structures Interview / VIVA Questions and Answers
ENERGY 211 / CME 211 Lecture 19 November 3, 2008.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
Introduction to Linked Lists
Circularly Linked Lists
Object Oriented Programming COP3330 / CGS5409
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked List Intro CSCE 121 J. Michael Moore.
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.
Lists CSE 373 Data Structures.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Data Structures and Algorithms
Linked Lists in C and C++
Chapter 17: Linked Lists.
LINKED LISTS.
CMSC 341 Lists 3.
CMSC 341 Lists 3.
Introduction to C++ Linear Linked Lists
Lecture 16 Section 6.2 Thu, Mar 1, 2007
Lists CSE 373 Data Structures.
CS210- Lecture 6 Jun 13, 2005 Announcements
CSCS-200 Data Structure and Algorithms
Linked List Intro CSCE 121.
BY PROF. IRSHAD AHMAD LONE.
LINEAR DATA STRUCTURES
CMPT 225 Lecture 5 – linked list.
Presentation transcript:

ENERGY 211 / CME 211 Lecture 12 October 17, 2008

Drawbacks of Arrays Even a dynamically-allocated array is still a block of a fixed size What if you have a data structure that changes frequently, due to addition or deletion of data? You can preallocate, but what if you want to keep elements ordered? Shifting array elements is inefficient

Memory Issues With arrays, one must frequenly deallocate and reallocate to avoid wasting space This can result in fragmentation of your program’s memory, thus degrading performance Every time you use the new operator, the runtime system looks for an available block, and this takes time!

Remedy: Lists An alternative is to use a data structure that represents a list of elements In a list, each element is associated with its own block of memory, called a node, independent of other elements No need to shift elements if one is added or deleted in the middle of the list Iteration through list somewhat slower than in an array, using pointers

Singly-linked Lists Each element’s node contains: the element itself a pointer to the next element’s node In the last node, this pointer is NULL To iterate, keep track of the first node (the head) and follow next pointers: current = current->next; The -> operator performs dereferencing and member access: x->y is (*x).y

Visualization 2-node singly-linked list of doubles 3.14 0xbbbbff18 2.72 NULL 1.41 0xbbbbff0c 2-node singly-linked list of doubles New node (1.41) added to list after head node (3.14), which updates its next pointer

Doubly-linked Lists In a doubly-linked list, each node contains two pointers, to the next and previous nodes This allows iteration in either direction For first element, previous pointer is NULL More storage required, and additions/deletions more complicated Need to keep track of head and tail nodes

Visualization 3.14 0xbbbbff10 NULL 3.14 0xbbbbff20 NULL 2.72 NULL 0xbbbbff20 2.72 NULL 0xbbbbff00 1.41 0xbbbbff10 0xbbbbff00 Node for 1.41 added to 2nd position in doubly-linked list of doubles Must update twice as many pointers!

Type Definitions Need to define a type for each element’s node, including pointer(s) Example: doubly-linked list of ints typedef struct tagNode { int data; struct tagNode *prev, *next; } NodeType; Need tagNode since structure refers to itself in its definition Keep track of pointer to first node, and use -> operator to access other node

Queues and Stacks Many data structures can be implemented using a linked list A queue maintains a list in a FIFO manner (first in, first out) A stack maintains a list in a LIFO manner (last in, first out) Stacks are used by the runtime system to implement function calls, also useful for evaluating mathematical expressions

Lists in the Standard Library The C++ standard library includes a number of classes that implement different kinds of collections Examples: vector and list A vector uses a dynamically allocated array for random access A list uses a doubly-linked list Each collection class has its faster and slower operations, so choose wisely!

Next Time Recursion The MDS debugger