1/**
2 * C program to insert a new node at the beginning of a Singly Linked List
3 */
4
5#include <stdio.h>
6#include <stdlib.h>
7
8
9/* Structure of a node */
10struct node {
11 int data; // Data
12 struct node *next; // Address
13}*head;
14
15
16void createList(int n);
17void insertNodeAtBeginning(int data);
18void displayList();
19
20
21int main()
22{
23 int n, data;
24
25 /*
26 * Create a singly linked list of n nodes
27 */
28 printf("Enter the total number of nodes: ");
29 scanf("%d", &n);
30 createList(n);
31
32 printf("\nData in the list \n");
33 displayList();
34
35 /*
36 * Insert data at the beginning of the singly linked list
37 */
38 printf("\nEnter data to insert at beginning of the list: ");
39 scanf("%d", &data);
40 insertNodeAtBeginning(data);
41
42 printf("\nData in the list \n");
43 displayList();
44
45 return 0;
46}
47
48
49/*
50 * Create a list of n nodes
51 */
52void createList(int n)
53{
54 struct node *newNode, *temp;
55 int data, i;
56
57 head = (struct node *)malloc(sizeof(struct node));
58
59 /*
60 * If unable to allocate memory for head node
61 */
62 if(head == NULL)
63 {
64 printf("Unable to allocate memory.");
65 }
66 else
67 {
68 /*
69 * Input data of node from the user
70 */
71 printf("Enter the data of node 1: ");
72 scanf("%d", &data);
73
74 head->data = data; // Link data field with data
75 head->next = NULL; // Link address field to NULL
76
77 temp = head;
78
79 /*
80 * Create n nodes and adds to linked list
81 */
82 for(i=2; i<=n; i++)
83 {
84 newNode = (struct node *)malloc(sizeof(struct node));
85
86 /* If memory is not allocated for newNode */
87 if(newNode == NULL)
88 {
89 printf("Unable to allocate memory.");
90 break;
91 }
92 else
93 {
94 printf("Enter the data of node %d: ", i);
95 scanf("%d", &data);
96
97 newNode->data = data; // Link data field of newNode with data
98 newNode->next = NULL; // Link address field of newNode with NULL
99
100 temp->next = newNode; // Link previous node i.e. temp to the newNode
101
102 temp = temp->next;
103 }
104 }
105
106 printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
107 }
108}
109
110
111/*
112 * Create a new node and inserts at the beginning of the linked list.
113 */
114void insertNodeAtBeginning(int data)
115{
116 struct node *newNode;
117
118 newNode = (struct node*)malloc(sizeof(struct node));
119
120 if(newNode == NULL)
121 {
122 printf("Unable to allocate memory.");
123 }
124 else
125 {
126 newNode->data = data; // Link data part
127 newNode->next = head; // Link address part
128
129 head = newNode; // Make newNode as first node
130
131 printf("DATA INSERTED SUCCESSFULLY\n");
132 }
133}
134
135
136/*
137 * Display entire list
138 */
139void displayList()
140{
141 struct node *temp;
142
143 /*
144 * If the list is empty i.e. head = NULL
145 */
146 if(head == NULL)
147 {
148 printf("List is empty.");
149 }
150 else
151 {
152 temp = head;
153 while(temp != NULL)
154 {
155 printf("Data = %d\n", temp->data); // Print data of current node
156 temp = temp->next; // Move to next node
157 }
158 }
159}
1#include <iostream>
2// Linked list
3struct node {
4 int data ;
5 node * link;
6};
7
8
9node * Node(int data) {
10 node * temp = new node();
11 temp->data = data;
12 temp->link = NULL;
13 return temp;
14}
15
16void append(node ** head, int data) {
17
18 if(*head == NULL) {
19 *head = Node(data);
20 }else {
21 node * temp = * head;
22 while (temp->link != NULL) {
23 temp=temp->link;
24 }
25 temp->link = Node(data);
26
27 }
28
29}
30
31
32// insertion at begining
33
34void insertBeg(node **head , int data) {
35 if(*head == NULL) {
36 * head = Node(data);
37 }else {
38 node * temp = Node(data);
39 temp->link = *head;
40 *head = temp;
41
42 }
43
44
45}
46// insert at specific position
47
48void addafter(node * head , int loc , int data) {
49 node * temp , * r ;
50 temp = head ;
51 for( int i = 0 ; i<loc;i++ ) {
52 temp = temp->link;
53 if(temp == NULL) {
54 cout<<"there ar less elemtns" ;
55 return;
56 }
57
58 }
59 // insert new node
60 r = Node(data);
61 r->link = temp->link;
62 temp->link = r;
63
64
65}
66
67void display(node * head) {
68
69 node * temp = head;
70 while(temp!= NULL) {
71 cout<<temp->data<<" ";
72 temp = temp->link;
73 }
74}
75
76
77
78int main() {
79 node * head = NULL;
80 append(&head,5);
81 append(&head,5);
82 append(&head,5);
83 append(&head,5);
84 display(head);
85 cout<<endl;
86 insertBeg(&head,6);
87 insertBeg(&head,6);
88 insertBeg(&head,6);
89 display(head);
90addafter(head,4,7);
91cout<<endl;
92display(head);
93
94 return 0;
95}
96