Sessions 26 and 27 - Data Structures &amp; Algorithm

# Sessions 26 and 27 - Data Structures &amp; Algorithm -...

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

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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
Linked Lists H D Q firstPtr lastPtr ...

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern