0% found this document useful (0 votes)
8 views28 pages

L8,9 List Stack Queue2

data structure lab note

Uploaded by

양산일
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views28 pages

L8,9 List Stack Queue2

data structure lab note

Uploaded by

양산일
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Data Structure

Lists, Stacks, and Queues 2 (Chapter 4)

U Kang
Seoul National University

U Kang 1
In This Lecture
 Learn the motivation and main idea of doubly
linked list

 Learn the Stack and Queue data structure

 Learn the Dictionary data structure

U Kang 2
Limitation of Linked List

 What is the time complexity of prev() in Linked List?

 How can we make prev() fast?

U Kang 3
Limitation of Linked List

 What is the time complexity of prev() in Linked List?

 How can we make prev() fast?


 Idea: doubly linked list!

U Kang 4
Doubly Linked Lists
class DLink<E> {
private E element;
private DLink<E> next;
private DLink<E> prev;
DLink(E it, DLink<E> p, DLink<E> n)
{ element = it; prev = p; next = n; }
DLink(DLink<E> p, DLink<E> n)
{ prev = p; next = n; }
DLink<E> next() { return next; }
DLink<E> setNext(DLink<E> nextval)
{ return next = nextval; }
DLink<E> prev() { return prev; }
DLink<E> setPrev(DLink<E> prevval)
{ return prev = prevval; }
E element() { return element; }
E setElement(E it) { return element = it; }
} U Kang 5
Doubly Linked Lists

U Kang 6
Doubly Linked Insert

U Kang 7
Doubly Linked Insert
public void 4 insert(E it) {
curr.setNext( 3 2
1 new DLink<E>(it, curr, curr.next()));
curr.next().next().setPrev(curr.next());
cnt++; 5
}

U Kang 8
Doubly Linked Remove

U Kang 9
Doubly Linked Remove
public E remove() {
if (curr.next() == tail) return null;
E it = curr.next().element();
curr.next().next().setPrev(curr); 1
curr.setNext(curr.next().next()); 2
cnt--;
return it;
}

U Kang 10
Effect of Fixed Head & Tail

 Doubly Linked List has a fixed tail node


 Reason: no need to consider special cases

 Insert() with fixed Tail node


public void insert(E it) {
curr.setNext(new DLink<E>(it, curr, curr.next()));
curr.next().next().setPrev(curr.next());
cnt++;
}

 Insert() without fixed Tail node


public void insert(E it) {
curr.setNext(new DLink<E>(it, curr, curr.next()));
if (curr.next().next() != null)
curr.next().next().setPrev(curr.next());
if (tail == curr) tail = curr.next();
cnt++;
} U Kang 11
Effect of Fixed Head & Tail
 Remove() with fixed Tail node
public E remove() {
if (curr.next() == tail) return null;
E it = curr.next().element();
curr.next().next().setPrev(curr);
curr.setNext(curr.next().next());
cnt--;
return it;
}
 Remove() without fixed Tail node
public E remove() {
if (curr.next() == null) return null;
E it = curr.next().element();
if (tail == curr.next()) tail = curr;
if (curr.next().next() != null)
curr.next().next().setPrev(curr);
curr.setNext(curr.next().next());
cnt--;
return it;
}
U Kang 12
Stacks
 LIFO: Last In, First Out.

 Restricted form of list: Insert and remove only at


front of list.
 Notation:
 PUSH for insertion
 POP for removal
 TOP for returning the
accessible element

<Dun Briste> in Ireland


U Kang 13
Stack ADT
public interface Stack<E> {
/** Reinitialize the stack. */
public void clear();

/** Push an element onto the top of the stack.


@param it Element being pushed onto the stack.*/
public void push(E it);

/** Remove and return top element.


@return The element at the top of the stack.*/
public E pop();

/** @return A copy of the top element. */


public E topValue();

/** @return Number of elements in the stack. */


public int length();
};
U Kang 14
Array-Based Stack
// Array-based stack implementation
private int maxSize; // Max size of stack
private int top; // Index for top
private E [] listArray;

 Issues:
 Which end is the top?
 Where does “top” point to?
 What are the costs of the operations?
 PUSH, POP, and TOP

