Information Science 2 - Basic Data Structures- Week 02 College of Information Science and Engineering Ritsumeikan University
Today s class outline l Basic data structures: Definitions and implementation issues - Singly and doubly linked lists - Stack - Queue l Quiz 2
Linked lists: Overview l Arrays and node-based data structures l Linked lists - The idea - Main operations - Single vs. double linked 3
Simplest data structure Array l The array is a fundamental building block for data structures: - Objects are stored in contiguous memory locations and referenced by an index - There is little memory-management overhead - Look-up operations are usually very fast - Operations that require rearrangement (insert, remove, etc.) can be slow 4
Nodes l A node is a small block of memory, referenced via its address, which may point to other nodes via addresses Node with one possible link Node data (element) Node with two possible links Node data (element) Pointer Pointer Pointer - A pointer is a variable that contains an address number - The node s pointers can have addresses of other nodes or may point to NULL, a special address for nothing 5
Node-based structures: Physical vs. Logical l When we draw pictures of node-based data structures, the positions of nodes in the picture have nothing to do with their positions in memory - For example, if a structure is stored like this (physical structure): - Then we might re-draw it like this (logical structure): 6
Linked lists: The idea l A linked list is a collection of nodes linked via pointers (references) Head NULL pointer 3 1 5 4 5 2 - The head of the list generally stores a pointer (reference) to the first node, or NULL if the list is empty. It can also store other information (data type, etc.) - There must be some way of identifying the last node. Typically, it is done by making its pointer (reference) NULL 7
Linked lists: Main operations l The main operations are to add (insert) a node and to delete a node l The operations require two steps: Finding the correct location Doing the work to add / delete the node l There are three possible positions of a node in the list: The front The end Somewhere in the middle 8
Doubly linked list l In a singly linked list, a node stores a data element and one pointer (reference). In a doubly linked list, each node stores: - Data-carrying element - Link to the next node - Link to the previous node prev node element l A doubly linked list also has special nodes called header and trailer nodes header nodes/positions next trailer elements (e.g. pictures) 9
Links: Doubly or singly? l Advantages of doubly linked lists: Can be traversed in either direction (may be essential for some programs) Some operations, such as deleting and inserting before a node, become easier l Disadvantages of doubly linked lists: Requires more space List manipulations are slower (because more links must be changed) Greater chance of having bugs (because more links must be manipulated) 10
Stacks: Overview l The concept of a stack l Main operations on a stack: - Push, pop - Initialize - Check status (empty or full) l Implementation issues l Application examples 11
What is a stack? l A stack is a linear data structure that stores a set of elements in a particular order: It realizes a FILO (First-In, Last-Out) interface l Sometimes it is called a push-down stack l Real life examples: spring-loaded coin dispensers plate dispensers at a cafeteria 12
Push and Pop operations l Plate dispensers are stacks: - Operation Push(item) adds item to top of stack - Operation Pop(item) removes item from top of stack and returns it - Number of items in the stack can vary from 0 to some N, which determines its storage capacity 13
A typical sequence of operations on a stack l Initialize the stack l Determine stack size l Check the status: Is the stack empty or full? l Push an item onto the top of the stack l l Pop an item off the top of the stack l l Clear the stack l 14
Implementation l Array-based stack implementation: Allocate array of some size = maximum number of elements in stack Bottom stack element stored at index 0 Current index tells which element is the top Increment index when element is pushed, decrement when popped Top index 0 l Linked list-based stack implementation: Allocate a node for each stack element Nodes are chained together by reference to next node in the chain : 2 1 15
Stack application example l Determining well-formed expressions with parenthesis Well-Formed Expressions Ill-Formed Expressions ( x x x [ ] ) ( x x x [ ) ( ) [ ] { } ( ( { ( x x x x x ) ( ) } { x x x x x ) ( ) } l Check the expression: ( x { x [ ] } x ) 16
Application example (cont-d) l Check the expression: ( x { x [ ] } x ) '(' : Push ( 'x' : Ignore '{' : Push { 'x' : Ignore '[' : Push [ ']' : Get top of stack, opensymbol='[' Pop Compare if '[' matches ']' '}' : Get top of stack, opensymbol='{' Pop Compare if '{' matches '}' 'x' : Ignore ')' : Get top of stack, opensymbol='(' Pop Compare if '(' matches ')' '\n' : Print expression is well-formed 17
Another example: 1+2 3+4 l l l 1 : push 1 on number stack + : push + on operation stack 2 : push 2 on number stack l : because has higher precedence than +, push onto operation stack l 3 : push 3 onto number stack l + : because + has lower precedence than pop 3, 2, and compute 2 3=6, and push 6 onto number stack push + onto operation stack l 4 : push 4 onto number stack l expression end : pop 4, 6 and +, compute 6+4=10, push 10; pop 10, 1, and +, compute 1+10=11, push 11 l 11 (at the top of the stack) is the answer 18
Queues: Overview l The concept of a queue l Main operations on a queue: - enqueue, dequeue l Implementation issues 19
What is a queue? l A queue is a linear data structure that stores a set of elements in a particular order: It realizes a FIFO (First In First Out) interface l Real life examples: Check-out in a supermarket Printing with network printers 20
Main operations on a queue l enqueue to add a new item at the rear l dequeue to remove an item from the front l queueempty to check whether the queue is empty or not l queuefull to check whether the queue is full or not l queuesize returns the number of items in the queue l queuepeek returns the front item C B A rear front 21
l Array-based queue implementation: Implementation D C B A Max_Size rear front After dequeue(a): n-1 3 2 n-1 3 2 D C B 1 0 1 0 Max_Size rear front l Problem: Once rear is at the end of this array, and there is a new item coming in, what can we do? 22
Implementation (cont-d) l A simple and elegant solution is to wrap the array around to create a circular array (queue): n-1 3 2 1 0 C B A front=0 1 0 A 2 B C 3 rear=3 rear=3 front=0 n-1 l Circular array with size n can at most hold n - 1 items 23
Circular queue operations enqueue rear = (rear + 1) MOD n dequeue front = (front + 1) MOD n 1 2 B C 3 rear=3 front=1 1 2 B C 3 0 A 0 A n-1 n-1 l l If (rear == front) then queueempty=true If ((rear + 1) MOD n == front) then queuefull=true 24
Implementing a queue using enqueue(2, 8, 4, 7) a linked list rear front dequeue rear front enqueue(3) rear front 25
Queue application example l Check-out line using a queue: 26
Summary l We have briefly surveyed three types of basic data structures singly and doubly linked lists stacks (FILO) queues (FIFO) l You need to clearly understand the concepts of these data structures, and also to be able to give examples of stack and queue applications 27
l Read these slides again l Receive self-preparation assignments and study them thoroughly Homework l Learn the English terms new for you 28
Next class l Applications of basic data structures - basics of Graph Theory - binary trees - (data) networks 29