You are on page 1of 45

Linked Lists

Introduction
• A linked list is a series of connected nodes
• Each node has two parts
– A piece of data [INFO]
– Pointer to the next node in the list [LINK]
• Successive nodes are connected by pointers.
• Last node points to NULL.
• It can grow or shrink in size during execution of a program.
• It can be made just as long as required.
• It does not waste memory space.
• Head: pointer to the first node
• The last node points to NULL
Introduction
Head=NULL
indicates that the list is empty
Introduction
• Keeping track of a linked list:
– Must know the pointer to the first element of the
list (called start, head, etc.).

• Linked lists provide flexibility in allowing the


items to be rearranged efficiently.
– Insert an element.
– Delete an element.

Spring 2012 Programming and Data Structure 4


Array versus Linked List
• Arrays are suitable for:
– Inserting/deleting an element at the end.
– Randomly accessing any element.
• Linked lists are suitable for:
– Inserting and deleting an element at any location
– Applications where sequential access is required.
– In situations where the number of elements
cannot be predicted beforehand.
Array versus Linked List
Arrays
• Arrays are static data structures
• Array occupies a block of memory space and array
size cannot be increased when additional space is
required
Linked Lists
• Linked lists are dynamic data structures
• Efficient memory utilization i.e. Memory is allocated
when required
Representation of Linked List in memory
Let LIST be the linear linked list.
It needs two linear arrays for memory
representation.
Let these linear arrays are INFO and LINK.
INFO[K] contains the information part and
LINK[K] contains the next pointer field of node K.
A variable START is used to store the location of
the beginning of the LIST and
NULL is used as sentinel which indicates the end
of LIST.
Representation of Linked List in memory
Illustration: Insertion
A B C

tmp X Item to be
inserted

A B C

curr

Spring 2012 Programming and Data Structure 9


Illustration: Deletion
Item to be deleted
A B C

tmp
curr

A B C

Spring 2012 Programming and Data Structure 10


Types of Linked Lists
Depending on the way in which the links are
used to maintain adjacency, several different
types of linked lists are possible.

– Singly-linked list (or simply linear list)


• One we have discussed so far.
Types of Linked Lists
– Circular linked list
• The pointer from the last element in the list points back
to the first element.
• There is no NULL at the end
Types of Linked Lists
– Doubly linked list
• Pointers exist between adjacent nodes in both
directions.
• The list can be traversed either forward or backward.
Types of Linked Lists
Circular Doubly Linked
• List has properties of both doubly linked list and circular
linked list in which two consecutive elements are linked or
connected by previous and next pointer
• the last node points to first node by next pointer and also the
first node points to last node by previous pointer.
Operations in Linked list
• Insertion
• Deletion
Garbage Collection
• The operating system of a computer may
periodically collect all the deleted space onto
the free storage list.
• Any technique which does this collection is
called garbage collection.

• It has two steps:


Garbage Collection
• Computer runs through memory, tagging those
cells which are currently in use.
• Computer runs through memory, collecting all
untagged space onto the free storage list.

• Garbage collection takes place when there is only


some minimum amount of space or no space at
all left in the free storage list
• Or when CPU is idle and has time to do the
collection
Traversing a linked list
Searching in linked list
Insertion
• Inserting at the beginning of a list
• Inserting after a given node
• Inserting into a sorted linked list
Inserting at the beginning of a list
Inserting after a given node
INSLOC(INFO,LINK,START,AVAIL,LOC,ITEM)
Inserting into a sorted linked list
INSERT(INFO,LINK,START,AVAIL,ITEM)
Call FINDA(INFO,LINK,START,ITEM,LOC)
Call INSLOC(INFO,LINK,START,AVAIL,LOC,ITEM)
Exit
Inserting into a sorted linked list
Implementation of Singly Linked List
#include <stdio.h>
#include <malloc.h>
#include<conio.h>
#include <stdlib.h>
void main()
{ struct node
{ int info;
struct node *next;
};
• struct node *p,*temp,*start=NULL;
• int n,i=1;
• printf("Enter total number of nodes");
• scanf("%d",&n);
• temp=(struct node *)malloc(sizeof(struct node));
• printf("Enter node no. %d ",i);
• scanf("%d",&temp->info);
• temp->next=NULL;
• p=temp;
• start=temp;
• for(i=2;i<=n;i++)
• { temp=(struct node *)malloc(sizeof(struct node));
• printf("Enter node no. %d ",i);
• scanf("%d",&temp->info);
• temp->next=NULL;
• p->next=temp;
• p=p->next;
• }
• }
Deletion
• Deleting the node following a given node
• Deleting the node with a given ITEM of
information
Deleting the node following a given node
Deleting the node with a given ITEM of
information
Linked Stacks
Linked Stacks
• No limitation on the capacity of the linked
stack
• Support as many as PUSH operations as the
free storage (AVAIL) list can support
• No need to maintain MAXSTK
PUSH in Linked Stack
POP in Linked Stack
Linked Queue
Linked Queue
• Unlike array representation, the linked queue
functions as a linear queue
• No need to view it as circular for efficient
management of space
Insertion in Linked Queue
Deletion in Linked Queue
Polynomial Representation
 A polynomial is a sum of terms.
 Each term consists of a coefficient and a
variable raised to an exponent.
 Example: 4x3 + 5x – 10.
Polynomial Representation
For single variable

INFO LINK
Polynomial Representation
Nodes are linked according to decreasing
degree
Addition of two Polynomials
P=5x2+4x+2
Q=5x+5
Multiplication of two polynomials
Addition of two polynomials (Algorithm)
Node Structure for 3 variables

Example: 3x2+2xy2+5y3+7yz

You might also like