© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Linked Lists.
Data Structure HKOI training /4/2010 So Pak Yeung.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
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.
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.
Chapter 17 Linked List.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
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.
Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.
CSC2100B Tutorial 2 List and stack implementation.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Working with Pointers An exercise in destroying your computer.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Subject Name : Data Structure Using C Title : Linked Lists
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
  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.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
One implementation of the LIST ADT Insert new node before current and new node becomes current (assume new node created) node newNode = new node; head.
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
UNIT-3 LINKED LIST.
Linked lists.
ENERGY 211 / CME 211 Lecture 12 October 17, 2008.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
Chapter 20: Binary Trees.
8-1.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
8-1.
Chapter 16-2 Linked Structures
Chapter 21: Binary Trees.
Stack and Queues Stack implementation using Array
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.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Linked Lists.
Linked lists.
BY PROF. IRSHAD AHMAD LONE.
Linked Lists Chapter 5 (continued)
Presentation transcript:

© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures

© M. Gross, ETH Zürich, 2014 Agenda  Linked Lists  Queues  Stacks  Exercise

© M. Gross, ETH Zürich, Linked Lists  Collection of nodes  Two components for each node  to store the data  to store the “next” linked node  Last node has no linked node  Referenced by NULL  Structure of node declaration c++ struct nodeType { int data; nodeType *next; }; nodeType *head; head

© M. Gross, ETH Zürich, Linked Lists Operations  Traversing a list  Use other pointer similar to *head not to lose reference to beginning of list  Finding an element in the list  Traverse the list until you find the element (here 12)  Do something if not found nodeType *current; current = head; while (current != NULL) { cout data next; } nodeType *current; current = head; int element = 12; while (current != NULL) { if(current->data == element) break; current = current->next; } if(current == NULL) cout <<"element not found“ << endl;

© M. Gross, ETH Zürich, Linked Lists Operations  Inserting a new node  Create a new node  Determine insertion position and assign it to current  Make the new node point to the same node that current is pointing to  Make current now point to the new node nodeType *newNode; newNode = new nodeType; newNode->data = 37; newNode->next = current->next; current- >next = newNode;

© M. Gross, ETH Zürich, Linked Lists Operations  Deleting a node  Determine the node to be removed (deleteNode) and point current to the node prior to that node  Create a pointer to point to the node you want to delete (i.e *deleteNode)  Make current’s link now point to deleteNode’s link  delete the node from memory Assuming current is at correct position nodeType *deleteNode; deleteNode = current->next; current->next = deleteNode->next; delete deleteNode;

© M. Gross, ETH Zürich, Building Linked Lists  Backward approach // Init the list nodeType *head; nodeType *newNode; int num; head = NULL; // Build the list using the backward approach for (int i=0; i<3; i++) { cout << "Enter number :"; cin >> num; newNode = new nodeType; // Create the new node newNode->data = num; // and assign its data value newNode->next = head; // make its link point to the // front of the list head = newNode; // make the head now be the // newNode }

© M. Gross, ETH Zürich, 2014 Doubly Linked Lists  Similar to single linked lists but  Each node has a connection to the previous one  List can be traversed also backwards  Can reach a3 from a1 and vice versa 8 struct nodeType { int data; nodeType *next; nodeType *prev; }; nodeType *head;

© M. Gross, ETH Zürich, Queues  Like linked lists  Add new elements at the end  Remove elements from the head struct element { int value; // The value of the element struct element *next; // Pointer to the next element }; typedef struct element Node; Node *head = NULL; //Pointer to first element Node *tail = NULL; //Pointer to last element a1a1 a2a2 a3a3 anan null headtail

© M. Gross, ETH Zürich, Queues  add element  remove element v a1a1 a2a2 a3a3 headtail null tail->next = v; tail = v; v -> next = NULL; Node *v = head; head = head->next; delete v; a1a1 a2a2 a3a3 null head tail v

© M. Gross, ETH Zürich, Stacks  “One-sided queues”  Add to and remove from the “top” by pushing/popping  The only element that is reachable is the top element! a0a0 a1a1 a2a2 a3a3 a4a4 top

© M. Gross, ETH Zürich, Stacks  add element  remove element v a1a1 a2a2 a3a3 head null v->next = head; head = v; Node *v = head; head = head->next; delete v; a1a1 a2a2 a3a3 null head v

© M. Gross, ETH Zürich, Stacks vs Queues  Queues:  Work in FIFO (first-in-first-out) fashion  Store two pointers  Add/removal using different pointers  Operating systems: Jobs wait in a queue for CPU time  Printers: Incoming jobs end up in a queue  Stacks:  Work in LIFO (last-in-first-out) fashion  Store only one pointer  Add/removal from top only  Evaluating Arithmetic Operations  Function calls: They are put in a stack by the compiler