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

CSE23302 Module2 Chapter 3.Pptx

This document provides an overview of linked lists, including their definition, types, and operations such as insertion, deletion, and display. It discusses the advantages of linked lists over sequential storage for stacks and queues, as well as implementations of these data structures using linked lists. Additionally, it includes C functions for various linked list operations and examples of polynomial addition and multiplication using linked lists.

Uploaded by

rohitjlloddnor
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)
12 views

CSE23302 Module2 Chapter 3.Pptx

This document provides an overview of linked lists, including their definition, types, and operations such as insertion, deletion, and display. It discusses the advantages of linked lists over sequential storage for stacks and queues, as well as implementations of these data structures using linked lists. Additionally, it includes C functions for various linked list operations and examples of polynomial addition and multiplication using linked lists.

Uploaded by

rohitjlloddnor
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/ 52

CSE23302

Data Structures
Module 2- Chapter 3
Linked Lists
Dr. R Kanagavalli
Professor
Department of CSE
Global Academy of Technology
Bangalore
Objectives
To Understand the concept of linked list
To implement the primitive operations on linked list
To understand the linked list implementation of stack
To understand the linked list implementation of
queues
To write programs to demonstrate the applications of
linked list.

2
LINKED LISTS
What are the drawbacks of using sequential storage to represent stacks
and queues?

1.Fixed amount of storage remains allocated to the stack or queue even


when the structure is actually using a smaller amount or possibly
no storage at all.

2.No more than that fixed amount of storage may be allocated, thus
introducing the possibility of overflow.

3
Linked list -Introduction
• A linked list is a collection of zero or more nodes where each node has
some information.

• Between each node in the list , there exits a logical relationship. Each
node can hold the data along with a pointer field using which address
of the next node can be obtained.

4
Types of Linked Lists

1. Singly linked lists


2. Circular singly linked lists
3. Doubly linked lists
4. Circular doubly linked lists

5
info next info next info next
list null

node node node

• Each item in the list is called a node and contains


two fields, an information field and a next address
field.
• the information field holds the actual element on the
list.
• the next address field contains the address of the
next node in the list.
• Such an address, which is used to access a
particular node, is known as a pointer.
6
• The next address field of the last node contains a special value, known
as null, which is not a valid address. This null pointer is used to signal
the end of a list.
• The list with no nodes on it is called the empty list or the null list.

7
Singly Linked List
• A linked list is a linked list where each node has some data and
pointer to the next node in the list .
• can traverse it from the beginning (first) node only. Cannot traverse in
backward or from the last node to first node.

8
Structure Definition of a node
struct node
{
int info;
struct node *link ;
};
typedef struct node *NODE;

9
Operations on Linked Lists
1. Insertion
2. Deletion
3. Display

10
Inserting Nodes into a List
• A list is a dynamic data structure.

• The number of nodes on a list may vary dramatically


as elements are inserted and removed.

• The dynamic nature of a list may be contrasted with


the static nature of an array, whose size remains
constant.

11
• For example, suppose that we are given a list of
integers, as shown in fig below:

info next info next info next


listp 5 2000 3 3000 8 null

1000 2000 3000

• We desire to add the integer 6 to the front of that list.


i.e., we wish to change the list so that it appears as in
fig below:

info next info next info next info next


listp 6 1000 5 2000 3 3000 8 null

5000 1000 2000 3000


12
• The first step is to obtain a empty node in which to
house the additional integer (i.e., 6).
• If a list is to grow and shrink, there must be some
mechanism for obtaining empty nodes to be added
onto the list.

13
C function to get a node from availability list
if(x ==NULL)
NODE getnode()
{
{
printf(“Out of memory\n”);
NODE x;
exit(0);
x=( NODE ) }
malloc(sizeof(struct return x;
node));
}

