0% found this document useful (0 votes)
7 views3 pages

Insertion Methods for Singly Linked List

The document contains C code for inserting nodes into a singly linked list at the beginning, end, and after a specified node. It includes functions for each type of insertion and handles memory allocation and overflow conditions. The main function allows users to repeatedly insert items based on their choice until they opt to stop.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Insertion Methods for Singly Linked List

The document contains C code for inserting nodes into a singly linked list at the beginning, end, and after a specified node. It includes functions for each type of insertion and handles memory allocation and overflow conditions. The main function allows users to repeatedly insert items based on their choice until they opt to stop.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Insertion in singly linked list at beginning

1. #include<stdio.h>
2. #include<stdlib.h>
3. void beginsert(int);
4. struct node
5. {
6. int data;
7. struct node *next;
8. };
9. struct node *head;
10. void main ()
11. {
12. int choice,item;
13. do
14. {
15. printf("\nEnter the item which you want to insert?\n");
16. scanf("%d",&item);
17. beginsert(item);
18. printf("\nPress 0 to insert more ?\n");
19. scanf("%d",&choice);
20. }while(choice == 0);
21. }
22. void beginsert(int item)
23. {
24. struct node *ptr = (struct node *)malloc(sizeof(struct node *));
25. if(ptr == NULL)
26. {
27. printf("\nOVERFLOW\n");
28. }
29. else
30. {
31. ptr->data = item;
32. ptr->next = head;
33. head = ptr;
34. printf("\nNode inserted\n");
35. }
36.
37. }

Insertion in singly linked list at the end


1. #include<stdio.h>
2. #include<stdlib.h>
3. void lastinsert(int);
4. struct node
5. {
6. int data;
7. struct node *next;
8. };
9. struct node *head;
10. void main ()
11. {
12. int choice,item;
13. do
14. {
15. printf("\nEnter the item which you want to insert?\n");
16. scanf("%d",&item);
17. lastinsert(item);
18. printf("\nPress 0 to insert more ?\n");
19. scanf("%d",&choice);
20. }while(choice == 0);
21. }
22. void lastinsert(int item)
23. {
24. struct node *ptr = (struct node*)malloc(sizeof(struct node));
25. struct node *temp;
26. if(ptr == NULL)
27. {
28. printf("\nOVERFLOW");
29. }
30. else
31. {
32. ptr->data = item;
33. if(head == NULL)
34. {
35. ptr -> next = NULL;
36. head = ptr;
37. printf("\nNode inserted");
38. }
39. else
40. {
41. temp = head;
42. while (temp -> next != NULL)
43. {
44. temp = temp -> next;
45. }
46. temp->next = ptr;
47. ptr->next = NULL;
48. printf("\nNode inserted");
49.
50. }
51. }
52. }

Insertion in singly linked list after specified Node


1. #include<stdio.h>
2. #include<stdlib.h>
3. void randominsert(int);
4. void create(int);
5. struct node
6. {
7. int data;
8. struct node *next;
9. };
10. struct node *head;
11. void main ()
12. {
13. int choice,item,loc;
14. do
15. {
16. printf("\nEnter the item which you want to insert?\n");
17. scanf("%d",&item);
18. if(head == NULL)
19. {
20. create(item);
21. }
22. else
23. {
24. randominsert(item);
25. }
26. printf("\nPress 0 to insert more ?\n");
27. scanf("%d",&choice);
28. }while(choice == 0);
29. }
30. void create(int item)
31. {
32.
33. struct node *ptr = (struct node *)malloc(sizeof(struct node *));
34. if(ptr == NULL)
35. {
36. printf("\nOVERFLOW\n");
37. }
38. else
39. {
40. ptr->data = item;
41. ptr->next = head;
42. head = ptr;
43. printf("\nNode inserted\n");
44. }
45. }
46. void randominsert(int item)
47. {
48. struct node *ptr = (struct node *) malloc (sizeof(struct node));
49. struct node *temp;
50. int i,loc;
51. if(ptr == NULL)
52. {
53. printf("\nOVERFLOW");
54. }
55. else
56. {
57.
58. printf("Enter the location");
59. scanf("%d",&loc);
60. ptr->data = item;
61. temp=head;
62. for(i=0;i<loc;i++)
63. {
64. temp = temp->next;
65. if(temp == NULL)
66. {
67. printf("\ncan't insert\n");
68. return;
69. }
70.
71. }
72. ptr ->next = temp ->next;
73. temp ->next = ptr;
74. printf("\nNode inserted");
75. }
76.
77. }

You might also like