LinkedList Programs
LinkedList Programs
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
//-------------------------------------------------
struct node// node definition
{
int data;
struct node *link;
}
*start=NULL;
//------------------------------------------------------------
void create()
{
char ch;
do
{
struct node *new_node,*temp;// declare two node new_node and temp
new_node=(struct node *)malloc(sizeof(struct node));// new_node creation
printf("\n Enter the data : ");
scanf("%d",&new_node->data);// take the input
new_node->link=NULL;// link part is null
if(start==NULL)// if start node is null; i.e. there is not any node present
{
start=new_node; // assign the new_node to start
temp=new_node;// assign the new_node to temp.
}
else
{
temp->link=new_node;// if start node is not null; then address of new_node is store in temp->link part
temp=new_node;// temp contains the new_node
}
printf("Do you want to creat another : ");
ch=getche();//getche() is used to get a character from console, and echoes to the screen.
}while(ch!='n');
}
//------------------------------------------------------------------
void display()
{
struct node *new_node;
printf("The Linked List : \n");
new_node=start;
while(new_node!=NULL)
{
printf("%d--->",new_node->data);
new_node=new_node->link;
}
printf("NULL");
}
//----------------------------------------------------
void main()
{
create();
display();
}
//----------------------------------------------------
C PROGRAM TO INSERT A NEW NODE AT THE
BEGINNING OF A SINGLY LINKED LIST
#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node {
int data; // Data
struct node *next; // Address
}*head;
void createList(int n);
void insertNodeAtBeginning(int data);
void displayList();
int main()
{
int n, data;
/*
* Insert data at the beginning of the singly linked list
*/
printf("\nEnter data to insert at beginning of the list: ");
scanf("%d", &data);
insertNodeAtBeginning(data);
return 0;
}
/*
* If unable to allocate memory for head node
*/
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
/*Input data of node from the user*/
printf("Enter the data of node 1: ");
scanf("%d", &data);
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; // Link data part
newNode->next = head; // Link address part
/*
* If the list is empty i.e. head = NULL
*/
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print data of current node
temp = temp->next; // Move to next node
}
}
}
void create()
{
char ch;
do
{
struct node *new_node,*temp;// declare two node new_node and temp
if(start==NULL)// if start node is null; i.e. there is not any node present
{
start=new_node; // assign the new_node to start
temp=new_node;// assign the new_node to temp.
}
else
{
temp->link=new_node;// if start node is not null; then address of new_node is store in temp->link part
temp=new_node;// temp contains the new_node
}
if(new_node == NULL)
printf("nFailed to Allocate Memory");
if(start==NULL)
{
start=new_node;
temp=new_node;
}
else
{
temp = start;
while(temp->link!=NULL)
{
temp = temp->link;
}
temp->link = new_node;
}
}
void display()
{
struct node *new_node;
printf("The Linked List : \n");
new_node=start;
while(new_node!=NULL)
{
printf("%d--->",new_node->data);
new_node=new_node->link;
}
printf("NULL");
}
//----------------------------------------------------
void main()
{
create();
display();
insert_at_end();
display();
}
//-----------------------------
PROGRAM LINKED LIST_COUNT NUMBER OF NODES
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
//-------------------------------------------------
struct node// node definition
{
int data;
struct node *link;
}
*start=NULL;
//------------------------------------------------------------
void create()
{
char ch;
do
{
struct node *new_node,*temp;// declare two node new_node and temp
if(start==NULL)// if start node is null; i.e. there is not any node present
{
start=new_node; // assign the new_node to start
temp=new_node;// assign the new_node to temp.
}
else
{
temp->link=new_node;// if start node is not null; then address of new_node is store in temp->link part
temp=new_node;// temp contains the new_node
}
void display()
{
struct node *new_node;
printf("\n \n The Linked List : ");
new_node=start;
while(new_node!=NULL)
{
printf("%d--->",new_node->data);
new_node=new_node->link;
}
printf("NULL");
}
//----------------------------------------------------
void count()
{
struct node *temp;
int length = 0;
temp = start;
while(temp!=NULL)
{
length++;
temp=temp->link;
}
printf("\n\n Length of Linked List : %d",length);
}
//----------------------------------------------------
void main()
{
create();
display();
count();
}
//----------------------------------------------------
PROGRAM TO REVERE A SINGLY LINKED LIST
/**
* C program to reverse a Singly Linked List
*/
#include <stdio.h>
#include <stdlib.h>
/* Structure of a node */
struct node {
int data; //Data part
struct node *next; //Address part
}*head;
int main()
{
int n, choice;
/*
* Create a singly linked list of n nodes
*/
printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);
/*
* Reverse the list
*/
printf("\nPress 1 to reverse the order of singly linked list\n");
scanf("%d", &choice);
if(choice == 1)
{
reverseList();
}
return 0;
}
/*
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
/*
* If unable to allocate memory for head node
*/
if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
/*
* Read data of node from the user
*/
printf("Enter the data of node 1: ");
scanf("%d", &data);
temp = head;
/*
* Create n nodes and adds to linked list
*/
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
/*
* Reverse the order of nodes of a singly linked list
*/
void reverseList()
{
struct node *prevNode, *curNode;
if(head != NULL)
{
prevNode = head;
curNode = head->next;
head = head->next;
while(head != NULL)
{
head = head->next;
curNode->next = prevNode;
prevNode = curNode;
curNode = head;
}
/*
* Display entire list
*/
void displayList()
{
struct node *temp;
/*
* If the list is empty i.e. head = NULL
*/
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data); // Print the data of current node
temp = temp->next; // Move to next node
}
}
}