Vous êtes sur la page 1sur 10

The Linked List Data Structure

Mugurel Ionu Andreica Spring 2012

The Structure of a Linked List


The contents of every element of the list
info: useful information next: pointer to the next element of the list prev: pointer to the previous element of the list
next next next next NULL

NULL

info prev prev

info prev

info prev

info

Operations
addFirst(x)
Adds an element with info=x at the beginning of the linked list

addLast(x)
Adds an element with info=x at the end of the linked list

removeFirst()
Removes the first element of the linked list

removeLast()
Removes the first element of the linked list

findFirstOccurrence(x)
Returns a pointer to the first element of the linked list for which info==x

findLastOccurrence()
Returns a pointer to the last element of the linked list for which info==x

removeFirstOccurrence(x)
Removes the first element of the linked list whose info field is equal to x

removeLastOccurrence(x)
Removes the last element of the linked list whose info field is equal to x

isEmpty()
Returns 1 if the linked list is empty and 0 otherwise

Linked List Implementation (linked_list.h)


#include <stdio.h> #include <stdlib.h> template<typename T> struct list_elem { T info; struct list_elem<T> *next, *prev; }; template <typename T> class LinkedList { public: struct list_elem<T> *pfirst, *plast; void addFirst(T x) { struct list_elem<T> *paux; paux = new struct list_elem<T>; paux->info = x; paux->prev = NULL; paux->next = pfirst; if (pfirst != NULL) pfirst->prev = paux; pfirst = paux; if (plast==NULL) plast=pfirst; } } void removeFirst() { struct list_elem<T>* paux; if (pfirst != NULL) { paux = pfirst->next; if (pfirst == plast) plast = NULL; delete pfirst; pfirst = paux; if (pfirst != NULL) pfirst->prev = NULL; } else fprintf(stderr, Error 101 - The list is empty\n); } void addLast(T x) { struct list_elem<T> *paux; paux = new struct list_elem<T>; paux->info = x; paux->prev = plast; paux->next = NULL; if (plast != NULL) plast->next = paux; plast = paux; if (pfirst == NULL) pfirst = plast;

Linked List Implementation (linked_list.h) (cont.)


void removeLast() { struct list_elem<T> *paux; if (plast != NULL) { paux = plast->prev; if (pfirst == plast) pfirst = NULL; delete plast; plast = paux; if (plast != NULL) plast->next = NULL; } else fprintf(stderr, Error 102 - The list is empty\n); } struct list_elem<T>* findFirstOccurrence(T x) { struct list_elem<T> *paux; paux = pfirst; while (paux != NULL) { if (paux->info == x) return paux; paux = paux->next; } return NULL; } } int isEmpty() { return (pfirst == NULL); } LinkedList() { pfirst = plast = NULL; } struct list_elem<T>* findLastOccurrence(T x) { struct list_elem<T> *paux; paux = plast; while (paux != NULL) { if (paux->info == x) return paux; paux = paux->prev; } return NULL;

Linked List Implementation (linked_list.h) (cont.)


void removeFirstOccurrence(T x) { struct list_elem<T> *px; px = findFirstOccurrence(x); if (px != NULL) { if (px->prev != NULL) px->prev->next = px->next; if (px->next != NULL) px->next->prev = px->prev; if (px->prev == NULL) // px == pfirst pfirst = px->next; if (px->next == NULL) // px == plast plast = px->prev; delete px; } } } } } void removeLastOccurrence(T x) { struct list_elem<T> *px; px = findLastOccurrence(x); if (px != NULL) { if (px->prev != NULL) px->prev->next = px->next; if (px->next != NULL) px->next->prev = px->prev; if (px->prev == NULL) // px == pfirst pfirst = px->next; if (px->next == NULL) // px == plast plast = px->prev; delete px;

Using a Linked List (very) short example


#include linked_list.h LinkedList<int> ll; void printList() { struct list_elem<int> *p; p = ll.pfirst; while (p != NULL) { printf("%d\n", p->info); p = p->next; } } int main() { ll.addFirst(89); ll.addFirst(34); ll.addLast(79); ll.addLast(34); ll.addFirst(89); printList(); ll.removeLastOccurrence(89); printList(); ll.removeLast(); ll.removeFirst(); printList(); return 0; }

Maintaining a Sorted Linked List


template <typename T> class SortedLinkedList : public LinkedList<T> { public: void addElement(T x) { struct list_elem<T> *p, *paux; p = pfirst; while (p != NULL && p->info < x) p = p->next; if (p == NULL) addLast(x); else { paux = new struct list_elem<T>; paux->info = x; paux->next = p; paux->prev = p->prev; p->prev = paux; if (p->prev != NULL) p->prev->next = paux; else pfirst = paux; } } SortedLinkedList() : LinkedList<T>() {} }; SortedLinkedList<int> ll; void printList() { struct list_elem<int> *p; p = ll.pfirst; while (p != NULL) { printf("%d\n", p->info); p = p->next; } } int main() { ll.addElement(7); ll.addElement(20); ll.addElement(3); ll.addElement(45); ll.addElement(22); ll.addElement(12); printList(); return 0; }

Implementing a Stack using a Linked List


template<typename T> class Stack { private: LinkedList<T> ll; public: void push(T x) { ll.addLast(x); } T pop() { if (isEmpty()) { fprintf(stderr, "Error 102 - The stack is empty!\n"); T x; return x; } T x = ll.plast->info; ll.removeLast(); return x; } T peek() { if (isEmpty()) { fprintf(stderr, "Error 103 - The stack is empty!\n"); T x; return x; } return ll.plast->info; } int isEmpty() { return (ll.isEmpty()); } };

Implementing a Queue using a Linked List


template<typename T> class Queue { private: LinkedList<T> ll; public: void enqueue(T x) { ll.addLast(x); } T dequeue() { if (isEmpty()) { fprintf(stderr, "Error 102 - The queue is empty!\n"); T x; return x; } T x = ll.pfirst->info; ll.removeFirst(); return x; } T peek() { if (isEmpty()) { fprintf(stderr, "Error 103 - The queue is empty!\n"); T x; return x; } return ll.pfirst->info; } int isEmpty() { return (ll.isEmpty()); } };

Vous aimerez peut-être aussi