0% found this document useful (0 votes)
23 views16 pages

Linked List: Joy Mukherjee Programming & Data Structures

Linked lists are self-referential data structures where each node points to the next node in the list via a pointer. The document describes how to implement a linked list using a struct with two members - data and a pointer to the next node. It then demonstrates functions to insert nodes at the beginning, end or after a given position, as well as delete a node based on its data value. The reverse function is also shown, which reverses the order of nodes in the linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views16 pages

Linked List: Joy Mukherjee Programming & Data Structures

Linked lists are self-referential data structures where each node points to the next node in the list via a pointer. The document describes how to implement a linked list using a struct with two members - data and a pointer to the next node. It then demonstrates functions to insert nodes at the beginning, end or after a given position, as well as delete a node based on its data value. The reverse function is also shown, which reverses the order of nodes in the linked list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Linked List

Joy Mukherjee
Programming & Data Structures
Self-Referential Structure

typedef struct node


{
int data;
struct node *next;
}node;
The keyword struct is used to create an user-defined
datatype
Here struct node is an user-defined datatype
It has two members: Data of type int & next of type
struct node *.
Self-Referential Structure

Why struct node * is required?


To point to (or address) an integer, we know that int * is
needed. It helps the compiler to detect the starting
address of int and the size of bytes it has to read.
Similarly, to point to (or address) a struct node, we know
that struct node* is needed. It helps the compiler to
detect the starting address of struct node and the size of
bytes it has to read.
If a structure has a pointer to itself, it is called self-
referential structure.
Role of typedef
typedef int integer
It tells the compiler that int and integer have identical
properties in all respects.
typedef struct node
{
int data;
struct node *next;
}node;
It tells the compiler that struct node (written before {) and
node (written after }) have identical properties in all respects.
Linked List

12 2000 2 3000 99 4000 8 NULL

Head = 1000 2000 3000 4000


void insertAtFirst(node** head, int
new_data)
/* Create a new node */
node* new_node = (node*) malloc(sizeof(node));

/* Put the data */


new_node->data = new_data;

/* Set next of the created node as head */


new_node->next = *head;

/* Move the head to point to the new node */


*head = new_node;
insertAtFirst(&head, 1)
node* new_node = (node*) malloc(sizeof(node));

1 Junk new_node->data = new_data;


500

12 2000 2 3000 99 4000 8 NULL

*Head = 1000 2000 3000 4000

new_node->next = *head;
1 1000
*head = new_node;
*Head = 500

12 2000 2 3000 99 4000 8 NULL

1000 2000 3000 4000


void insertAtEnd(node** head, int
new_data)
/* Create a new node */
node* new_node = (node*) malloc(sizeof(node));
new_node->data = new_data;
new_node->next = NULL;

/* If the Linked List is empty, then make the new node as


head */
if (*head == NULL) {
*head = new_node;
return;
}
Cont.

/* The node 'last' used for traversing to the current last node
*/
node *last = *head;

/* Traverse till the last node */


while (last->next != NULL)
last = last->next;

/* Change the next of last node */


last->next = new_node;
insertAtEnd(&head, 1)
1 NULL

500

12 2000 2 3000 99 4000 8 NULL

*Head = 1000 2000 3000 4000

1 NULL

500

12 2000 2 3000 99 4000 8 500

*Head = 1000 2000 3000 4000


void insertAfter(node** head, int new_data,
int pos)
int i = 1;
node *prev_node = *head;
for(i = 1; i < pos; i++)
prev_node = prev_node->next;
node* new_node =(node*) malloc(sizeof(node));
new_node->data = new_data;

/* Set next of new node as next of prev_node */


new_node->next = prev_node->next;

/* Move the next of prev_node as new_node */


prev_node->next = new_node;
insertAfter(&head, 1,3)
1 NULL

500

12 2000 2 3000 99 4000 8 NULL

*Head = 1000 2000 3000 4000

1 NULL
3000

new_node=500

12 2000 2 500
3000 99 4000 8 NULL

*Head = 1000 Prev_node=2000 3000 4000


void deleteNode(node **head, int key)
/* Store head node */
node* temp = *head;
node *prev;

/* If head node itself holds the key to be deleted */


if (temp != NULL && temp->data == key) {
*head = temp->next; // Changed head
free(temp); // free old head
return;
}
Cont.
while (temp != NULL && temp->data != key) {
prev = temp;
temp = temp->next;
}

/* If key was not present in linked list */


if (temp == NULL) return;

/* Unlink the node from linked list */


prev->next = temp->next;

free(temp); // Free memory


deleteNode(&head, 99)

12 2000 2 3000 99 4000 8 NULL

*Head = 1000 2000 3000 4000

12 2000 2 3000
4000 99 4000 8 NULL

*Head = 1000 prev=2000 temp=3000 4000


reverse(&head)
Current->next = NULL Current->next = 1000 Current->next = 2000;
Prev = NULL Prev = 1000 Prev = 2000 *head = 3000;
Current = 1000 Current = 2000 Current = 3000
Next = 2000 Next = 3000 Next = NULL
Current->next = 2000 Current->next = 3000 Current->next = NULL

12 2000
NULL 2 1000
3000 99 2000
NULL

*Head = 1000 2000 *Head = 3000


3000

if(*head == NULL) while (current->next != NULL) { current->next = prev;


return; current->next = prev; *head = current;
node* prev = NULL; prev = current;
node* current = *head; current = next;
node* next = current->next; next = current->next;
}

You might also like