Linked Lists
Linked Stacks and Queues
front rear
top
0
0
Linked
Stack
Linked
Queue
Template of Queues
class Queue;
class Node {
friend class Queue;
private:
int info;
Node *link;
};
class Queue {
public:
Queue() {front = rear = NULL;};
void Add(int);
int Delete();
bool Empty();
private:
Node *front, *rear;
};
Queue Operations
front
×rear
Queue::Queue()
{
first = rear = NULL;
};
bool Empty()
{
if ( front==NULL)
return TRUE;
else
return FALSE;
}
Queue Operations
int Queue::Delete()
{
if (Empty())
{
cout <<“Queue underflow”;
exit(1);
}
Node *temp = front;
int x = temp->info;
front = temp->link;
if (front== NULL)
rear = NULL;
delete temp;
return x;
}
Queue Operations
front
rear
××
temp
642 8
void Queue::Add(int x)
{
Node *temp = new Node;
temp->info=x;
temp->link = NULL;
if (rear== NULL)
front = temp;
else
rear->link = temp;
rear = temp;
}
Queue Operations
front
rear
×
temp
642 8 ×10×
Various Linked Lists
 Adding an element in the beginning of
the list, or the end of the list
 Chains, circular lists
 Singly linked lists, doubly linked lists
 Etc.
Circular List
 Circular List: The tail of the list points
back to the head
 There is no NULL pointer to “end” the
list.
62 84
Circular List
 There is no first and last node in circular list so
we establish a first, last node by convention
First Node Last Node Last
 An external pointer is used to point to last
node.
62 84
Stacks as a Circular List
Let last be the pointer pointing to the last node in the stack
and first node be considered as top of the stack.
Implementation of Stack
class Stack;
class Node {
friend class Stack;
private:
int info;
Node *link;
};
class Stack {
public:
Stack() {last = NULL;};
void Push(int);
int Pop();
bool Empty();
private:
Node *last;
};
bool Stack::Empty()
{
if (last == NULL)
return TRUE;
else
return FALSE:
}
Stack Operations
void Stack::Push(int x)
{
Node * temp = new Node;
temp->info = x;
if (last == NULL)
last = temp;
else
temp->link = last->link;
last->link = temp;
}
PUSH
642
last
1
temp
int Stackint::Pop()
{
int x; Node *temp;
if (Empty()) {cout << “Empty Stack”; exit(1);}
temp = last->link;
x = temp->info;
if (temp == last)
last = NULL;
else
last->link = temp->link;
delete temp;
return x;
}
POP
642
last
1
temp
void Queue::Add(int x)
{
int x; Node *temp= new Node;
temp->info = x;
if (Empty())
last = temp;
else
temp->link = last->link;
last->link = temp;
last = temp;
}
Queue As Circular List
642
last
1
temp
8
void Queue::DelAfter(Node *p)
{
int x; Node *temp;
if (p==NULL || p==p->link)
{
cout << “void deletion”; return;
}
temp = p->link;
x = p->info;
p->link = temp->link;
delete temp;
}
DELAFTER()
642
last
1
temp
8
p
read n;
read(name);
while (name != END)
{
insert name on the circular list;
read(name);
}
while (there is more than one node on the list)
{
count through n-1 nodes on the list;
print the name in the nth node;
delete nth node
}
Print the name of the only node on the list
Josephus Problem
 List can not be traversed backward.
 A node can not be deleted from it,
given only a pointer to that node.
Solution is to use a Doubly Linked
List
Shortcomings of circular list
Doubly-Linked Lists
 Each node contains two pointers, one to its successor
and one to its predecessor.
 Doubly linked list can be linear or circular.
88NULL 42 109 NULL
head
left info right
88 42 109
Same basic functions operate on list
 Insertleft()
 Insertright()
 Delete()
 deleteleft()
 deleteright()
