Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Applications Hao Jiang Computer Science Department Boston College.

Similar presentations


Presentation on theme: "Data Structures and Applications Hao Jiang Computer Science Department Boston College."— Presentation transcript:

1 Data Structures and Applications Hao Jiang Computer Science Department Boston College

2 Linked List 1234 null head class Node { public int d; public Node next; public Node(int d) { this.d = d; next = null; } … Node head; Node v1 = new Node(1); Node v2 = new Node(2); Node v3 = new Node(3); Node v4 = new Node(4); head = v1; v1.next = v2; v2.next = v3; v3.next = v4; v4.next = null;

3 Some Properties 1234 null head A list is dynamic: the length of a list is determined in the runtime. Some operations such as insertion and deletion are cheaper than the data structure array.

4 Insert 1234 null head 100 Insert a new node at position 1.

5 Insert in the Front 1234 null head 100 Insert a new node at position 0.

6 Insert at the End 1234 head 100 null Insert a new node at the end.

7 Delete 1234 null head Delete a new node at position 1.

8 Delete from the Front Delete a node at position 0. 1234 null head

9 Delete the Last Node Delete the last node in the list. 123 null 4 head

10 A List of Objects obj1obj2obj3obj4 null head class Node { public Object d; public Node next; public Node(Object d) { this.d = d; next = null; }

11 A More Accurate Illustration null head obj1obj2obj3obj4 A list of objects.

12 List Class public class List { private Node head; public List() { head = null; } public boolean isEmpty() { return (head == null); } public int length(Node h) { if (h == null) return 0; return 1 + length(h.next); } public int length() { return length(head); } … public void append() {…} public void insert(int n, Object d) {…} public Object delete(int n) {…} public void print() {…} … }

13 Queue A queue is a first-in-first-out linear data structure. It supports two operations: –void enQueue(Object d) –Object deQueue() Data In Data Out

14 Stack A stack is a first-in-last-out linear data structure. It supports two operations: –void push(Object d) –Object pop() top of the stack bottom of the stack

15 Queue based on List public class Queue { private List list; public Queue() { list = new List(); } public void enQueue(Object d) { list.append(d); } public Object deQueue() { Object t = list.getHead(); list.delete(0); return t.d; }

16 Stack based on List public class Stack { private List list; public Stack() { list = new List(); } public void push(Object d) { list.insert(0, d); } public Object pop() { Node t = list.getHead(); list.delete(0); return t.d; }

17 Simulation Application of the Queue Gas Station car1car2car3car4 How long does everyone have to wait? How long can the line be? Top of the lineEnd of the line

18 Language Parser We can use stacks to make language parsers. ( ( 1 + 2 ) / 3 ) = ? 2121 + 3 3333 / 1 ( 1 + 2 ) / 3 ) The result is 1.


Download ppt "Data Structures and Applications Hao Jiang Computer Science Department Boston College."

Similar presentations


Ads by Google