Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Chapter 4 Linked Stacks and Queues Andreas Savva.

Similar presentations


Presentation on theme: "Data Structures Chapter 4 Linked Stacks and Queues Andreas Savva."— Presentation transcript:

1 Data Structures Chapter 4 Linked Stacks and Queues Andreas Savva

2 2 The Problem of Overflow Writing a program using arrays we must decide on the maximum amount of memory that will be needed. Writing a program using arrays we must decide on the maximum amount of memory that will be needed. If we run the program on a small sample, then much of this space will never be used. If we run the program on a small sample, then much of this space will never be used. If we run the program on a large sample, then we may encounter overflow, even when the computer memory itself is not fully used, because the size of the array is too small. If we run the program on a large sample, then we may encounter overflow, even when the computer memory itself is not fully used, because the size of the array is too small. Even if we declare our arrays large enough to use up all the available memory, we can still encounter overflow, since one array may be full while unused space remains in others. Even if we declare our arrays large enough to use up all the available memory, we can still encounter overflow, since one array may be full while unused space remains in others.

3 3 A pointer is a variable that stores a memory location (that is a machine address) of some data structure. A pointer is a variable that stores a memory location (that is a machine address) of some data structure. The programmer is not concerned where the data is stored; the computer itself locates the data when required. The programmer is not concerned where the data is stored; the computer itself locates the data when required. Pointers Lynn r s John t u Ann The object “Ann” is lost; it has become garbage NULL

4 4 Linked Structures Every element of a linked structure has a pointer pointing to the next structure. Every element of a linked structure has a pointer pointing to the next structure. 21 Jan 99-672205 Paul 28 Feb 99-456543 Carol 30 Jun 99-785434 Tom 3 Oct 99-767554 Fred 18 Feb 99-296580 Jacky A linked list

5 5 Dynamic Memory Allocation Dynamic memory is created at run-time. Dynamic memory is created at run-time. The storage occupied by dynamic objects is managed entirely by the programmer. The storage occupied by dynamic objects is managed entirely by the programmer. The only way to access dynamic memory is by using pointers. The only way to access dynamic memory is by using pointers.

6 6 Pointers Whenever memory for more data is needed, the system is requested to allocate it. Whenever memory for more data is needed, the system is requested to allocate it. item *p; p = new item; When an item is no longer needed, its space can be returned to the system, which can then assign it to another task. When an item is no longer needed, its space can be returned to the system, which can then assign it to another task. delete p;

7 7 Pointer Examples ??? p = NULL; p = new int; 2004 *p = 2004; 2004 delete p; ìnt* p; NULL ?? ??

8 8 Dynamically Allocated Arrays int size, *A; size = 7; A = new int[size];..... delete []A; 792114312 [0][1] [2] [3][6] [4] [5] A A+1 A+4 A[0] = *A = 7 A[1] = *(A+1)= 9 A[2] = *(A+2)= 2 etc.

9 9 Pointer Assignment Music p Calculus q Music p Calculus q Calculus p Calculus q p = q *p = *q

10 10 Pointers to Structures Data members are accessed as: Data members are accessed as: (*p).numerator (*p).numerator p->numerator p->numerator class Fraction { public: int numerator; int numerator; int denominator; int denominator;}; Fraction *p;

