You are on page 1of 42

LINKED LIST

Pointer basics
• A pointer is a variable that holds the memory address of another object.
• General form of pointer declaration:
type *var_name;
Here, type is the base type of the pointer.
The base type specifies the type of object that the pointer can point to.

1/27/2020 Prepared by Tasneea Hossain 2


Pointer Basics

• Here, two special pointer operators : * and & are used.


• The & operator returns the address of the variable it precedes.
• The * operator returns the value stored at the address it precedes.

1/27/2020 Prepared by Tasneea Hossain 3


Pointer Basics
• In the preceding code, p is declared as an integer pointer and q as an
integer.
• Next q is assigned the value of 100.
• Next p is assigned the address of q.

• When a variable’s value is referenced through a pointer, the process is


called indirection.

1/27/2020 Prepared by Tasneea Hossain 4


Pointer Basics
• The base type of a pointer is very important.
For example :
Here, the pointer fp which is of
type double is assigned the
address of an integer.
However, since ints are usually
shorter than doubles and this
assignment causes memory
adjacent to q to be overwritten.

1/27/2020 Prepared by Tasneea Hossain 5


Pointer Basics
• Attempting to use a pointer before it has been assigned the address of a
variable can cause the program to crash.
• For example:

1/27/2020 Prepared by Tasneea Hossain 6


Pointer Basics
Location Contents

Let’s assume that p is an integer pointer


200 90
that currently points to the location 200.
After the statement : p++;
204 unknown
p will have the value 204, assuming that
208 unknown
integers are 4 bytes long.

1/27/2020 Prepared by Tasneea Hossain 7


Pointer Basics
• Character on the other hand is 1 byte long. So, a character pointer will
increase or decrease by 1 for the previous statement.

• Besides addition and subtraction, multiplication, division and modulus of a


pointer cannot be conducted.

1/27/2020 Prepared by Tasneea Hossain 8


Pointer Basics
• Suppose, p is an integer pointer.
• What will the following statements execute?
*p++;
The statement first increments p and then obtains the value at the new
location.

(*p)++;
 Increments the value pointed to by the pointer.

1/27/2020 Prepared by Tasneea Hossain 9


Pointer Basics
• Character on the other hand is 1 byte long. So, a character pointer will
increase or decrease by 1 for the previous statement.

• Besides addition and subtraction, multiplication, division and modulus of a


pointer cannot be conducted.

1/27/2020 Prepared by Tasneea Hossain 10


Uses of pointer
• Pointer is required for dynamic memory allocation.

• Linked list requires pointer in order to be implemented.

1/27/2020 Prepared by Tasneea Hossain 11


Dynamic Memory Allocation
• Dynamic memory allocation refers to managing system memory at
runtime. Dynamic memory management in C programming language is
performed via a group four functions named malloc(), calloc(), realloc(),
and free().

• These functions are defined under the C standard library header file called
<stdlib.h>.

1/27/2020 Prepared by Tasneea Hossain 12


malloc()
Prototype:
void *malloc(size_t numbytes);

Here, numbytes is the number of bytes of memory that the programmer


wishes to allocate.
The malloc function returns a pointer to the start of the allocated piece of
memory.

1/27/2020 Prepared by Tasneea Hossain 13


free()
Prototype:
void free(void *ptr);
To free memory, free() has to be called with a pointer to the start of the block
of memory that the programmer uses to free.

1/27/2020 Prepared by Tasneea Hossain 14


Structure Basics
• A structure is an aggregate data type that is composed of two or more
related variables called members.
• Each member of the structure can be of various types.
• General form:
struct tag_name{
type member1;
type member2;
type member3;

}variable_list;
1/27/2020 Prepared by Tasneea Hossain 15
Structure Basics
• The keyword struct tells the compiler that a structure type is being
defined.
• Tag-name is the type name of the structure.
• The variable list is where actual instances of the of the structure are
declared.
• Instances can also be declared in the main but will require the usage of the
keyword struct.

1/27/2020 Prepared by Tasneea Hossain 16


Structure Basics
• For example:

1/27/2020 Prepared by Tasneea Hossain 17


Pointers to structures
• To declare a pointer to structure :
struct point{
int x, y;
}s, *p;
To assign the address of s:
p=&s;

1/27/2020 Prepared by Tasneea Hossain 18


Pointers to structures
To access an individual element of s using p, the arrow operator needs to be
used instead of the dot operator.
p->x = 10;

1/27/2020 Prepared by Tasneea Hossain 19


Linked List
• A linked list is a linear data structure where the objects are arranged in a
linear order.
• The order in the linked list is determined by the pointer in each object.
• Linked lists are appropriate when the number of data elements to be
represented in the data structure at once in not predictable.
• Linked lists are dynamic , so the length of a list can increase or decrease as
necessary.

1/27/2020 Prepared by Tasneea Hossain 20


Linked List
• However, linked lists have a big disadvantage compared to arrays. Finding
the k-item of a linked list will take time proportional to k.

1/27/2020 Prepared by Tasneea Hossain 21


Linked List
• Each node of the linked list is divided into two parts:
• 1st part contains the information of the element
• 2nd part is called the link field or the next pointer field which contains the
address of the next node in the list.

struct node { 5 7 3 1
int value;
struct node *next;
}
1/27/2020 Prepared by Tasneea Hossain 22
Insertion into a Linked List
• A node can be added at first, last or after an interior node in the linked list.

Head

H E L L O

1/27/2020 Prepared by Tasneea Hossain 23


