Sessions 26 and 27 - Data Structures & Algorithm

2 enter integer 4 the list is 2 1 3 4 sharif

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: 89 90 int main() { // test List of int values List< int > integerList; testList( integerList, "integer" ); // test List of double values List< double > doubleList; testList( doubleList, "double" ); return 0; } // end main Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 31 Testing a List of integer values Enter one of the following: 1 to insert at beginning of list 2 to insert at end of list 3 to delete from beginning of list 4 to delete from end of list 5 to end list processing ?1 Enter integer: 1 The list is: 1 ?1 Enter integer: 2 The list is: 2 1 ?2 Enter integer: 3 The list is: 2 1 3 ?2 Enter integer: 4 The list is: 2 1 3 4 Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 32 ?3 2 removed from list The list is: 1 3 4 ?3 1 removed from list The list is: 3 4 ?4 4 removed from list The list is: 3 ?4 3 removed from list The list is empty ?5 End list test Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 33 Testing a List of double values Enter one of the following: 1 to insert at beginning of list 2 to insert at end of list 3 to delete from beginning of list 4 to delete from end of list 5 to end list processing ?1 Enter double: 1.1 The list is: 1.1 ?1 Enter double: 2.2 The list is: 2.2 1.1 ?2 Enter double: 3.3 The list is: 2.2 1.1 3.3 ?2 Enter double: 4.4 The list is: 2.2 1.1 3.3 4.4 ?3 2.2 removed from list The list is: 1.1 3.3 4.4 Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 34 ?3 1.1 removed from list The list is: 3.3 4.4 ?4 4.4 removed from list The list is: 3.3 ?4 3.3 removed from list The list is empty ?5 End list test All nodes destroyed All nodes destroyed Sharif University of Technology Introduction to Programming: Sessions 26 and 27: Data Structures & Algorithm 35 Linked Lists Types of linked lists Singly linked list (used in example) Pointer to first node Travel in one direction (null-terminated) Circular, singly-linked As above, but last node points to first Doubly-linked list Each node has a forward and backwards pointer Travel forward or backward Last node null-terminated Circular, double-linked As above, but first and last node joined Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 36 Stacks Stack Nodes can be added/removed from top Constrained version of linked list Like a stack of plates Last-in, first-out (LIFO) data structure Bottom of stack has null link Stack operations Push: add node to top Pop: remove node from top Stores value in reference variable Sharif University of Technology Advanced Programming: Sessions 26 and 27: Data Structures & Algorithm 37 Stacks Stack applications Function calls: know how to return to caller Return address pushed on stack Most recent function call on top If function A calls B which calls C: A B A C B A B A A Used to store automatic variables Popped of stack when no longer needed Used by compilers Example in the exercises in book Sharif University of Technology Advanced Programming: Sessions 26 and...
View Full Document

This note was uploaded on 09/01/2013 for the course CE CE230 taught by Professor Arash during the Fall '13 term at Sharif University of Technology.

Ask a homework question - tutors are online