U Kang 15
Linked Stack
class LStack<E> implements Stack<E> {
private Link<E> top;
private int size;
}

 What are the costs of the operations?

 What is the space requirement compared to that of


the array-based stack implementation?

U Kang 16
Queues
 FIFO: First in, First Out

 Restricted form of list: Insert at one end, remove


from the other.
 Notation:
 Enqueue for insertion
 Dequeue for deletion
 Front for accessing first element
 Rear for accessing last element

U Kang 17
Queue Implementation (1)

U Kang 18
Queue Implementation (2)

“Circular” Queue
U Kang 19
Dictionary

 Often want to insert records, delete records, and


search for records.

 Required concepts:
 Search key: describe what we are looking for
 E.g., for records consisting of (country name, population, # of
visitors), a search key is ‘country name’
 Key comparison
 Equality: while doing sequential search

 Relative order: after sorting

U Kang 20
Records and Keys

 Problem: How do we extract the key from a


record?
 Records can have multiple keys.
 Fundamentally, the key is not a property of the
record, but of the context.
 Solution: We will explicitly store the key with the
record.
 Thus we have (key, record) pair
 This is also called (key, value) pair

U Kang 21
Dictionary ADT
public interface Dictionary<Key, E> {
public void clear();
public void insert(Key k, E e);
public E remove(Key k); // Null if none
public E removeAny(); // Null if none
public E find(Key k); // Null if none
public int size();
};

U Kang 22
Payroll Class
// Simple payroll entry: ID, name, address
class Payroll {
private Integer ID;
private String name;
private String address;
Payroll(int inID, String inname, String inaddr)
{
ID = inID;
name = inname;
address = inaddr;
}
public Integer getID() { return ID; }
public String getname() { return name; }
public String getaddr() { return address; }
}
U Kang 23
Using Dictionary
// IDdict organizes Payroll records by ID
Dictionary<Integer, Payroll> IDdict =
new UALdictionary<Integer, Payroll>();
// namedict organizes Payroll records by name
Dictionary<String, Payroll> namedict =
new UALdictionary<String, Payroll>();
Payroll foo1 = new Payroll(5, "Joe", "Anytown");
Payroll foo2 = new Payroll(10, "John", "Mytown");
IDdict.insert(foo1.getID(), foo1);
IDdict.insert(foo2.getID(), foo2);
namedict.insert(foo1.getname(), foo1);
namedict.insert(foo2.getname(), foo2);
Payroll findfoo1 = IDdict.find(5);
Payroll findfoo2 = namedict.find("John");

U Kang 24
Unsorted List Dictionary
class UALdictionary<Key, E>
implements Dictionary<Key, E> {
private static final int defaultSize = 10;
private AList<KVpair<Key, E>> list;
// Constructors
UALdictionary() { this(defaultSize); }
UALdictionary(int sz)
{ list = new AList<KVpair<Key, E>>(sz); }
public void clear() { list.clear(); }
/** Insert an element: append to list */
public void insert(Key k, E e) {
KVpair<Key,E> temp = new KVpair<Key,E>(k, e);
list.append(temp);
} U Kang 25
Sorted vs. Unsorted Array List
Dictionaries
 If the list is sorted
 Could use binary search to speed up search
 Would need to insert in order, slowing insert
 Which is better?
 If lots of searches, sorted list is good
 If insertions are much more frequent, then sorting has
no benefit.

U Kang 26
What You Need to Know
 Motivation and main idea of doubly linked list
 Doubly linked list vs. singly linked list

 Stack and Queue data structure


 Array based stack vs. link based stack
 How to implement circular queue using array

 Dictionary data structure


 Sorted vs. unsorted array list dictionary

U Kang 27
Questions?

U Kang 28

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy