0% found this document useful (0 votes)
3 views

Linked List C Program

The document contains a C program that creates a singly linked list based on user input. It prompts the user for the number of nodes and their respective data, then constructs the linked list and prints the data for each node. The program utilizes dynamic memory allocation for node creation.

Uploaded by

Tauseef Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Linked List C Program

The document contains a C program that creates a singly linked list based on user input. It prompts the user for the number of nodes and their respective data, then constructs the linked list and prints the data for each node. The program utilizes dynamic memory allocation for node creation.

Uploaded by

Tauseef Ahmad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

/

***************************************************************************
***

Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

***************************************************************************
****/
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};

int main()
{
int i, n, num;

struct node *start, *nnode, *temp;


printf(" Input the number of nodes : ");
scanf("%d", &n);
start=(struct node *)malloc(sizeof(struct node));

printf(" Input data for node 1 : ");


scanf("%d", &num);
start->data = num;
start->next = NULL;
temp = start;
for(i=2;i<=n;i++)
{
nnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node %d : ", i);
scanf(" %d", &num);

nnode->data = num;
nnode->next = NULL;

temp->next = nnode;
temp = temp->next;
}
while (start!=NULL)
{
printf(" Data = %d\n", start->data);
start = start->next;
}

/*struct node *START=NULL, *SECOND, *THIRD;


START=(struct node *)malloc(sizeof(struct node));
//SECOND=(struct node *)malloc(sizeof(struct node));
//THIRD=(struct node *)malloc(sizeof(struct node));
//START=malloc(sizeof(struct node));
START->data=50;
START->next=NULL;
SECOND=(struct node *)malloc(sizeof(struct node));
SECOND->data=60;
SECOND->next=NULL;
START->next=SECOND;
THIRD=(struct node *)malloc(sizeof(struct node));
THIRD->data=70;
THIRD->next=NULL;
SECOND->next=THIRD;
printf("\n%d\n%d\n%d",START->data, SECOND->data, THIRD->data);*/
return 0;
}

You might also like