Vous êtes sur la page 1sur 53

Linked Lists

DATA STRUCTURES
Arrays versus Linked Lists
 Arrays are simple to implement.
 Disadvantages of arrays as storage data
structures:
 Slow searching in unordered array
 Fixed size
 Slow insertion in ordered array
 to insert or remove an element at an interior location in
an ArrayList requires shifting of data and is an O(n)
operation.
Arrays versus Linked Lists …

 Linked lists solve some of these problems


Linked Lists

 A linked list or one-way list is a linear


collection of data elements, called nodes,
where the linear order is given by means of
pointers.
 Each node is divided into two parts:
 The first part contains the information of the
element and
 The second part contains the address of the next
node (link /next pointer field) in the list.
Linked Lists
info next info next info next
list null

Linear linked list


Invalid address

The null pointer signals the end of the list


Representation of LL in Memory
Bed No. Patient Next
1 Kalie 7
2
Start=5,INFO[5]=Adams, LINK[5]=3
3 Dean 11 INFO[3]=Dean , LINK[3]=11
Start 5
4 Maxwell 12
5 Adams 3
6
7 Lane 4
8 Green 1
9 Samuels 0
10
11 Fields 8
12 Nelson 9
Traversing a Linked List

Traverse(LIST,PROCESS,PTR)
Where LIST is a linked list in memory, PROCESS is an operation, PTR points to
node currently being processed.

1. Set PTR=START [Initializes pointer PTR]


2. Repeat steps 3 and 4 while PTR!=NULL
3. Apply PROCESS to INFO[PTR].
4. Set PTR=LINK[PTR].[PTR now points to the next node]
[End of step2 loop]
5. Exit
Counting No. of elements in a LL

Count(INFO,LINK,START,NUM)
1. Set NUM=0 [Initializes Counter]
2. Set PTR=START [Initializes pointer PTR]
3. Repeat steps 4 and 5 while PTR!=NULL.
4. Set NUM=NUM+1.
5. Set PTR=LINK[PTR]. [Updates pointer]
[End of step 3 loop]
6. Return
Searching a Linked List
When List is Unsorted

SEARCH(INFO,LINK,START,ITEM,LOC)
1. Set PTR=START [Initializes pointer PTR]
2. Repeat step 3 while PTR!=NULL.
3. If ITEM=INFO[PTR],then:
Set LOC=PTR, and Exit.
Else:
Set PTR=LINK[PTR]. [PTR now points to next node]
[End of If]
[End of Step 2 loop]
4. [Search is unsuccessful] Set LOC=NULL.
5. Exit.
Searching a Linked List
When List is Sorted
SEARCH(INFO,LINK,START,ITEM,LOC)
1. Set PTR=START [Initializes pointer PTR]
2. Repeat step 3 while PTR!=NULL.
If ITEM<INFO[PTR],then:
Set PTR=LINK[PTR]
Else if ITEM=INFO[PTR],then:
Set LOC=PTR, and Exit.[Search is successful]
Else:
Set LOC=NULL, and EXIT.[ITEM now exceeds INFO[PTR].]
[End of If]
[End of Step 2 loop]
4. Set LOC=NULL.
5. Exit.
Adding an Element to the front of a
Linked List

info next info next info next


list 5 3 8 null
Some Notations for use in algorithm
(Not in C programs)
 p: is a pointer
 node(p): the node pointed to by p

 info(p): the information portion of the node

 next(p): the next address portion of the node

 getnode(): obtains an empty node

 freenode(p): makes node(p) available for


reuse even if the value of the pointer p is
changed.
Adding an Element to the front of a
Linked List
info next
p p = getnode()

info next info next info next


list 5 3 8 null
Adding an Element to the front of a
Linked List
info next
p 6 info(p) = 6
info next info next info next
list 5 3 8 null
Adding an Element to the front of a
Linked List

info next
6
p
next(p) = list
info next info next info next
5 3 8 null
list
Adding an Element to the front of a
Linked List

info next
p
6
list list = p
info next info next info next
5 3 8 null
Adding an Element to the front of a
Linked List

info next info next info next info next


list 6 5 3 8 null
ALLOCATING AND FREEING
DYNAMIC VARIABLES
 C library function malloc() is used for dynamically
allocating a space to a pointer. Note that the
malloc() is a library function in <stdlib.h> header file.
 The following lines allocate an integer space from
the memory pointed by the pointer p.

int *p;
p = (int *) malloc(sizeof(int));

 Note that sizeof() is another library function that returns the


number of bytes required for the operand. In this example,
4 bytes for the int.
ALLOCATING AND FREEING
DYNAMIC VARIABLES
 Allocate floating point number space for a
float pointer f.

float *f;
f = (float *) malloc(sizeof(float));
Question:What is the output of the
following lines?
int *p, *q;
int x;
p = (int *) malloc(sizeof(int)); p
p 3
*p = 3;
x 6
x = 6;
q = (int *) malloc(sizeof(int)); q
q 6
*q=x;
printf(“%d %d \n”, *p, *q);
 The above lines will print 3 and 6.
C Implementation of Linked List

Program for linked list implementation


#include<stdio.h>
#include<conio.h>
struct Node // Defining the element of Linked List
{ int info;
struct Node *next;
}; typedef struct Node Node;
Node *list = NULL;
C Implementation of Linked List …

// Insertion at the beginning of the linked list


void insertBeg(int x)
{ Node *ptr = (Node *)malloc(sizeof(Node));
ptr->info = x;
ptr->next = list;
list = ptr;
}
C Implementation of Linked List …
// Insertion at the end of the linked list
void insertEnd(int x)
{ Node *ptr = (Node *)malloc(sizeof(Node));
Node *loc;
ptr->info = x;
ptr->next = NULL;
if(list == NULL) // If list is empty
list = ptr;
else
{ loc = list;
while(loc->next != NULL)
loc = loc->next;
loc->next = ptr;
}
}
Inserting an item x into a list after a
node pointed to by p
p

list X0 X1 X2 X3 X4 X5 X6 null

list X0 X1 X2 X3 X4 X5 X6 null

q x
C Implementation of Linked List …
// Insertion after the specified location in the linked list
void insertLoc(int loc, int x)
{ Node *ptr;
Node *temp; int k;
for(k = 0, temp = list; k< loc-1; k++)
{ temp = temp->next;
if(temp == NULL)
{ printf("Desired location is more than the size of list\n");
return;
}
}
ptr = (Node *)malloc(sizeof(Node));
ptr->info = x;
ptr->next = temp->next;
temp->next = ptr;
}
Removing an Element from the front
of a Linked List
info next info next info next info next
list 6 5 3 8 null
Removing an Element from the front
of a Linked List
p = list
info next info next info next info next
list
6 5 3 8 null
p
Removing an Element from the front
of a Linked List
info next
p 6 list = next(p)
info next info next info next
5 3 8 null
list
Removing an Element from the front
of a Linked List
info next
p 6 x = info(p)
info next info next info next
x=6
5 3 8 null
list
Removing an Element from the front
of a Linked List
info next
p freenode(p)
info next info next info next
x=6
list 5 3 8 null
Removing an Element from the front
of a Linked List

info next info next info next


x=6 list 5 3 8 null
C Implementation of Linked List …
// Deletion from the beginning of the linked list
int delBeg()
{ Node *temp; int item;
if(list == NULL)
{ printf("List is empty\n");
return;
}
else
{ temp = list;
list = list->next;
item = temp->info;
free(temp);
return(item);
}
}
C Implementation of Linked List …

// Display the linked list


void display()
{ if(list == NULL) printf("List is empty\n");
else
{ Node *tmp = list;
printf("The list is : \n");
while(tmp != NULL)
{ printf("%d\n", tmp->info);
tmp = tmp->next;
}
}
}
Inserting an item x into a list after a
node pointed to by p
q=getnode();
info(q)=x;
next(q)=next(p);
next(p)=q;
Deleting an item x from a list after a
node pointed to by p
p q

list X0 X1 X2 X3 X4 X5 X6 null

x =X3

list X0 X1 X2 X3 X4 X5 X6 null
Deleting an item x from a list after a
node pointed to by p
q=next(p);
x=info(q);
next(p)=next(q);
freenode(q);
LINKED LISTS STRUCTURES
AND BASIC FUNCTIONS
 The value zero can be used in a C program as the null pointer. You
can use the following line to declare the NULL constant. Note that a
NULL pointer is considered NOT to point any storage location.

#define NULL 0

 The following node structure can be used to implement Linked Lists.


Note that the info field, which can be some other data type (not
necessarily int), keeps the data of the node and the pointer next links
the node to the next node in the Linked List.

struct node{
int info;
struct node *next;
};
typedef struct node *NODEPTR;
LINKED LISTS STRUCTURES
AND BASIC FUNCTIONS
 When a new node is required (e.g. to be inserted
into the list) the following function, getnode, can be
used to make a new node to be available for the list.
NODEPTR getnode()
{
NODEPTR p;
p = (NODEPTR) malloc(sizeof(struct node));
return p;
}
LINKED LISTS STRUCTURES
AND BASIC FUNCTIONS
When a new node is no longer used (e.g. to
be deleted from the list) the following
function, freenode, can be used to release
the node back to the memory.
void freenode(NODEPTR p)
{
free(p);
}
PRIMITIVE FUNCTIONS FOR
LINEAR LINKED LISTS
 The following functions insertafter(p,x) and
delafter(p,px) are primitive functions that can
be used for the dynamic implementation of a
linked list. Assume that list is a pointer
variable pointing the first node of a list (if any)
and equals NULL in the case of an empty list.
void insertafter(NODEPTR p, int x)
{
NODEPTR q;
q=getnode();
q->info = x;
q->next = p->next;
p->next = q;
}
void delafter(NODEPTR p , int *px)
{
NODEPTR q;
if(p != NULL)
{
q = p->next;
*px = q->info;
p->next = q->next;
freenode(q);
}
Searching through the linked list.

 The following function searches through the


linked list and returns a pointer the first
occurrence of the search key or returns NULL
pointer if the search key is not in the list. Note
that the linked list contains integer data items.
NODEPTR searchList(NODEPTR plist, int key)
{
NODEPTR p;
p = plist;
while(p != NULL){
if(p->info == key)
return p;
p = p->next;
}
return NULL;
}
Displaying the linked list elements

 Write a function to display the student with highest


CGPA in a linked list containing student data. Use
the following node structure for your linked list.

struct node{
int stNo;
float CGPA;
struct node *next;
};
typedef struct node *NODEPTR;
void DisplayMax(NODEPTR plist)
{
NODEPTR p;
float maxCGPA=-1.0;
int maxstNo;
p = plist; /*current node*/
if(p == NULL){
printf(“no node/data is available in the list\n”);
return;
}
do{
if(p->CGPA > maxCGPA){
maxCGPA = p->CGPA;
maxstNo = p->stNo;
}
p = p->next;
} while(p!= NULL);
printf(“The student number with max CGPA: %d\n”, maxstNo);
printf(“The student’s CGPA: %d\n”, maxCGPA);
}
Linked Lists

 Any sequential organization of items is list


 Array is one way to represent lists
 Another more efficient way is to make each
item in the list a part of the structure that also
contains link to the structure containing the
next item
 This type of list is known as linked list whose
order is given by links (pointers) from one
node to next
 Main advantage of using LL over arrays is
that LL can grow or shrink in size during
execution of a program.
 LL does not waste memory space.
 Provides flexibility to rearrange the items
 Insertion and deletion of items is easy simply
by rearranging those links (pointers)
 In arrays, order of elements is given by their
physical placement in memory ( contiguous
memory location)
 We simply use an index to access and
manipulate elements in an array
 In LL structure elements are ordered by
logical links (pointers) that are part of
structure itself
 Each structure of list is called a node and consists of two
field – data part and other containing address of next
item ( pointer to next item)
 Link is a pointer to another structure of same type
 struct node
{
int item;
struct node * next;
};
 consider a simple example:
struct node node1, node2;
The next pointer of node1 can be made point to
node2 by the statement:
node1.next = &node2;
Now, this process may continue to add any
number of nodes
node2.next = &node3;
Indicate end of LL by assigning null or 0 to next
part of last node
For example, node3.next = 0; // node3 is last
node of LL
 Let us also assign values to the field item :
node1.item = 10;
node2.item = 20;
node3.item = 30;
Operations on LL

 Creation
 Traversal
 Insertion
 Deletion

Vous aimerez peut-être aussi