Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.

Slides:



Advertisements
Similar presentations
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
M180: Data Structures & Algorithms in Java
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Lecture 2 Pointers Pointers with Arrays Dynamic Memory Allocation.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Rossella Lau Lecture 4, DCO20105, Semester A, DCO Data structures and algorithms  Lecture 4: C++ and list  Usage of Vector and List  C++
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Lecture No.01 Data Structures Dr. Sohail Aslam
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Chapter 5: Programming Languages and Constructs by Ravi Sethi Activation Records Dolores Zage.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Dynamic Memory Allocation 9.8.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Chapter 5 – Dynamic Data Structure Par1: Abstract Data Type DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen
D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C H A P T E R F I V E Memory Management.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
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.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Review 1 List Data Structure List operations List Implementation Array Linked List.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Lecture – Pointers1 C++ Pointers Joseph Spring/Bob Dickerson School of Computer Science Operating Systems and Computer Networks Based on notes by Bob Dickerson.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
DATA STRUCTURE & ALGORITHMS Pointers & Structure.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Revision on C++ Pointers TCP1201: 2013/2014. Pointer Basics  Why pointer is important? 1. Reference/Point to existing data without cloning the data.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
Slide 1 of 36. Attributes Associated with Every Variable Data type Data type Actual value stored in variable Actual value stored in variable Address of.
P OINTERS A pointer is an address All data is stored in memory in some location that is indexed with an address Can refer to variables by name or by memory.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
14-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
POINTERS AND MEMORY ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
Lectures linked lists Chapter 6 of textbook
UNIT-3 LINKED LIST.
Chapter 4 Linked Lists.
Pointers Revisited What is variable address, name, value?
Pointers Psst… over there.
Pointers Psst… over there.
Dynamic Memory Allocation
Chapter 12 Pointers and Memory Management
Dynamic Memory A whole heap of fun….
Chapter 17: Linked Lists.
Pointers, Dynamic Data, and Reference Types
Presentation transcript:

Comp 245 Data Structures Linked Lists

An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster processing Programming is easier for most; arrays are “built in” to most languages Memory is contiguous

A Single Linked List A Node contains information(data) and a link The majority of the time, linked lists are dynamic Memory efficient Sequential access; slow processing compared to array Nodes are non-contiguous

Pointer Variable Review A pointer contains a memory address; at this address is where data is stored. C++ pointer review Declaring a pointer Allocating an Deallocating Memory Using a pointer

Declaring a Pointer int x; This declares an integer variable; integer data is stored directly into this variable. int * x; This declares an integer pointer variable; a memory address will be stored directly into this variable; at this address is where the integer data is stored. Examples: int x, y, *z; char * word; list * grades;

Allocating and Deallocating Memory new This function will dynamically allocate memory from the heap. The address of this memory is returned from the function. delete The function will deallocate the memory associated with a pointer variable Example: int * x = new int(5); //code using x here delete x;

Using Pointers //declare one int and two ptr’s to int’s int x, *y, *z; //dynamically allocate memory for an integer y = new int; //assign data to an int variable x = 3; //dereference pointer to store int data at pointer address *y = 5; //assign z the address of the int variable x z = &x; //output sum of data – should be 11 cout << x + *y + *z << endl; //deallocate dynamic memory – for every new there should be a matching delete delete y;

Worksheets Pointer manipulation Creating a Linked List Linked List coding fundamentals

Other Types of Linked Lists Doubly Linked

Other Types of Linked Lists Circular

Other Types of Linked Lists Doubly Circular

Other Types of Linked Lists Lists of Lists

C++ STL List Class The C++ STL (Standard Template Library) is a generic collection of class templates and algorithms that allow programmers to easily implement standard data structures like a list. List containers are implemented as doubly-linked lists; Doubly linked lists can store each of the elements they contain in different and unrelated storage locations Here is a code sample GradeAnaSTL.cpp