TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Insertion in Singly Linked List at Beginning

Insertion in Singly Linked List at Beginning with Introduction, Asymptotic Analysis, Array, Pointer, Structure, Singly Linked List, Doubly Linked List, Circular Linked List, Binary Search, Linear Search, Sorting, Bucket Sort, Comb Sort, Shell Sort, Heap Sort, Merge Sort, Selection Sort, Counting Sort, Stack, Qene, Circular Quene, Graph, Tree, B Tree, B+ Tree, Avl Tree etc.

<< Back to INSERTION

Insertion in singly linked list at beginning

Inserting a new element into a singly linked list at beginning is quite simple. We just need to make a few adjustments in the node links. There are the following steps which need to be followed in order to inser a new node in the list at beginning.

  • Allocate the space for the new node and store data into the data part of the node. This will be done by the following statements.
ptr = (struct node *) malloc(sizeof(struct node *));
			ptr → data = item 
  • Make the link part of the new node pointing to the existing first node of the list. This will be done by using the following statement.
ptr->next = head;
  • At the last, we need to make the new node as the first node of the list this will be done by using the following statement.
head = ptr;

Algorithm

  • Step 1: IF PTR = NULL
  • Write OVERFLOW
         Go to Step 7
        [END OF IF]

  • Step 2: SET NEW_NODE = PTR
  • Step 3: SET PTR = PTR → NEXT
  • Step 4: SET NEW_NODE → DATA = VAL
  • Step 5: SET NEW_NODE → NEXT = HEAD
  • Step 6: SET HEAD = NEW_NODE
  • Step 7: EXIT

Insertion in singly linked list at beginning

C Function

#include<stdio.h>
#include<stdlib.h>
void beginsert(int);
struct node
{
	int data;
	struct node *next;
};
struct node *head;
void main ()
{
	int choice,item;
	do 
	{
		printf("\nEnter the item which you want to insert?\n");
		scanf("%d",&item);
		beginsert(item);
		printf("\nPress 0 to insert more ?\n");
		scanf("%d",&choice);
	}while(choice == 0);
}
void beginsert(int item)
	{
		struct node *ptr = (struct node *)malloc(sizeof(struct node *));
		if(ptr == NULL)
		{
			printf("\nOVERFLOW\n");
		}
		else
		{
			ptr->data = item;
			ptr->next = head;
			head = ptr;
			printf("\nNode inserted\n");
		}
		
	}

Output

Enter the item which you want to insert?
12

Node inserted

Press 0 to insert more ?
0

Enter the item which you want to insert?
23

Node inserted

Press 0 to insert more ?
2

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf