CS261 Data Structures Linked Lists - Introduction.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Linked Lists Mohammed Almashat CS /03/2006.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Stacks, Queues, and Linked Lists
Linked Lists.
CS Winter 2010 Linked Lists - Part 2 Deque with Double Links and Sentinel, Bag.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Data Structures: A Pseudocode Approach with C
Linked Lists... An introduction to creating dynamic data structures.
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.
Linked Lists in C and C++ By Ravi Prakash PGT(CS).
1 Day 03 Introduction to C. 2 Memory layout and addresses r s int x = 5, y = 10; float f = 12.5, g = 9.8; char c = ‘r’, d = ‘s’;
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
CS261 Data Structures Dynamic Arrays Part 2: Implementation.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Introduction to Data Structures Systems Programming.
ECE 103 Engineering Programming Chapter 61 Abstract Data Types Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Introduction to Data Structures Systems Programming Concepts.
CS261 – Data Structures Iterator ADT Dynamic Array and Linked List.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
C Lab 1 Introduction to C. Basics of C Developed by Dennis Ritchie in the 1970s. Maps very easily to machine instructions. Even allows inline assembly!
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
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.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
Definition: A stack is an ordered collection of elements in which insertions(Push) and deletions(Pop) are restricted to one end. LIFO(Last In First Out)
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Circular Linked List Singly Circular Linked List Doubly Circular Linked List.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
CS Fall 2009 Linked List Introduction. Characteristics of Linked Lists Elements are held in objects termed Links Links are 1-1 with elements, allocated.
  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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Linked List Introduction
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
12 C Data Structures.
Data Structures and Algorithms
LINKED LISTS CSCD Linked Lists.
Pointers and Linked Lists
Abstract Data Types (ADTs)
Object Oriented Programming COP3330 / CGS5409
Linked Lists: Implementation of Queue & Deque
Review & Lab assignments
Data Structures and Algorithms
Chapter 17: Linked Lists.
ECE 103 Engineering Programming Chapter 62 Stack Implementation
Introduction to C CS 3410.
Presentation transcript:

CS261 Data Structures Linked Lists - Introduction

Dynamic Arrays Revisited Dynamic array can sometimes be slow – When? – Why?

Linked Lists to the Rescue Alan Newell 1956 b c Dynamic Array in memory data Size = 3 Capacity = 5 da a c b Linked List in memory head Size = 3 LL a What can we now do…and not do …quickly?

Data elements held in structures called “links” Like a chain: each link is tied to the next Links are 1 – 1 with elements, allocated and released as necessary data link data link … next link next link Linked Lists - Characteristics

struct Link { /* Single link. */ TYPE val; /* Data contained by this link. */ struct Link *next; /* Pointer to next link. */ }; val next Typical Link Structure (Singly Linked)

Linked List Variations next 8 List List List 513

Implementing a stack interface with a linked list: – Header with head reference only: null if empty – Null terminated – Singly linked – Where should the ‘top’ of the stack be???? – Only access first element Linked List Stack 8 firstLink 513 listStack

struct linkedListStack { struct Link *firstLink; /* Initialize routine sets to zero/ NULL. */ }; void linkedListStackInit (structlinkedListStack s) { s->firstLink = 0; } Linked List Stack firstLink listStack

void pushListStack(struct ListStack *s, TYPE d) { /* You are going to write this: 1. Allocate (malloc) a new link (check that it works!). 2. Set data fields in the new link. 3. Change head to point to new link. */ } Linked List Stack

Linked List Tips… Draw the diagram! Go through the steps visually, labeling each step Convert each step to C code Try the boundary cases: – Empty list? – List with several items?

How do you tell if stack is empty? How do you return first element (i.e., firstLink)? How do you remove an element? Other Linked List Operations

Worksheet 17 pushLinkedListStack popLinkedListStack Your Turn: