Linked Lists: Data Structures
Linked Lists: Data Structures
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 …
Traverse(LIST,PROCESS,PTR)
Where LIST is a linked list in memory, PROCESS is an operation, PTR points to
node currently being processed.
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
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
int *p;
p = (int *) malloc(sizeof(int));
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
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
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
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.
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
Creation
Traversal
Insertion
Deletion