Insert First
• To add a new node to the head of the linear linked list, we need to construct
a new node that is pointed by pointer newitem.
• Assume there is a global variable head which points to the first node in the
list.
• The new node points to the first node in the list. The head is then set to
point to the new node.

Head L null

1/27/2020 Prepared by Tasneea Hossain 24


Insert First
• Step 1. Create a new node that is pointed by pointer newItem.
• Step 2. Link the new node to the first node of the linked list.
• Step 3. Set the pointer head to the new node.

1/27/2020 Prepared by Tasneea Hossain 25


Insert First
struct node{
int value;
struct node *next;
};
struct node *head;
void insertHead(){
//create a new node
struct node *newItem;
newItem=(struct node *)malloc(sizeof(struct node));
newItem->value = 10;
newItem->next = NULL;
//insert the new node at the head
newItem->next = head;
head = newItem;
}
1/27/2020 Prepared by Tasneea Hossain 26
Insert Last
• Step1. Create the new node.
• Step2. Set a temporary pointer prev to point to the last node.
• Step3. Set prev to point to the new node and new node as last node.

1/27/2020 Prepared by Tasneea Hossain 27


Insert Last
• In order to insert a node at the last, a new item has to be created by setting
the value of the link field to “NULL”.

• Next, the last node has to be detected and the current last node has to
point to the newly created node.

1/27/2020 Prepared by Tasneea Hossain 28


Insert after a desired node
struct node{
int value;
struct node *next;
};
struct node *head;

void insertMiddle(int num){


//create a new node to be inserted
struct node *newItem;
newItem=(struct node *)malloc(sizeof(struct node));
newItem->value = 10;
newItem->next = NULL;
// set prev to point to the desired node of the list
struct node *prev = head;
while (prev->value != num){
prev = prev->next;
}
newItem->next = prev->next;
prev->next = newItem;
}
1/27/2020 Prepared by Tasneea Hossain 29
Delete First

• To delete the first node of the linked list, the head pointer has to point to
the second node and the memory occupied by the abandoned node has to
be freed as well.

1/27/2020 Prepared by Tasneea Hossain 30


Delete First
• Step1. Initialize the pointer cur point to the first node of the list.
• Step2. Move the pointer head to the second node of the list.
• Step3. Remove the node that is pointed by the pointer cur.

cur
head

cur head

1/27/2020 Prepared by Tasneea Hossain 31


Delete First
void deleteHead()
{
struct node *cur;
if (head = = NULL) //list empty
return;
cur = head; // save head pointer
head = head->next; //advance head
free(cur);
}
1/27/2020 Prepared by Tasneea Hossain 32
Delete Last
• In order to delete the last item, two new local variables are to be used.
• One of the local variable, cur, will point to the last node.
• The other variable, prev, will point to the second last node

1/27/2020 Prepared by Tasneea Hossain 33


Delete Last
• Step1. Initialize pointer cur to point to the first node of the list, while the
pointer prev has a value of NULL.
• Step2. Traverse the entire list until the pointer cur points to the last node of
the list.
• Step3. Set NULL to next field of the node pointed by the pointer prev.
• Step4. Remove the last node that is pointed by the pointer cur.

1/27/2020 Prepared by Tasneea Hossain 34


Delete Any
• Step1. Initialize pointer cur to point to the first node of the list, while the
pointer prev has a value of null.
• Step2. Traverse the entire list until the pointer cur points to the node that
contains value of x, and prev points to the previous node.
• Step3. Link the node pointed by pointer prev to the node after the cur’s
node.
• Step4. Remove the node pointed by cur.

1/27/2020 Prepared by Tasneea Hossain 35


Doubly Linked List
• Sometimes, it is desirable to be able to traverse a linked list in either a
forward or reverse manner.
• In a doubly linked list, two pointer attributes will be present: next and prev.

2 7 4 9 1

prev key next


• Given an element x in the list, next will point to the successor and prev will
point to the predecessor.

1/27/2020 Prepared by Tasneea Hossain 36


Insertion into doubly linked list
Insert First:
Step 1 :To add a new node at the head, create the node with appropriate
value and set the prev pointer to null.
Step 2: Point the next pointer to the current head.
Step 3: Make the new node the new head.

1/27/2020 Prepared by Tasneea Hossain 37


Insertion into doubly linked list
Insert First:

1/27/2020 Prepared by Tasneea Hossain 38


Insertion into doubly linked list
Insert Last:
How would you insert an element at the last position?

Step 1: Create the node and set the next to NULL.


Step 2: Set the prev of the new node to the current head and next of current
head to the new node.
Step 3: Make the newly inserted node the last node.

1/27/2020 Prepared by Tasneea Hossain 39


Deletion in doubly linked list
• Step1. Set pointer prev to point to the left node of old and pointer cur to point to
the node on the right of old.
• Step2. Set the right link of prev to cur, and the left link of cur to prev.
• Step3. Discard the node pointed by old.

1/27/2020 Prepared by Tasneea Hossain 40


Doubly Linked List
Advantages :
• Can be traversed in both directions
• Insertion at first or middle takes O(1) time
Disadvantages:
• Every node requires extra space for previous pointer
• All operations require the maintenance of an extra previous pointer

1/27/2020 Prepared by Tasneea Hossain 41


Circular Linked List
1 9 8 2

Advantages:
Any node can be traversed to easily.
Saves time when we have to go from the last node to the first node

Disadvantages:
Since there are no NULLs to mark the end, the loop control is harder.

1/27/2020 Prepared by Tasneea Hossain 42

You might also like