Doubly Linked List Review - We are writing this code

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Queues and Linked Lists
Data Structure Lecture-5
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
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.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
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.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
slides adapted from Marty Stepp and Hélène Martin
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Linked Data Structures
Linked List, Stacks Queues
Programming Circular Linked List.
Lecture 6 of Computer Science II
Cpt S 122 – Data Structures Abstract Data Types
Linked List Stacks, Linked List Queues, Dequeues
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
Building Java Programs
Linked Lists CSM Linked Lists.
UNIT-3 LINKED LIST.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Linked lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
Algorithm for deleting a node from a singly linked list
Programming Abstractions
Stack and Queue APURBO DATTA.
Java collections library
slides created by Marty Stepp
Chapter 4 Linked Lists
Notes on Assignment 1 Your code will have several classes, most notably the class that represents the entire list data structure, and the class.
Doubly linked lists.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
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
Queue, Deque, and Priority Queue Implementations
Chapter 16-2 Linked Structures
Linked Lists.
Topic 11 Linked Lists -Joel Spolsky
Linked Lists: Implementation of Queue & Deque
Chapter 13 Collections.
Building Java Programs
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Programming Abstractions
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Doubly Linked List Implementation
slides created by Marty Stepp and Hélène Martin
Building Java Programs
Problem Understanding
Building Java Programs
LINKED LISTS.
Header and Trailer Sentinels
Building Java Programs
slides adapted from Marty Stepp
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
slides created by Marty Stepp
Doubly Linked List Implementation
Linked lists.
More on Linked List Yumei Huo Department of Computer Science
Topic 11 Linked Lists -Joel Spolsky
Data Structures & Programming
Presentation transcript:

Doubly Linked List Review - We are writing this code for practice –as you need just a singly linked list. Suppose we want to use a doubly linked list as a queue. Recall a queue is like a line for a performance. We insert at the tail. We remove from the front.

class DoublyLinkedList { int size; // Number of items in linked list Node* head; // First node in list Node* tail; // Last node in list public: DoublyLinkedList(void) { head=NULL; size=0;tail=NULL;} void add(int); int remove(); void clear(); bool isEmpty() {return head==NULL;} string toString(); }; // Node in a doubly linked list of ints class Node { public: int value; // data Node * next; // next Node Node * prev; // previous Node Node(int v, Node * anext=NULL, Node *aprev=NULL); }; wHAT What is this? What does an empty list look like?

Write the code to insert 10 random numbers between 0 and 100 into the list. Pseudo code is fine for now, but come as close as possible to real code. int main() { }

int main() { DoublyLinkedList list; for (int i = 0; i < 10; i++) list.add(rand()%100); cout << list.toString(); }

Write the code to create a string which Contains the contents of the linked list.

Stringstream can be very useful string DoublyLinkedList::toString(){ stringstream ss; ss << "DoublyLinkedList " << endl; return ss.str(); }

string DoublyLinkedList::toString(){ stringstream ss; ss << "DoublyLinkedList " << endl; for (Node * t = head; t != NULL; t = t->next) ss << t->value << " "; ss << endl; return ss.str(); }

Write the code to add to a linked list // Add to the tail of the list void DoublyLinkedList::add(int w) { } 5 5

Write the code to add to a linked list void DoublyLinkedList::add(int w) { Node* n = new Node(w,NULL,tail); //Can set previous here size++; if (DEBUG) cout << “add: Just added to list " << w << endl; if (head == NULL) { head = n; tail = n; return; } tail->next = n; Letting every routine talk to you is a great way of testing.

Write the code to remove the first element from a doubly linked list (return the value removed) Draw pictures of what you are trying to do. It will help YOU and help others you communicate with. What are all the special cases?

Write the code to remove the first element from a doubly linked list // Return the first thing in the queue and return it. int DoublyLinkedList::remove() { if (head == NULL) return 0; size--; int v = head->value; Node* temp = head; head = head->next; delete (temp); if (head == NULL) tail = NULL; else head->prev = NULL; return v; }

Write the code to remove from a doubly linked list // Return the first thing in the queue and return it. int DoublyLinkedList::remove() { if (head == NULL) return 0; // Check for inability to remove anything;(A) size--; // Update class variables (B) int v = head->value; // Take care of return value (C) Node* temp = head; // clean up memory (D) head = head->next; // Set new head. (E) delete (temp); // clean up memory (D) if (head == NULL) // Set tail if queue is empty (F) tail = NULL; else head->prev = NULL; // Remove previous of current head. (G) return v; // Take care of return value (C) } Score your code. At one point each, what is your score?