0% found this document useful (0 votes)
4 views4 pages

Linklist S 57

The document contains C code for a linked list implementation with functions to create a list, display its contents, and insert a node at the end or at any specified position. It includes error handling for memory allocation and checks for empty lists. The main function interacts with the user to gather input for the number of nodes and their data.

Uploaded by

anishwarushe0725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Linklist S 57

The document contains C code for a linked list implementation with functions to create a list, display its contents, and insert a node at the end or at any specified position. It includes error handling for memory allocation and checks for empty lists. The main function interacts with the user to gather input for the number of nodes and their data.

Uploaded by

anishwarushe0725
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<stdio.

h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*header;
void create_list(int n);
void display();
//void insert_beg(int data);
void insert_end(int data);
int main(){
int n;
int data;
printf("Enter number of nodes:");
scanf("%d",&n);
create_list(n);
display();
// insert_beg(data);
//display();
insert_end(data);
display();
return 0;
}
void create_list(int n){
struct node *newnode,*temp;
int data,i;
newnode=(struct node *)malloc(sizeof(struct node));
if(newnode==NULL){
printf("memory not allocated ");
}
else{
printf("Enter your data :");
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
header=newnode;
temp=newnode;
for(i=2;i<=n;i++){
newnode=(struct node *)malloc(sizeof(struct node));
if(newnode==NULL){
printf("memory not allocated ");
break;
}
else{
printf("Enter your data :");
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
temp->next=newnode;
temp=temp->next;
}
}
}
}
/*void insert_beg(int data){
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter data at begninning\n ");
scanf("%d",&data);
newnode->data=data;
newnode->next=header;
header=newnode;
*/
void insert_end(int data){
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(struct node));
printf("enter data at end\n ");
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
temp=header;
while(temp!=NULL)
temp->next=newnode;
temp=temp->next;
printf("data inserted successfully:\n");
}
void display(){
struct node *temp;
int count=0;
if(header==NULL){
printf("ERROR");
}
else{
temp=header;
while(temp!=NULL){
printf("Data is %d\n",temp->data);
temp=temp->next;
count++;
}

}
printf("total nodes are %d/n",count);
}

newnode at any pos

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*header;
void create_list(int n);
void insert_at_anypost(int data,int pos);
void display();
int main(){
int n,i;
int data;
int pos;
printf("Enter number of nodes:");
scanf("%d",&n);
create_list(n);
display();

insert_at_anypost(data, pos);
display();
return 0;
}
void create_list(int n){
struct node *newnode,*temp;
int data,i;
newnode=(struct node *)malloc(sizeof(struct node));
if(newnode==NULL){
printf("memory not allocated ");
}
else{
printf("Enter your data :");
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
header=newnode;
temp=newnode;
for(i=2;i<=n;i++){
newnode=(struct node *)malloc(sizeof(struct node));
if(newnode==NULL){
printf("memory not allocated ");
break;
}
else{
printf("Enter your data :");
scanf("%d",&data);
newnode->data=data;
newnode->next=NULL;
temp->next=newnode;
temp=temp->next;
}
}
}
}
void insert_at_anypost(int data,int pos){
struct node *newnode,*temp;
newnode=(struct node*)malloc(sizeof(newnode));
int i;
if(newnode==NULL){
printf("Unable to locate memory:");
}
else{
printf("Enter your data :");
scanf("%d",&data);
printf("Enter the position:");
scanf("%d",&pos);
newnode->data=data;
newnode->next=NULL;
temp=header;
for(i=2;i<=(pos-1);i++){
temp=temp->next;
if(temp==NULL)
break;
}
if(temp!=NULL){
newnode->next=temp->next;
temp->next=newnode;
printf("inserted at pos");
}
else{
printf("@@##@#");
}
}
}
void display(){
struct node *temp;
int count=0;
if(header==NULL){
printf("ERROR");
}
else{
temp=header;
while(temp!=NULL){
printf("Data is %d\n",temp->data);
temp=temp->next;
count++;
}

}
printf("total nodes are %d\n",count);
}

You might also like