Download presentation
Presentation is loading. Please wait.
Published byCory Sibyl Lang Modified over 9 years ago
1
Comp 245 Data Structures Linked Lists
2
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
3
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
4
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
5
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;
6
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;
7
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;
8
Worksheets Pointer manipulation Creating a Linked List Linked List coding fundamentals
9
Other Types of Linked Lists Doubly Linked
10
Other Types of Linked Lists Circular
11
Other Types of Linked Lists Doubly Circular
12
Other Types of Linked Lists Lists of Lists
13
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. http://www.cppreference.com/wiki/stl/start 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 http://www.cplusplus.com/reference/stl/list/ Here is a code sample GradeAnaSTL.cpp
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.