Lists, Stacks and Queues


Objectives:


Lists


The List ADT

Type: a collection of lists
Operations:
length(e1,e2,...,en) = n
return(k,(e1,e2,...,ek,...,en))=ek
insert(x,k,(e1,e2,...,ek-1,ek,...,en))=(e1,e2,...,ek-1,x,ek,...,en)
remove(k,(e1,e2,...,ek,...,en))=(e1,e2,...,ek-1,ek+1,...,en)
find(x,(e1,e2,...,ek-1,x,ek+1,...,en))=k (k - first occurrence)
find(x,(e1,e2,...,en))=0 (x - not in the list)


Array-Based Implementation

i := 0
while (x <> a[i])
    i := i +1
return i;


C++ class definition

#define Element int
class List{
private:
    int max_size;
    int actual_size;
    Element* list_array;
public:
    List(const int);
    ~List();
    length();
    Element& return_nth(const int);
    void insert(const Element&, const int);
    void remove(const int);
    int find(const Element&);
};
List::List(const int size){
    max_size = size;
    actual_size = 0;
    list_array = new Element[size];
    return;
}
 
List::~List(){
    delete []list_array;
    return;
}
int List::length(){
    return actual_size;
}
Element& List::return_nth(const int k){
    return list_array[k];
}
void List::insert(const Element& e,const int k){
    register int k_new;
    if (actual_size == max_size)
        return;
    else{
        actual_size++;
        k_new = (k < 0) ? 0 : k;
        for (int i = actual_size-1 ; i > k ; i--)
            list_array[i] = list_array[i-1];
        list_array[k] = e;
    return;
    }
}
void List::remove(const int k){
    if (actual_size == 0)
        return;
    else{
        actual_size--;
        for (int i = k ; i < actual_size - 1 ; i++)
        list_array[i] = list_array[i+1];
        return;
    }
}
int List::find(const Element& e){
    register int i;
    for (i = 0; (i<actual_size)&&(list_array[i] != e); i++);
    if (list_array[i] == e)
        return i;
    else
        return -1;
}


Linked Lists

current := head
i := 1
while (current.value <> x) 
    current := current.next 
    i := i + 1 
end (while)
return i;


C++ class definition

class Node{
public:
    Element value; 
    Node *next; 
    Node(const Element& e, Node* l = NULL) 
        {value = e; next = l;} 
    ~Node() {}; 
};
class List{
private:
    int actual_size; 
    Node* head; 
public: 
    List(); 
    ~List(); 
    length(); 
    Element& return_nth(const int); 
    void insert(const Element&, const int); 
    void remove(const int); 
    int find(const Element&); 
};
List::List(){
    actual_size = 0; 
    head = NULL; 
    return; 
}
List::~List(){
    for (Node *current = head; head != NULL; delete current)
        head = head -> next;
    return;
}
int List::length(){
    return actual_size;
}
Element& List::return_nth(const int k){
    Node* current = head;
    for (int i = 0 ; i != k ; i++)
        current = current -> next;
    return current-> value;
}
void List::insert(const Element& e,const int k){
    Node* current = head; 
    actual_size++; 
    if (k <= 0){ 
        head = new Node(e,head); 
        return; 
    } 
    else{ 
        for (int i = 1 ;(i<k) && current->next!=NULL); i++) 
            current = current->next; 
        current->next = new Node(e,current->next);
        eturn; 
    }
}
void List::remove(const int k){
    Node* current = head;
    Node* temp;
    if ((k < 0) || (actual_size <= k))
        return;
    else{
        actual_size--;
        if (k == 0){
            temp = head;
            head = head->next;
        }
        else{
            for (int i = 1 ; i < k ; i++)
                current = current->next;
            temp = current->next;
            current->next = (current->next)->next;
        }
    }
    delete temp;
    return;
}
int List::find(const Element& e){
    Node* current = head;
    register int i; 
    for(i=0;(current->value!=e)&&(current->next!=NULL);i++)
        current = current->next; 
    if (current->value == e) 
        return i; 
    else 
        return -1; 
}
 


Comparison of list implementations

From the point of view of space required array-based lists are better when the size of the list is approximately known in advance
Linked lists are preferred to array- based lists from the point of view of time required


Implementing elements


Doubly linked lists


Stacks

A stack is a list in which elements may be inserted or removed from only one end.
push for insertion
top for access
pop for access & remove


Queues

A queue is a list in which an element may only be inserted at the end and may only be accessed and removed at the head
enqueue for inserting
dequeue for access & remove