0% found this document useful (0 votes)
70 views71 pages

Linked Lists

The document describes various operations that can be performed on linked lists including implementing a queue using a linked list. Key points: - A queue can be implemented using a linked list by adding nodes to the rear/end and removing from the front. - Common queue operations like enqueue and dequeue can be performed by inserting nodes at the rear and deleting nodes from the front of the linked list. - Other linked list operations described are inserting a node at the end/rear of list, deleting the front/first node, and deleting the last node. - Examples of implementing stacks, reversing a linked list, inserting in a sorted linked list, and deleting a specific value from a sorted linked list are also provided. P

Uploaded by

Shobhit Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views71 pages

Linked Lists

The document describes various operations that can be performed on linked lists including implementing a queue using a linked list. Key points: - A queue can be implemented using a linked list by adding nodes to the rear/end and removing from the front. - Common queue operations like enqueue and dequeue can be performed by inserting nodes at the rear and deleting nodes from the front of the linked list. - Other linked list operations described are inserting a node at the end/rear of list, deleting the front/first node, and deleting the last node. - Examples of implementing stacks, reversing a linked list, inserting in a sorted linked list, and deleting a specific value from a sorted linked list are also provided. P

Uploaded by

Shobhit Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Applications of linked lists

Queue
A queue can be implemented as a linked list
A new member joins at the end
Member at the front gets serviced

Q- front

NULL

NULL

enqueue
A new member joins the queue
Joining is always at the end of the queue
Linked list: (Insert a node at the rear of list)

front
NULL

rear

dequeue
A member gets serviced and leaves the queue
It is always the member at the front of the queue
Linked list: ( Delete a node at front of the list )

front
NULL

gone

Queue operations
Insert node at rear of the list
Delete a node from front of the list

INSERT A NODE AT REAR OF A LIST

Function to insert a node at rear of the list


1. Function accepts pointer to the head node r, and the value
2. If list is empty pointer r is NULL .
3. Create a new node p using malloc.
4. Insert the value as in data field of new node
5. Assign the list address r to a temporary variable q.
6. Using q keep on moving down the list till q points to last
node.
7. Attach new node p to node q.
8. Function returns r (the pointer to Head node).
9. If r is NULL , return p as Head node.

NULL

NULL

11

r
NULL

12

struct node *insert_end(struct node *r, int value)


{ struct node *p,*q;
p = //s.n. malloc
pdata = value; //create a new node with data
pnext = NULL;

q=r;
// start from head node
while (qnext != NULL)
q = qnext; // find the last element
qnext =p;
// attach the new node to last node
return r;
}

It is possible the list was empty in the beginning.


struct node *insert_end(struct node *r, int value)
{ struct node *p,*q;
p = //s.n. malloc
pdata = value; //create a new node with data
pnext = NULL;
if (r==NULL) return p;
q=r;
// start from head node
while (qnext!=NULL)
q=qnext;
// find the last element
qnext =p;
// attach the new node to last element
return r;
}

r points to a list with


first node with 11 and
last node with 4.

Contd.11

4
NULL

Add a node
containing 15 at
the end of the list

11
r

15
NULL

11

15
q

r
15

NULL

NULL

Generating a Queue by inserting


nodes at rear
..........
struct node *Q1;
Q1 = NULL;
Q1 = insert_end(Q1, 10);
display(Q1);
Q1 = insert_end(Q1, 11);
display(Q1);
Q1 = insert_end(Q1, 12);
display(Q1);
return 0;
}

Output
List = {10, }
List = {10, 11, }
List = {10, 11, 12, }

16

DELETION OF FRONT NODE


OF A LIST

Function to delete the node at front of the list


1. Function accepts pointer to the head node r
3. Assign the list address r to a temporary variable p.
4. Now let r point to next node.
5. Delete the node pointed by p (free it).
6. Function returns r (the pointer to new Head node).
7. If r is NULL , return NULL.

Given list
let p point to first node
let r point to second node

p
NULL
r

19

r
NULL

20

struct node * delete_F ( struct node * r)


{
struct node *p ;
p = r;
r = p->next; // move to next node
free(p);
// free the occupied space
return r;
}

Stack of cartons
stack of mattresses

stack operations
PUSH
insert an node at front of list

POP
delete a node at front of list

struct node *PUSH(struct node *r, int value)


{
// r is address of head node (top)
struct node *p;
p = (struct node *) malloc(sizeof(struct node));
p->data = value; // add data value in new node p
p ->next = r;
// attach the list to p
return p;
// p is now the new top
}

Creating a stack
void display (struct node *);
struct node * PUSH(struct node * , int);
int main()
{ struct node *top;
top = NULL;
top = PUSH(top, 10);
display(top);
top = PUSH(top, 11);
display(top);
top = PUSH(top, 12);
display(top);
return 0;
25
}

Output
List = {10, }
List = {11, 10, }
List = {12, 11, 10, }

Declare top globally as a variable of node structure and


make use of it in any function.

#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *link; }
NODE;
void Push( int );
int pop( );
void Display( );
NODE *top = NULL; /* Global Declarations */

int main( ) { . . . . .
......
.......

void PUSH ( int value ) {


NODE *temp;
temp = ( NODE *) malloc ( sizeof ( NODE ) );
temp ->data = value;
temp ->link = top;
top = temp;
printf (" Data added at top of stack );
}

int POP( ) {
int info;
NODE *t;
if( top == NULL) {
printf(" stack empty!!!");
return 1; }
else {
t=top;
info=top->data;
top=top->link;
t->link=NULL;
free(t);
return(info);
}
}

more operations on LINKED lists

DELETE LAST NODE OF A LIST

list with last node deleted

head

NULL

NULL

prev

32

cur

first node as prev,


second node as cur

head
NULL

prev

cur

33

Function to delete the last node of the list


1. Function accepts pointer to the head node r
3. Assign the list address r to a temporary variable prev.
4. Let cur point to second node of the list.
5. Move both prev and cur down the list till cur points to last
node.
6. Now set NEXT field of node prev to NULL.
7. The node pointed by cur is now deleted from the list.
(free it).

8. Function returns r.

list where prev and cur are moved to


end of the list

head
NULL

prev

35

cur

struct node * delete_L ( struct node * r)


{
struct node *cur, *prev ;
prev = r;
cur = r next;
while ( cur next != NULL) {
prev = cur;
cur = cur next; } // cur moves to last node
prev next = NULL; // this deletes the last node
free (cur);
return r;
}

head
NULL

prev

37

cur

head

NULL

NULL

prev

38

cur

CREATE A NEW LIST WITH


ELEMENTS IN REVERSE ORDER

Given a linked list P create another list Q which has the


elements in the reverse order.

#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *link; }
NODE;
void Push( int );
int pop( );
void Display( );
NODE *top = NULL; /* Global Declarations */
int main( ) { . . . . .
......

given list

P
10
prev

30

20

40

cur
40
41

NULL

reverse list

Q
40

30

prev

cur

20

42

10

NULL

Given a linked list P create another list Q which has the


elements in the reverse order.

Start with a list Q = NULL;


Put the first value of P in a new node q.
Make q point to node Q.

Rename the new node as Q.


Now repeat with other values of P.

cur = P;
Q = NULL;
while( cur != NULL) {
q = //s.n.malloc
qdata = curdata;
qnext = Q;
Q = q;
cur = curnext;
}

reverse list

10

45

NULL

reverse list

10

46

NULL

reverse list

20
q

47

10

NULL

reverse list

20
Q

48

10

NULL

INSERTING A NODE IN A SORTED


LIST

Given an array
[ 7 9 12 15 22 38 46 50]
Insert 20. This would require all elements >20 to be shifted right.
[ 7 9 12 15 20 22 38 46 50]

But in a linked list


7 9 12 15 22 38 46 50
.
to insert 20 search the number just below 20.
Change links such that 20 points to 22.
Change links such that 15 points to 20.
7 9 12 15 20 22 38 46 50

insertions on a sorted list


The new node value could be smallest than
that in the first node
The new node value could be somewhere in
middle
The new node value could be larger than that
in last node

new node is smallest


first

prev

13

15

new

ptr
18

22

--

Create new node for 7


Find correct place the first node itself is more than 7

52

new node inserted at front


first

prev

13

15

new

ptr
18

22

Take care ! It might turn out to be smallest of all elements and


has to be inserted before first node!
53

Insertion node 7 in middle


first

prev

new

ptr
8

--

Create new node for 7


Find correct place when ptr finds the 8 (7 < 8)

54

12

link 7 to both sides


first

prev

new

ptr
8

Make prev point to new node


Make new node point to current ptr

55

12

node has largest value


first

prev

new

16

__

Make prev point to new node


It need not point to anything as it is the last node

56

12

node gets inserted at end


first

prev

new

16

__

Make prev point to new node


It need not point to anything as it is the last node

57

12

Get data from the user and insert nodes to form a list in
sorted order
main() {
struct node *start;
int i,n,value;
start = NULL;
scanf("%d",&n);
for(i=0; i<n; i++) {
printf( Give Data: " );
scanf("%d , &value);
start = insert_m (start, value);
}
display(start);
return 0;
}

struct node* insert_m (struct node* r, int value) {


struct node*q, * p = //s.n. malloc;
p data = value; pnext = NULL;
if (r == NULL || ( value <= r data) {
p next = r;
r = p;
}
else {
q = r;
while (q next != NULL && qnextdata < value)
q = q next;
p next = q next;
q next = p;
}
return r;
}

new node smaller than first


r

13

15

18

22

When data turns out to be smallest of all elements and has to


be inserted before first node!
60

struct node* insert_m (struct node* r, int value) {


struct node*q, * p = //s.n. malloc;
p data = value; pnext = NULL;
if (r == NULL || ( value <= r data) {
p next = r;
r = p;
}
else
{
q = r;
while (q next != NULL && qnextdata < value)
q = q next;
p next = q next;
q next = p;
}
return r;
}

link 7 to both sides


r

qnext

pnext

Make new nodenext point to current node next


Make current node point to new node

62

12

node value more than last node


r

q
3

12

qnext

16

__

pnext

pnext = qnext which is NULL


qnext points to new node.
63

GIVEN A SORTED LIST, DELETE


NODE CONTAINING SPECIFIC VALUE

Deletion from a list


To delete a data item from a linked list
Find the data item in the list, and if found
Delink this node from the list
Free the node created by malloc

65

Delete node 8
first

prev

ptr

12

When ptr finds the item to be deleted, e.g. 8, we need the


previous node to make the link to the next one after ptr (i.e.
ptr -> next)
Also check whether first node is to be deleted
66

Example of Deletion
first

prev

ptr

12

When ptr finds the item to be deleted, e.g. 8, we need the


previous node to make the link to the next one after ptr (i.e.
ptr -> next)
Also check whether first node is to be deleted
67

delete(r,4)

Deleting an element

11

4
q

11

11
q
68

15

15

4
q

15

p NULL

struct node * delete(struct node * r, int value)


{ struct node *p, *q;
p =r;
q = p;
while(p!=NULL)
{
if (p->data == value) {
if (p==r) r = p->next; /* first node? */
else
q->next = p->next;
p->next = NULL;
/* Remove its link */
free(p);
/* free the occupied space */
return r;
}
else { q = p;
p = p->next; }
/* move down the list */
}
return r;
}

Deleting an element
struct node * delete(struct node * r, int value)
{ struct node *p, *q;
p =r;
11
q = p;
q
while(p!=NULL) {
if (p->data == value){
if (p==r) r = p->next;
else q->next = p->next;
p->next = NULL;
11
free(p);
q
return r;
}
else { q = p;
p = p->next;
11
}
q
}
return r;
70
}

delete(r,4)

15

15

15

4
p NULL

Exercises
Print a list backwards (try a recursive print)
Count the number of elements in a list (both
using and not using recursion)
Concatenate two lists
Reverse a list

71

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy