{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Sessions 26 and 27 - Data Structures & Algorithm

Sessions 26 and 27 - Data Structures & Algorithm -...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Advanced Programming Sessions 26 and 27: Data Structures & Algorithm 1 Instructor: Arash Tavakkol [email protected] Sharif University of Technology Department of Computer Engineering Fall 2012
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Outline Introduction Self-Referential Classes Dynamic Memory Allocation and Data Structures Linked Lists Stacks Queues Trees Searching Algorithms Sorting Algorithms 2 Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm
Background image of page 2
Introduction Fixed-size data structures Arrays, structs Dynamic data structures Grow and shrink as program runs Linked lists Insert/remove items anywhere Stacks Insert/remove from top of stack Queues Like a line, insert at back, remove from front Binary trees High-speed searching/sorting of data 3 Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Self-Referential Classes Self-referential class Has pointer to object of same class Link together to form useful data structures Lists, stacks, queues, trees Terminated with NULL pointer 4 Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 15 10 NULL pointer (points to nothing) Data member and pointer
Background image of page 4
Self-Referential Classes Sample code class Node { public: Node( int ); void setData( int ); int getData() const; void setNextPtr( Node * ); const Node *getNextPtr() const; private: int data; Node *nextPtr; }; Pointer to object called a link nextPtr points to a Node 5 Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Dynamic Memory Allocation and Data Structures Dynamic memory allocation Obtain and release memory during program execution Create and remove nodes Operator new Takes type of object to create Returns pointer to newly created object Node *newPtr = new Node( 10 ); Returns bad_alloc if not enough memory 10 is the node's object data 6 Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm
Background image of page 6
Dynamic Memory Allocation and Data Structures Operator delete delete newPtr; Deallocates memory allocated by new , calls destructor Memory returned to system, can be used in future newPtr not deleted, only the space it points to 7 Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Linked Lists Linked list Collection of self-referential class objects (nodes) connected by pointers (links) Accessed using pointer to first node of list Subsequent nodes accessed using the links in each node Link in last node is null (zero) Indicates end of list Data stored dynamically Nodes created as necessary Node can have data of any type 8 Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm
Background image of page 8
Linked Lists H D Q firstPtr lastPtr ...
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}