Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.

Similar presentations


Presentation on theme: "1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue."— Presentation transcript:

1 1 Linked-list, stack and queue

2 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue

3 3 Outline Abstract Data Type (ADT)‏  Introduction to Abstract Data Type (ADT)‏  Examples of Abstract Data Type Linked list Stack Queue

4 4 Introduction to Abstract Data Type (ADT)‏ Abstract Data Type (ADT)‏  A data type consists of a collection of values together with a set of basic operations on these values  In C++, an ADT is usually implemented using the keyword class. A collection of values is associated to private data members, and a set of basic operations is associated to public member functions  “Abstract” means that users ONLY know the interface (i.e. what operations it provides) without the detailed knowledge about the implementation (i.e. how these operations are implemented).

5 5 Outline Abstract Data Type (ADT)‏ Linked list  Major components  A Simple Linked List Class in C++  Variations of Linked List  Question 1: Tracing operations  Question 2: Polynomial using linked list  Practice: derivative of polynomial Stack Queue

6 6 Major components 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 Head: pointer to the first node The last node points to NULL A  BC Head A node

7 7 A Simple Linked List Class in C++ We use two classes: Node and List Declare Node class for the nodes  data : double -type data in this example  next : a pointer to the next node in the list class Node { public: doubledata;// data Node*next;// pointer to next };

8 8 A Simple Linked List Class in C++ Declare List, which contains  head : a pointer to the first node in the list. Since the list is empty initially, head is set to NULL class List { public: List(void) { head = NULL; }// constructor ~List(void);// destructor bool IsEmpty() { return head == NULL; } Node* InsertNode(int index, double x); int FindNode(double x); int DeleteNode(double x); void DisplayList(void); private: Node* head; };

9 9 A Simple Linked List Class in C++ Operations of List  IsEmpty determine whether or not the list is empty  InsertNode insert a new node at a particular position  FindNode find a node with a given value  DeleteNode delete a node with a given value  DisplayList print all the nodes in the list  The detail implemented is listed in the lecture slides

10 10 Variations of Linked List Variations on the data arrangement  Unsorted linked list Data inside a linked list is stored in an arbitrary order  Sorted linked list Data is sorted by the “key” on each node For example: A student record may use student ID as “key” A list of student records may be sorted in ascending order of the student ID Variations on the structure  Singular linked list (described in the previous slide)‏  Circular linked list The last node points to the first node of the list  Doubly linked list Each node points to not only successor but the predecessor

11 11 Variations of Linked Lists Circular linked lists  The last node points to the first node of the list How do we know when we have finished traversing the list? Solution: We can use a while loop, we can set an initial pointer “current” pointing to “head”. We can traverse the linked list until we found that we go back to the “head” again A Head BC

12 12 Variations of Linked Lists Doubly linked lists  Each node points to not only successor but the predecessor  There are two NULL: at the first and last nodes in the list  Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards Head AB  C 

13 13 Question 1: Tracing operations Suppose we have a sorted linked list storing a sequence of integers  The input sequence is as follow: Step 1: Insert 3 Step 2: Insert 9 Step 3: Delete 3  Draw the linked list before and after each operation The drawing should contain “Head”, links connect nodes, and the NULL pointer. You should also draw “prev”, “curr” if applicable

14 14 Linked list example: Step 1 Before step 1: Insert 3 After step 1: Insert 3 Head   3  Curr, Prev

15 15 Linked list example: Step 2 Before step 2: Insert 9 After step 2: Insert 9 Head  3 Prev  Curr Head 3  9

16 16 Linked list example: Step 3 Before step 3: delete 3 After step 3: delete 3 Head 3 Prev  Curr Head  9  9

17 17 Outline Abstract Data Type (ADT)‏ Linked list Stack  Stack ADT  Operations: Push and Pop Queue

18 18 Stack ADT A stack is a list in which insertion and deletion take place at the same end  This end is called top  The other end is called bottom Stacks are known as LIFO (Last In, First Out) lists.  The last element inserted will be the first to be retrieved

19 19 Operations: Push and Pop Primary operations: Push and Pop Push  Add an element to the top of the stack Pop  Remove the element at the top of the stack top empty stack A top push an element top push another A B top pop A

20 20 Implementation of Stacks Any list implementation could be used to implement a stack  Arrays (static: the size of stack is given initially)‏  Linked lists (dynamic: never become full)‏ We will explore implementations based on array and linked list Let’s see how to use an array to implement a stack first

21 21 Stack class class Stack { public: Stack(int size = 10);// constructor ~Stack() { delete [] values; }// destructor bool IsEmpty() { return top == -1; } bool IsFull() { return top == maxTop; } double Top(); // examine, without popping void Push(const double x); double Pop(); void DisplayStack(); private: int maxTop;// max stack size = size - 1 int top;// current top of stack double* values;// element array };

22 22 Stack class Attributes of Stack  maxTop : the max size of stack  top : the index of the top element of stack  values : point to an array which stores elements of stack Operations of Stack  IsEmpty : return true if stack is empty, return false otherwise  IsFull : return true if stack is full, return false otherwise  Top : return the element at the top of stack  Push : add an element to the top of stack  Pop : delete the element at the top of stack  DisplayStack : print all the data in the stack

23 23 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue  Queue ADT  Enqueue and Dequeue

24 24 Queue ADT Like a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion is performed at the other end. Accessing the elements of queues follows a First In, First Out (FIFO) order.  Like customers standing in a check-out line in a store, the first customer in is the first customer served. rearfront

25 25 Enqueue and Dequeue Primary queue operations:  Enqueue and Dequeue Like check-out lines in a store, a queue has a front and a rear.  Enqueue insert an element at the rear of the queue  Dequeue remove an element from the front of the queue Insert (Enqueue)‏ Remove (Dequeue)‏ rearfront

26 26 Implementation of Queue Just as stacks can be implemented as arrays or linked lists, so with queues. Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks

27 27 An example question List out one similarity and one difference between Stack and Queue  Similarity Both ADTs can be implemented using static array Both support fast insertion and extraction (although the rule to govern the insertion and extraction is different)‏ And more…  Difference Stack is a LIFO data structure, while Queue is a FIFO data structure And more…


Download ppt "1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue."

Similar presentations


Ads by Google