0% found this document useful (0 votes)
1 views10 pages

LinkedList Programs

The document contains multiple C programs that demonstrate the creation and manipulation of singly linked lists. Key functionalities include creating a linked list, inserting nodes at the beginning and end, displaying the list, counting nodes, and reversing the list. Each program includes necessary structures, functions, and user prompts for interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views10 pages

LinkedList Programs

The document contains multiple C programs that demonstrate the creation and manipulation of singly linked lists. Key functionalities include creating a linked list, inserting nodes at the beginning and end, displaying the list, counting nodes, and reversing the list. Each program includes necessary structures, functions, and user prompts for interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C PROGRAM TO CREATE A SINGLY LINKED LIST

#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;

/* Create a singly linked list of n nodes*/


printf("Enter the total number of nodes: ");
scanf("%d", &n);
createList(n);

printf("\nData in the list \n");


displayList();

/*
* 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);

printf("\nData in the list \n");


displayList();

return 0;
}

/* Create a list of n nodes*/


void createList(int n)
{
struct node *newNode, *temp;
int data, i;

head = (struct node *)malloc(sizeof(struct node));

/*
* 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);

head->data = data; // Link data field with data


head->next = NULL; // Link address field to NULL
temp = head;

/*Create n nodes and adds to linked list */


for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */


if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);

newNode->data = data; // Link data field of newNode with data


newNode->next = NULL; // Link address field of newNode with NULL
temp->next = newNode; // Link previous node i.e. temp to the newNode
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}
/*Create a new node and inserts at the beginning of the linked list.*/
void insertNodeAtBeginning(int data)
{
struct node *newNode;

newNode = (struct node*)malloc(sizeof(struct node));

if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; // Link data part
newNode->next = head; // Link address part

head = newNode; // Make newNode as first node

printf("DATA INSERTED SUCCESSFULLY\n");


}
}
/*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 data of current node
temp = temp->next; // Move to next node
}
}
}

PROGRAM LINKED LIST_INSERTION_AT END


POSITION
#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 insert_at_end()
{
struct node *new_node,*temp;

new_node=(struct node *)malloc(sizeof(struct node));

if(new_node == NULL)
printf("nFailed to Allocate Memory");

printf("\n Enter the data : ");


scanf("%d",&new_node->data);
new_node->link=NULL;

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

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("\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;

/* Functions used in the program */


void createList(int n);
void reverseList();
void displayList();

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);

printf("\nData in the list \n");


displayList();

/*
* Reverse the list
*/
printf("\nPress 1 to reverse the order of singly linked list\n");
scanf("%d", &choice);
if(choice == 1)
{
reverseList();
}

printf("\nData in the list\n");


displayList();

return 0;
}

/*
* Create a list of n nodes
*/
void createList(int n)
{
struct node *newNode, *temp;
int data, i;

head = (struct node *)malloc(sizeof(struct node));

/*
* 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);

head->data = data; // Link the data field with data


head->next = NULL; // Link the address field to NULL

temp = head;

/*
* Create n nodes and adds to linked list
*/
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));

/* If memory is not allocated for newNode */


if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of node %d: ", i);
scanf("%d", &data);

newNode->data = data; // Link the data field of newNode with data


newNode->next = NULL; // Link the address field of newNode with NULL

temp->next = newNode; // Link previous node i.e. temp to the newNode


temp = temp->next;
}
}

printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");


}
}

/*
* 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;

prevNode->next = NULL; // Make first node as last node

while(head != NULL)
{
head = head->next;
curNode->next = prevNode;

prevNode = curNode;
curNode = head;
}

head = prevNode; // Make last node as head

printf("SUCCESSFULLY REVERSED LIST\n");


}
}

/*
* 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
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy