Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

Structure.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
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.
 Head pointer  Last node  Build a complete linked list  Node deletion  Node insertion  Helpful hints.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Memory Management.
Module 20/21/22: The Standard Template Library
Pointers What is the data type of pointer variables?
EGR 2261 Unit 11 Pointers and Dynamic Variables
Popping Items Off a Stack Using a Function Lesson xx
Understanding Algorithms and Data Structures
Pointers and Linked Lists
Linked List :: Basic Concepts
Pointers and Linked Lists
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Pointers & Arrays.
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
Two-Dimensional Arrays Lesson xx
UNIT-3 LINKED LIST.
Objectives Identify the built-in data types in C++
Chapter 4 Linked Lists.
Linked lists.
Chapter 5 Classes.
CSCE 210 Data Structures and Algorithms
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
void Pointers Lesson xx
Pointer Data Type and Pointer Variables
Arrays & Functions Lesson xx
Structures Lesson xx In this module, we’ll introduce you to structures.
Returning Structures Lesson xx
Pointer to a Structure & Structure Containing a Pointer Difference Lesson xx  In this presentation, we will illustrate the difference between a pointer.
One-Dimensional Array Introduction Lesson xx
File I/O with Records Lesson xx
Passing Structures Lesson xx
Popping Items Off a Stack Lesson xx
CMSC202 Computer Science II for Majors Lecture 04 – Pointers
Pointers and Linked Lists
Value returning Functions
Pointers, Dynamic Data, and Reference Types
Pointer to Structures Lesson xx
Linked Lists.
Functions Pass By Value Pass by Reference
Linked Lists Chapter 4.
Linked List.
Chapter 17: Linked Lists.
From C to C++: Summary of weeks 1 - 4
Lists.
Arrays Arrays A few types Structures of related data items
C Programming Pointers
Data Structures & Algorithms
Pointers & Arrays.
Lecture No.02 Data Structures Dr. Sohail Aslam
Creating and Using Pointer Variables in C++ By: Ed Brunjes
Pointers and dynamic objects
Pointer Data Type and Pointer Variables
Pointer Data Type and Pointer Variables
Linked lists.
Pointers, Dynamic Data, and Reference Types
4.1 Introduction Arrays A few types Structures of related data items
LINEAR DATA STRUCTURES
Presentation transcript:

Linked List Lesson xx   In this presentation, we introduce you to the basic elements of a linked list.

Objectives Linked list concept Node structure Creating a static linked list Accessing items in a linked list Our goal is to show you what a linked list looks like pictorially, introduce you to the elements of a linked list called a node. Create a linked list and show you how to access items in a linked list.

Illustration of a Linked List Head a b c d e 0 A linked list consists of a series of records that are joined together using a pointer. Each of the records are called nodes of a linked list. You can see that record a is connected to record b which is connected to record c and so forth. Linked list are used because it is easy to insert and delete items compared to an array. Arrays are stored sequentially in memory, records, on the other hand, may be store anywhere in memory. They are joined together using pointers to make the list seem sequential.

Linked List Program #include <iostream> using std::cout; using std::endl; struct entry {   int value;   entry* next; }; int main() {   entry n1, n2, n3;   n1.value = 100;   n2.value = 200;   n3.value = 300;   n1.next = &n2;   n2.next = &n3; cout << n1.value << “ “ << n1.next << endl;   cout << n1.next->value << endl; // cout <<(*n1.next).value ;   cout << n1.next->next->value << endl ;    cout << n2.next->value << endl ;   return 0; } Here is a program that creates a basic linked list and shows how to access information in the nodes. We will go through the entire program in the next series of slides.

Node Structure struct entry { int value; entry* next; }; .value .next Here is the code to set up the structure of a node in the linked list. You set up a structure that contain data members. The last item is a pointer to a structure that looks like itself. In our specific case, we have one integer data member called value. In a different example, you might have a data member to keep track of a name, an address , a city and a state. We’ll keep our structure simple for demonstration purposes. The last line of the structure is entry * next. This is a pointer to a structure of the form entry. We have drawn a picture of the node for you. The first part is called .value and contains an int, the 2nd part is called .next and is a pointer to a structure of the form entry.

Node Declaration entry n1, n2, n3; n1 n1.value n1.next n2 n2.value Entry n1,n2, n3, sets up 3 nodes called n1,n2, and n3. Looking at the picture you can see that each node contains 2 parts. The parts of n1 are n1.value and n1.next. At this point in the program, the nodes contain no data.

Node Initialization n1.value = 100; n2.value = 200; n3.value = 300; n1 n2.next n3 n3.value n3.next 100 200 300 This portion of the program puts information into the nodes. In n1.value, we store the number 100. In n2.value we store 200 and etc. n1.value n1.next

Connecting Nodes n1.next = &n2; n2.next = &n3; n1 n1.value n1.next n2 100 200 300 (56c) (7fa) (88b) 7fa 88b Here is the code to connect the nodes of the linked list together. N1.next = &n2; means that n1.next should contain the address of n2. In our drawing, n1.next contains the address of n2 which is 7fa. N2.next contains the address of n3 which is 88b.

Different Explanation   n1.next = &n2;   n2.next = &n3; n1 n2 n2.value n2.next n3 n3.value n3.next 100 200 300 Another way to explain the statement n1.next = &n2; is: make n1.next point to the node n2. n2.next = &n3 means make n2.next point to node n3. In C++ a pointer and an address are the same thing. n1.value n1.next

Print Contents of First Node cout << n1.value << “ “ << n1.next; << endl; n1 n1.value n1.next n2 n2.value n2.next n3 n3.value n3.next 100 200 300 (56c) (7fa) (88b) 7fa 88b The statement: cout << n1.value << N1.next prints the contents of the 1st node. N1.value contains the # 100 and n1.next contains the address of n2 which is 7fa in our example.

Print Value of 3rd Node cout << n2.next->value << endl ; n1 n1.value n1.next n2 n2.value n2.next n3 n3.value n3.next 100 200 300 (56c) (7fa) (88b) 7fa 88b cout << n2.next->value << endl; prints the #300. Here’s why, n2.next contains 88b which is a pointer to the structure n3. Since n2.next is pointer to a structure, and the structure contains 2 parts, we have to specify which part of the structure, the value part or next part. So n2.next->value specifies the value part which contains the # 300. Of course, the simple way is: cout << n3.value; Later on we’ll see why the simple way isn’t always possible.

Print Value of 3rd Node Different Syntax   cout << n1.next->next->value;   n1 n1.value n1.next n2 n2.value n2.next n3 n3.value n3.next 100 200 300 (56c) (7fa) (88b) 7fa 88b cout << n1.next->next->value also prints the #300. Here’s why, n1.next contains 7fa which is a pointer to the structure n2. N1.next->next contains 88b which is another pointer to a structure and is pointing to n3. The value member of n3 contains the # 300.

Summary Linked list concept Node structure Creating a static linked list Accessing items in a linked list We have written code that demonstrates how to build a linked list. In that program we learned how to create a node using a structure and we learned how to access the various parts of the linked list using different terminology. In the next module, we’ll learn more about linked list and how to manipulate them.