14
C function to insert a node at the front of the list
NODE insert_front(int item, NODE first)
{
NODE temp;
temp= (NODE ) malloc(sizeof (struct node);
temp->info = item;
temp->link = first;
return temp;
}
15
info next
temp
= 5000
5000

info next info next info next


first 5 2000 3 3000 8 null

1000 2000 3000

16
info next
temp 6 1000
= 5000
5000

info next info next info next


first 5 2000 3 3000 8 null
= 1000
1000 2000 3000 17
Deleting Nodes from a List
• Removing the first node of a nonempty list and
storing the value of its info field into a variable x.

Initial configuration:

info next info next info next


first 7 5 9 null
node node node

Final configuration:
info next info next
first 5 9 null
node node 18
C function to delete a node from the front of the list

NODE delete_front(NODE first)


{
NODE temp;
if ( first == NULL )
{
printf(“List is empty cannot delete\n”);
return NULL;
}
temp = first;
temp = temp->link;
printf(“Item deleted = %d\n”,first->info);
free(first);
return temp;
}
19
• The process itself is almost the exact opposite of
the process to add a node to the front of a list.

• Sequence of operations:

1. temp = first;

temp
info next info next info next
first 7 5 9 null
node node node

20
2. temp=temp->link

first
info next info next info next
temp 7 5 9 null
node node node

temp

21
3. x = first->info

first
info next info next info next
7 5 9 null
node node node

temp

X=7

22
C function to display the contents of the list
void display(NODE first) printf(“The contents of singly linked
{ list\n”);
cur = first;
NODE cur;
while ( cur !=NULL )
if (first == NULL) {
printf(“%d “, cur->info);
{ cur = cur->link;
printf(“List is empty\n”); }
}
return;
}
23
C function to insert element from rear end
NODE insert_rear(int item, NODE first) cur = first;
{ while ( cur->link != NULL )
NODE temp;
{
NODE cur;
temp = getnode(); cur = cur->link;
temp->info = item; }
temp->link = NULL; cur->link = temp;
if ( first == NULL ) return first;
return temp; }

24
C function to delete element from rear end
NODE delete_rear(NODE first) if( first->link == NULL )
{ {
NODE cur, prev;
printf(“The item to deleted is
if (first == NULL)
{ %d\n, first->info);
printf(“List is empty cannot be free(first);
deleted\n”); return NULL;
return first; }
}

25
prev = NULL; printf(“The item deleted is
cur = first; %d\n”,cur->info);
while( cur->link !=NULL ) free(cur);
{ prev->link = NULL
prev = cur; return first;
cur = cur->link; }
}

26
Linked List implementation of Stacks

• The operation of adding an element to the front of a


linked list is quite similar to that of pushing an
element onto a stack.

• Similarly, the operation of removing the first element


from a linked list is analogous to popping a stack.
• insert_front ()
• delete_front()
• display()

27
• Ex: stack as a linked list
info next info next info next
s 60 90 40 null

1. Insert(s,45)

info next info next info next info next


temp 45 60 90 40 null

s s

2. delete(s)

info next info next info next info next


temp 45 60 90 40 null

s s
X = 45
28
• After freeing temp, we get

info next info next info next


60 90 40 null

s
X = 45

29
• The advantage of the list implementation of the stack
is that:

1. All stacks being used by a program can share the same


available list.

2. When a stack needs a node, it can obtain it from the single


available list.

3. When any stack no longer needs a node, it returns the node


to the same available list.

30
4. Each stack is able to grow and shrink to any size.

5.No space has been preallocated to any single stack


and no stack is using space that it does not need.

31
Linked List implementation of Queues

How to represent a queue as a linked list ?

• We know, Items are deleted from the front of the


queue and inserted at the rear end.

• Let a pointer to the first element of a list represent


the front of the queue.

• Another pointer to the last element of the list


represents the rear of the queue.
32
info next info next info next
front null
=1000
1000 2000 3000

rear =
Insert() 3000

info next info next info next info next


front null
=1000
1000 4000
2000 3000

rear =
4000

33
• Use the following functions
• insert_rear()
• delete_front()
• display()

34
Insertion of new node in the given position
• If p points to an element of the list, inserting a new element after
node(p) involves
• Allocating a new node
• Inserting the information
• Adjusting the pointers

35
•q=getnode( )
•q->info=x
•q->link=p->link
•p->link=q

36
p

s x0 x1 x2 x3 x4 x5 x6

s x0 x1 x2 x3 x4 x5 x6

x q
37
C function to insert element at some
position(AFTER)
NODE insert_after(int item, NODE first, NODE cur = first;
p) while ( cur != p )
{
{
NODE temp;
NODE cur; cur = cur->link;
temp = getnode(); }
temp->info = item; temp->link = temp->link = cur->link;
NULL; cur->link=temp ;

return first;
}

38
• If p points to an element of the list, inserting a new element before
node(p) involves
• Allocating a new node
• Inserting the information
• Inserting the element immediately after node(p) and then
interchanging info(p) with info field of the newly created successor.

39
C function to insert element at some
position(BEFORE)
NODE insert_before(int item, NODE first, cur = first;
NODE p) while ( cur != p )
{
{
NODE temp;
NODE cur; cur = cur->link;
temp = getnode(); }
temp->info = item; temp->link = temp->link = cur->link;
NULL; cur->link=temp ;
cur->info=temp->info;
temp->info=p->info;
return first;
}

40
Other useful operations in the linked list
• To find the length of the linked list
• To search for an item in the list
• To delete an item from the list
• To concatenate two lists
• To reverse a list

41
Node declaration of Polynomial
struct node
{
int coeff ;
int exp;
struct node *next;
};

typedef struct node *NODE;

42
Function to add two polynomials using Linked list

void polyadd(NODE poly1, NODE poly2) if (poly1->exp > poly2->exp )


{ {
NODE res; res->exp= poly1->exp;
while(poly1->next && poly2->next) res->coeff = poly1->coeff;
poly1 = poly1->next;
{
}
// If power of 1st polynomial is greater
then 2nd, then store 1st as it is and
move its pointer

43
// If power of 2nd polynomial is elseif ( poly1->exp < poly2->exp)
greater then 1st, then store 2nd {
as it is and move its pointer
res->exp= poly2->exp;
res->coeff = poly2->coeff;
poly2 = poly2->next;
}

44
// If power of both polynomial else
numbers is same then add {
their coefficients
res->exp= poly1->exp;
res->coeff = poly1->coeff + poly2->coeff;
poly1 = poly1->next;
poly2 = poly2->next;
}

45
// Dynamically create new node
res->next = (structNODE )malloc(sizeof(structnode));
res = res->next;
res->next = NULL;
}

46
while(poly1->next || poly2->next) if (poly2->next)
{ {
if (poly1->next) res->exp= poly2->exp;
{ res->coeff = poly2->coeff;
res->exp= poly1->exp; poly2 = poly2->next;
res->coeff = poly1->coeff; }
poly1 = poly1->next;
}

47
res->next= (struct NODE ) malloc (sizeof (struct node));
res = res->next;
res->next = NULL;
}
}

48
Function to Multiply two polynomials using
Linked list

struct node
{
int co,po;
struct node *addr;
};
typedef struct node *NODE;

49
NODE insertend(NODE start,int co,int po)
{
if(start==NULL)
NODE temp,cur;
return temp;
temp=(NODE)malloc(sizeof(struct
cur=start;
node));
while(cur->addr!=NULL)
temp->co=co;
cur=cur->addr;
temp->po=po;
cur->addr=temp;
temp->addr=NULL;
return start;
}

50
NODE addterm(NODE res,int co,int po) while(cur!=NULL)
{ {
NODE temp,cur; if(cur->po==po)
temp=(NODE)malloc(sizeof(struct {
node)); cur->co=cur->co+co;
temp->co=co; return res;
temp->po=po; }
temp->addr=NULL; cur=cur->addr;
if(res==NULL) }
return temp; if(cur==NULL)
cur=res; res=insertend(res,co,po);
return res;
}

51
NODE multiply(NODE poly1,NODE poly2)
{
NODE p1,p2,res=NULL;
for(p1=poly1;p1!=NULL;p1=p1->addr)
for(p2=poly2;p2!=NULL;p2=p2->addr)
res=addterm(res,p1->co*p2->co,p1->po+p2->po);
return res;
}

52

You might also like