We have
left(right(p)) =p = right(left(p))
Doubly-Linked Lists
Doubly Link List
Let first be the pointer pointing to the first node in the
Doubly.
Implementation of Doubly
class Doubly;
class Node {
friend class Doubly;
private:
int info;
Node *left, *right;
};
class Doubly {
public:
Doubly() {first = NULL;};
-----------
private:
Node *first;
};
Delete
int Doubly::Delete(Node *p)
{
Node *q, *r; int x;
if (p==NULL)
{ cout << “void deletion”; return;}
x = p->info;
q = p->left;
r = p->right;
q->right = r;
r->left = q;
delete p;
DeleteLeft
void Doubly:: DelRight(Node *p)
{
Node *q, *temp;
int x;
r = p->right;
temp = r->right;
p->right = temp;
if (temp!=NULL)
temp->left = p;
cout << r->info;
delete r;
}
Insertright
void Doubly:: insertright(Node *p, int x)
{
Node *q, *r;
if (p==NULL)
{ cout << “void insertion”; return;}
q = new Node;
q->info = x;
r = p->right;
r->left = q;
q->right = r;
q->left = p;
p->right = q;
Insert on head side
void Doubly:: Insert(int x)
{
Node *q, *r;
r = first;
q = new Node;
q->info = x;
q->right = r;
q->left = NULL;
r->left = q;
first = q;
}
Deletion on head side
void Doubly:: Delete(int v)
{
Node *q, *r;
int x;
r = first;
q= r->right;
q->left = NULL;
r->right = NULL;
x = r->info;
first = q;
delete r;
}

Link list part 2

  • 1.
  • 2.
    Linked Stacks andQueues front rear top 0 0 Linked Stack Linked Queue
  • 3.
    Template of Queues classQueue; class Node { friend class Queue; private: int info; Node *link; }; class Queue { public: Queue() {front = rear = NULL;}; void Add(int); int Delete(); bool Empty(); private: Node *front, *rear; };
  • 4.
  • 5.
    bool Empty() { if (front==NULL) return TRUE; else return FALSE; } Queue Operations
  • 6.
    int Queue::Delete() { if (Empty()) { cout<<“Queue underflow”; exit(1); } Node *temp = front; int x = temp->info; front = temp->link; if (front== NULL) rear = NULL; delete temp; return x; } Queue Operations front rear ×× temp 642 8
  • 7.
    void Queue::Add(int x) { Node*temp = new Node; temp->info=x; temp->link = NULL; if (rear== NULL) front = temp; else rear->link = temp; rear = temp; } Queue Operations front rear × temp 642 8 ×10×
  • 8.
    Various Linked Lists Adding an element in the beginning of the list, or the end of the list  Chains, circular lists  Singly linked lists, doubly linked lists  Etc.
  • 9.
    Circular List  CircularList: The tail of the list points back to the head  There is no NULL pointer to “end” the list. 62 84
  • 10.
    Circular List  Thereis no first and last node in circular list so we establish a first, last node by convention First Node Last Node Last  An external pointer is used to point to last node. 62 84
  • 11.
    Stacks as aCircular List Let last be the pointer pointing to the last node in the stack and first node be considered as top of the stack. Implementation of Stack class Stack; class Node { friend class Stack; private: int info; Node *link; }; class Stack { public: Stack() {last = NULL;}; void Push(int); int Pop(); bool Empty(); private: Node *last; };
  • 12.
    bool Stack::Empty() { if (last== NULL) return TRUE; else return FALSE: } Stack Operations
  • 13.
    void Stack::Push(int x) { Node* temp = new Node; temp->info = x; if (last == NULL) last = temp; else temp->link = last->link; last->link = temp; } PUSH 642 last 1 temp
  • 14.
    int Stackint::Pop() { int x;Node *temp; if (Empty()) {cout << “Empty Stack”; exit(1);} temp = last->link; x = temp->info; if (temp == last) last = NULL; else last->link = temp->link; delete temp; return x; } POP 642 last 1 temp
  • 15.
    void Queue::Add(int x) { intx; Node *temp= new Node; temp->info = x; if (Empty()) last = temp; else temp->link = last->link; last->link = temp; last = temp; } Queue As Circular List 642 last 1 temp 8
  • 16.
    void Queue::DelAfter(Node *p) { intx; Node *temp; if (p==NULL || p==p->link) { cout << “void deletion”; return; } temp = p->link; x = p->info; p->link = temp->link; delete temp; } DELAFTER() 642 last 1 temp 8 p
  • 17.
    read n; read(name); while (name!= END) { insert name on the circular list; read(name); } while (there is more than one node on the list) { count through n-1 nodes on the list; print the name in the nth node; delete nth node } Print the name of the only node on the list Josephus Problem
  • 18.
     List cannot be traversed backward.  A node can not be deleted from it, given only a pointer to that node. Solution is to use a Doubly Linked List Shortcomings of circular list
  • 19.
    Doubly-Linked Lists  Eachnode contains two pointers, one to its successor and one to its predecessor.  Doubly linked list can be linear or circular. 88NULL 42 109 NULL head left info right 88 42 109
  • 20.
    Same basic functionsoperate on list  Insertleft()  Insertright()  Delete()  deleteleft()  deleteright() We have left(right(p)) =p = right(left(p)) Doubly-Linked Lists
  • 21.
    Doubly Link List Letfirst be the pointer pointing to the first node in the Doubly. Implementation of Doubly class Doubly; class Node { friend class Doubly; private: int info; Node *left, *right; }; class Doubly { public: Doubly() {first = NULL;}; ----------- private: Node *first; };
  • 22.
    Delete int Doubly::Delete(Node *p) { Node*q, *r; int x; if (p==NULL) { cout << “void deletion”; return;} x = p->info; q = p->left; r = p->right; q->right = r; r->left = q; delete p;
  • 23.
    DeleteLeft void Doubly:: DelRight(Node*p) { Node *q, *temp; int x; r = p->right; temp = r->right; p->right = temp; if (temp!=NULL) temp->left = p; cout << r->info; delete r; }
  • 24.
    Insertright void Doubly:: insertright(Node*p, int x) { Node *q, *r; if (p==NULL) { cout << “void insertion”; return;} q = new Node; q->info = x; r = p->right; r->left = q; q->right = r; q->left = p; p->right = q;
  • 25.
    Insert on headside void Doubly:: Insert(int x) { Node *q, *r; r = first; q = new Node; q->info = x; q->right = r; q->left = NULL; r->left = q; first = q; }
  • 26.
    Deletion on headside void Doubly:: Delete(int v) { Node *q, *r; int x; r = first; q= r->right; q->left = NULL; r->right = NULL; x = r->info; first = q; delete r; }