Deux applications. Freelists Les appels système new et delete sont lents: // Singly-linked list node with freelist template class Link { private: static.

Slides:



Advertisements
Similar presentations
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Advertisements

Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
1 Linked List Position (1). 2 Linked List Position (2)
Les pointeurs 1. 00A0 00A1 00A2 00A3 00A4 00A5 00A6 00A7 00A8 00A9 00AA 00AB 00AC 00AD 00AE 00AF 00B0 2 Code du programme BSSTas void main(){ node exemple(0);
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class List { public: List(); // constructor List(const.
Linked Lists A linked list is a series of connected nodes Each node contains at least –A piece of data (any type) –Pointer to the next node in the list.
Lecture 5 Sept 12, 2011 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists.
Stacks Using Linked Lists Here, we use the same concept of the stack but eliminate the MAXIMUM data items constraint. Since we shall be using Linked Lists.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
Reference: Vinu V Das, Principles of Data Structures using C and C++

1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 13 Recursion Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Review of Lists, Stacks, and Queues CS 400/600 – Data Structures.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Circular Linked List Singly Circular Linked List Doubly Circular Linked List.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
 آشنايي با درخت  درخت هاي دودويي  پيمايش درختان  هرم  جنگل اهداف 1.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Yang Cao Department of Computer Science Virginia Tech Copyright ©
CS505 Data Structures and Algorithms
Abstract Data Types Polynomials CSCI 240
Doubly Linked List Review - We are writing this code
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Algorithm for deleting a node from a singly linked list
Pointers Revisited What is variable address, name, value?
Reserved Words.
Recursion.
Chapter 16-2 Linked Structures
הקצאות דינאמיות קרן כליף.
ليست هاي پيوندي.
Stack and Queues Stack implementation using Array
Lists List: finite sequence of data elements
Stack and Queues Stack implementation using Array
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Introduction to Programming
Pointers & Functions.
CSCI 333 Data Structures Chapter 4 13 and 16 September 2002.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Introduction to Programming
Linked Lists.
CS150 Introduction to Computer Science 1
Dynamic Memory.
Lists.
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
Pointers & Functions.
Pointers, Dynamic Data, and Reference Types
Linked Lists.
Presentation transcript:

Deux applications

Freelists Les appels système new et delete sont lents: // Singly-linked list node with freelist template class Link { private: static Link * freelist; // Début public: Elem element; // Valeur du noeud Link* next; // Pointe au nœud suivant Link(const Elem& elemval, Link* nextval =NULL) { element = elemval; next = nextval; } Link(Link* nextval =NULL) {next=nextval;} void* operator new(size_t); // Surcharge void operator delete(void*); // Surcharge };

Freelists (2) template Link * Link ::freelist = NULL; template // Surcharge de new void* Link ::operator new(size_t) { if (freelist == NULL) // La liste est vide return ::new Link; Link * temp = freelist; freelist = freelist->next; return temp; }

Freelists (3) template // Surcharge de delete void Link ::operator delete(void* ptr){ ((Link *)ptr)->next = freelist; freelist = (Link *)ptr; }

Tours d’Hanoi typedef int Pole; #define move(X, Y) cout << " Déplacer " << (X) << " vers " << (Y) << endl void TOH(int n, Pole start, Pole goal, Pole tmp) { if (n == 0) return; // Cas de base TOH(n-1, start, tmp, goal); // Appel récursif: n-1 disques move(start, goal); // Déplacer un disque TOH(n-1, tmp, goal, start); // Appel récursif n-1 disques }

enum TOHop { DOMOVE, DOTOH }; class TOHobj { public: TOHop op; int num; Pole start, goal, tmp; TOHobj(int n, Pole s, Pole g, Pole t) { op = DOTOH; num = n; start = s; goal = g; tmp = t; } TOHobj(Pole s, Pole g) { op = DOMOVE; start = s; goal = g; } };

void TOH(int n, Pole start, Pole goal, Pole tmp, Stack & S){ S.push(new TOHobj(n, start, goal, tmp)); // Initialisation TOHobj* t; while (S.pop(t)) { // Déterminer la prochaine tâche if (t->op == DOMOVE) // Déplacement move(t->start, t->goal); else if (t->num > 0) { // 3 étapes de la récursion en ordre inverse int num = t->num; Pole tmp = t->tmp, goal = t->goal, start = t->start; S.push(new TOHobj(num-1, tmp, goal, start)); S.push(new TOHobj(start, goal)); S.push(new TOHobj(num-1, start, tmp, goal)); } delete t; // Must delete the TOHobj we made }