0% found this document useful (0 votes)
64 views

Linklist 1

Linked lists are data structures that store data in nodes that are connected to each other via links. There are several types of linked lists including singly linked lists, doubly linked lists, and circular linked lists. Singly linked lists have nodes that contain a data field and a link to the next node. Doubly linked lists have nodes with links to both the next and previous nodes. Circular linked lists form a closed loop with the last node linking back to the first node.

Uploaded by

Farooq Shad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Linklist 1

Linked lists are data structures that store data in nodes that are connected to each other via links. There are several types of linked lists including singly linked lists, doubly linked lists, and circular linked lists. Singly linked lists have nodes that contain a data field and a link to the next node. Doubly linked lists have nodes with links to both the next and previous nodes. Circular linked lists form a closed loop with the last node linking back to the first node.

Uploaded by

Farooq Shad
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Linked Lists

Structure for Linked List:


We have already used a simple node definition:

struct Node { int data; Node *link; };

Insert at end
Inserts an element at the end of the list void insertend(int x) { Node *temp = head; while (temp -> link != NULL) temp = temp->link; Node *newer = new Node; newer->data = x; newer->link = NULL; temp->link = newer; } head 12 10

temp

newer

14

16

Delete a Node after a Node


Delete an element after the specific node int delafter(Node *temp) { Node *temp1 = temp->next; int x = temp->data; temp = temp1->link; delete temp1; return x; temp temp1 } head

10

12

14

16

Assignment
Merge two lists. Delete all occurrences of a number from the list.

Stacks As Singly Linked List


We can implement a stack with a singly linked list
The head pointer points to the first node. Both insertion and deletion are done from head side.
Stacks

We had already done.


nodes

head

elements

Queue with a Singly Linked List


We can implement a queue with a singly linked list
The front element is stored at the first node The rear element is stored at the last node
Stacks 7

rear
nodes

front

elements

Queue Operations
struct Queue
{ int info; Queue *link; }; Queue *front, *rear; rear = front = NULL;

front

rear

Queue Operations
bool empty()
{ if ( front==NULL) return TRUE; else return FALSE; }

Queue Operations
int remove()
{
if (empty()) { cout <<Queue underflow; exit(1); } Queue *temp = front; int x = front->info; front = front->link; if (front== NULL) rear = NULL; delete temp; return x;

rear
6 8

front temp

Queue Operations
Void insert(int x)
{
Queue *temp = new Queue; temp->info=x; temp->link = NULL; if (rear== NULL) front = temp; else rear->link = temp; rear = temp;

rear

temp

front

10

Shortcomings of linked linear list


We can not reach any of the node that precede a node p. If a list is traversed the external pointer is needed to be preserved to be able to refer the list again. Solution is to use a Circular List

Circular List
Circular List: The tail of the list points back to the head

There is no NULL pointer to end the list. 4


2 6

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
2

Last
4 6
8

An external pointer is used to point to last node.

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
struct Node { int info; Node *link; }; Node *last; last = NULL;

Stack Operations
bool empty() { if (last == NULL) return TRUE; else return FALSE: }

PUSH
void push(int x)
{ Node * temp = new Node; temp->info = x; if (last == NULL) last = temp; else temp->link = last->link; } last->link = temp; }

last

temp

POP
int 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; }

last

1 temp

Queue As Circular List


void insert(int x)
{ int x; Node *temp= new node; temp->info = x; if (empty()) last = temp; else temp->link = last->link; last->link = temp->info; last = temp; }

last

temp

DELAFTER()
void 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->next; p delete temp;

temp

last

Josephus Problem
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

Shortcomings of circular list


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

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.
left
NULL

info

right

88

42

109

NULL

head 88 42 109

Doubly-Linked on list Lists Same basic functions operate


Insertleft() Insertright() Delete() deleteleft() deleteright() We have left(right(p)) =p = right(left(p))

Doubly link list can be implemented using lists. Each node contains left, right pointers and info part. struct node { int info; node *left, *right; };

Implementation of DoublyLinked Lists

Delete
int 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; }

Insertright
void 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 insert(Node *head, int x) { Node *q, *r; r = head; q = new node; q->info = x; q->right = r; q->left = NULL; r->left = q; head = q; }

Deletion on head side


int insert(Node *head) { Node *q, *r; int x; r = head; q= r->right; q->left = NULL; r->right = NULL; x = r->info; head = q; delete r; }

You might also like