11 11 The Basics of Linked Structures class Node { public: // data members int entry; Node *next; // constructors Node(); Node(int item, Node *n = NULL); }; Node::Node() { next = NULL; } Node::Node(int item, Node *n) { entry = item; next = n; } struct Node { // data members int entry; Node *next; // constructors Node(); Node(int item, Node *n = NULL); }; Node::Node() { next = NULL; } Node::Node(int item, Node *n) { entry = item; next = n; } Classes: Default is private Struct: Default is public

12 12 Linked Structures Node First(12); Node *p = &First; Node *q = new Node(34); p->next = q; q->next = new Node(107,p); cout next->next->next->entry; q = q->next; p 34 q 12 First 107 12

13 13 Linked Stack implementation - Pointers struct Node { Stack_entry entry; Node *next; Node(); Node(int item, Node *n = NULL); }; class Stack { public: Stack(); bool empty() const; Error_code top(Stack_entry &) const; Error_code pop(); Error_code top_and_pop(Stack_entry &); Error_code push(const Stack_entry &); int size() const; void print() const; void clear(); private: Node *top_node; };

14 14 Data Structure – Linked Structure top_nodePaulJohnMark AnnMary struct Node { Stack_entry entry; Node *next;..... }; class Stack { public:..... private: Node *top_node; };

15 15 Create Stack Stack::Stack() { } Constructor: top_node

16 16 Empty bool Stack::empty() const { }

17 17 Top Error_code Stack::top(Stack_entry &item) const { } top_node539 41 Top

18 18 Pop Error_code Stack::pop() { } top_node 8 4 2 temp

19 19 Top and Pop Error_code Stack::top_and_pop(Stack_entry &item) { } item = top_node B A C temp A

20 20 Push Error_code Stack::push(const Stack_entry &item) { } top_node Old 2 nd node Old top node Old last node temp Newnode

21 21 Size int Stack::size() const { int count = 0; } temp top_node742 16 count 0 1 2345

22 22 Print void Stack::print() const { } top_node742 16 temp Screen 7 4 2 1 6

23 23 Clear void Stack::clear() { }

24 24 Safeguards for (i = 0; i < 1000000; i++) { Stack small; small.push(some_data); } What is the result of the following statement? Garbage This problem should not be blamed on the client. This problem should not be blamed on the client. Every linked structure should be equipped with a destructor to clear its objects before they go out of scope. Every linked structure should be equipped with a destructor to clear its objects before they go out of scope.

25 25 Destructor Stack::~Stack() // Post: The Stack is cleared { while (!empty()) pop(); }

26 26 The Assignment Statement Stack outer_stack;..... for (i = 0; i < 1000000; i++) { Stack inner_stack; inner_stack.push(some_data); inner_stack = outer_stack; } outer_stack.top_node inner_stack.top_node some_data Lost data What is the problem with the following code? More garbage More garbage Lost of outer_stack entries (because of the destructor) – This will make the pointer outer_stack.top_node pointing at some memory location that can be later allocated to some other variable. Lost of outer_stack entries (because of the destructor) – This will make the pointer outer_stack.top_node pointing at some memory location that can be later allocated to some other variable.

27 27 Overloading the Assignment Statement void Stack::operator = (const Stack &original) // Overload assignment // Post: The Stack is reset as a copy of Stack original { Node *new_top, *new_copy, *original_node = original.top_node; if (original_node == NULL) new_top = NULL; else { // Duplicate the linked nodes new_copy = new_top = new Node(original_node->entry); while (original_node->next != NULL) { original_node = original_node->next; new_copy->next = new Node(original_node->entry); new_copy = new_copy->next; } while (!empty()) pop(); // Clean out the old Stack entries top_node = new_top; // and replace them with the new entries }

28 28 Copying a Stack What is the problem with the following code? What is the problem with the following code? void destroy_the_stack(Stack copy) {..... } int main() { Stack vital_data;..... destroy_the_stack(vital_data); }

29 29 The Copy Constructor Stack::Stack(const Stack &original) // Copy constructor // Post: The Stack is initialized as a copy of Stack original { Node *new_copy, *original_node = original.top_node; if (original_node == NULL) top_node = NULL; else { // Duplicate the linked nodes top_node = new_copy = new Node(original_node->entry); while (original_node->next != NULL) { original_node = original_node->next; new_copy->next = new Node(original_node->entry); new_copy = new_copy->next; }

30 30 The Final Linked-Stack Specification class Stack { public: // Standard Stack methods Stack(); void clear(); bool empty() const; int size() const; Error_code top(Stack_entry &item) const; Error_code pop(); Error_code top_and_pop(Queue_entry &item); Error_code push(const Queue_entry &item); void print( ) const; // Safety features for linked structures ~Stack(); Stack(const Stack &original); void operator = (const Stack &original); private: Node *top_node; };

31 31 Derived Classes class Stack { public: Stack(); bool empty() const; Error_code top(Stack_entry &item) const; Error_code pop(); Error_code push(const Queue_entry &item); ~Stack(); Stack(const Stack &original); void operator = (const Stack &original); protected: Node *top_node; }; class Extended_stack : public Stack { public: Extended_class(); void clear(); int size() const; Error_code top_and_pop(Queue_entry &item); void print() const; }

32 32 Derived Classes methods: Stack (constructor) empty top pop push data members class Stack methods: Extended_stack (constructor) empty top pop push clear size print top_and_pop data members additional data members class Extended_stack Base class Derived class

33 33 Linked Queues Two pointers are needed: Two pointers are needed: front front rear rear frontPaulJohnMark AnnMary rear

34 34 Linked Queue implementation - Pointers class Queue { public: Queue(); bool empty() const; Error_code retrieve(Queue_entry &item) const; Error_code serve(); Error_code append(const Queue_entry &item); ~Queue(); Queue(const Queue &original); void operator = (const Stack &original); protected: Node *front, *rear; };

35 35 Create Queue Queue::Queue() { } Constructor: front rear

36 36 Empty bool Queue::empty() const { }

37 37 Retrieve frontBAC rear Error_code Queue::retrieve(Queue_entry &item) const { } item = A

38 38 Serve front Entry to Remove rear Error_code Queue::serve() { } temp

39 39 Append front rear Entry to add temp Error_code Queue::append(const Queue_entry &item) { }


Download ppt "Data Structures Chapter 4 Linked Stacks and Queues Andreas Savva."

Similar presentations


Ads by Google