0% found this document useful (0 votes)
24 views7 pages

Insertion in Singly 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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views7 pages

Insertion in Singly 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 DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Insertion in Singly Linked List

1. Insert at Beginning
Struct node
{
Int data;
Struct node *next;
};
Struct node *head, *newnode;
Newnode = (struct node *) malloc(size of (struct
node));
Printf(“Enter data you want to insert:”);
Scanf(“%d”, &newnode->data);
Newnode->next = head;
Head = newnode;
2. Insert at the End
Struct node
{
Int data;
Struct node * next;
};
Struct node *head, *newnode, *temp;
Newnode = (struct node * malloc(sizeof(struct
node));
Printf(“Enter data you want to insert:”);
Scanf(“%d”, &newnode->data);
Newnode->next = 0;
Temp = head;
While (temp->next != 0)
{
Temp = temp->next;
}
Temp->next = newnode;
3. Insert after a given location
Int pos; i=1;
Struct node
{
Int data;
Struct node * next;
};
Struct node * head, *newnode, *temp;
Newnode = (struct node *) malloc(sizeof(struct
node));
Printf(“Enter the position:”);
Scanf(“%d”, &pos);
If (pos > count)
{
Printf (“Invalid position”);
}
Else
{
Temp=head;
While (i < pos)
{
Temp = temp->next;
i++;
}
Printf(“Enter data”);
Scanf(“%d”, &newnode->data);
Newnode->next = temp->next;
Temp->next = newnode;

Deletion from Singly Linked List:


1. Delete from Beginning
2. Delete from End
3. Delete from specified position

1. Delete from Beginning

Struct node
{
Int data;
Struct node *next;
};
Struct node *head, *temp;
DeletefromBeg()
{
// Struct node * temp;
Temp = head;
Head = head->next;
Free(temp);
}

2. Delete from End


DeletefromEnd()
{
Struct node *head, *temp, *prevnode;
Temp = head;
While (temp->next != 0)
{
Prevnode = Temp;
Temp = temp -> next;
}
If (temp == head)
{
Head = 0;
Free(temp);
}
Else
{
Prevnode->next = 0;
Free (temp);
}
}

3. Delete from Specified Position:


Struct node
{
Int data;
Struct node *next;
};
Struct node * head, *temp;
DeleteFromPos()
{
Struct node *nextnode;
Int pos, I = 1;
Temp = head;
Printf(“Enter position:”);
Scanf(“%d”,&pos);
While(i<pos-1)
{
Temp=temp->next;
i++;
}
Nextnode = temp->next;
Temp->next=nextnode->next;
Free(nextnode);
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy