0% found this document useful (0 votes)
9 views2 pages

DS Programs

The document provides a C program for inserting a new node at the start of a single linked list. It defines a structure for the linked list nodes, functions to insert a node and display the list, and demonstrates the functionality in the main function. The program initializes a linked list, inserts a new element at the beginning, and displays the updated list.

Uploaded by

sudhadegree
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)
9 views2 pages

DS Programs

The document provides a C program for inserting a new node at the start of a single linked list. It defines a structure for the linked list nodes, functions to insert a node and display the list, and demonstrates the functionality in the main function. The program initializes a linked list, inserts a new element at the beginning, and displays the updated list.

Uploaded by

sudhadegree
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

INSERTION IN SINGLE LINKED LIST

#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void insertStart (struct Node **head, int data)
{
struct Node *newNode = (struct Node *) malloc (sizeof (struct Node));
newNode->data = data;
newNode->next = *head;
//changing the new head to this freshly entered node
*head = newNode;
}
void display (struct Node *node)
{
//as linked list will end when Node is Null
while (node != NULL)
{
printf ("%d ", node->data);
node = node->next;
}
printf ("\n");
}
int main ()
{
//creating 4 pointers of type struct Node
//So these can point to address of struct type variable
struct Node *head = NULL;
struct Node *node2 = NULL;
struct Node *node3 = NULL;
struct Node *node4 = NULL;
// allocate 3 nodes in the heap
head = (struct Node *) malloc (sizeof (struct Node));
node2 = (struct Node *) malloc (sizeof (struct Node));
node3 = (struct Node *) malloc (sizeof (struct Node));
node4 = (struct Node *) malloc (sizeof (struct Node));
head->data = 15; // data set for head node
head->next = node2; // next pointer assigned to address of node2
node2->data = 10;
node2->next = node3;
node3->data = 12;
node3->next = node4;
node4->data = 3;
node4->next = NULL;
printf ("Linklist : ");
display (head);
// Need '&' i.e. address as we need to change head
insertStart (&head, 25);
printf ("\nAfter Inserting Element\n");
printf ("\nLinklist : ");
// no need for '&' as head need not be changed
// only doing traversal
display (head);
return 0;
}

You might also like