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
Circular List: The tail of the list points
back to the head
There is no NULL pointer to “end” the
list.
62 84
10. 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
11. 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;
};
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)
{
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
16. 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
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 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
19. 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
20. Same basic functions operate on list
Insertleft()
Insertright()
Delete()
deleteleft()
deleteright()
We have
left(right(p)) =p = right(left(p))
Doubly-Linked Lists
21. 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;
};
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;
25. 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;
}
26. 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;
}