forked from gunanksood/C-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly_linked_list.c
More file actions
62 lines (62 loc) · 1.41 KB
/
singly_linked_list.c
File metadata and controls
62 lines (62 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Node
{
int info;
struct Node *next;
};
struct node* createNodeList(int n)
{
struct Node *start=NULL;
struct Node *temp,*new_node;
int no,i;
start=(struct Node*)malloc(sizeof(struct Node));
if(start==NULL)
printf("memory can not be allocated\n");
else
{
printf("enter data for node 1:\n");
scanf("%d",&no);
start->info=no;
start->next=NULL;
temp=start;
for(i=2;i<=n;i++)
{
//printf("in loop\n");
new_node=(struct Node*)malloc(sizeof(struct Node));
if(new_node==NULL)
printf("memory can not be allocated\n");
else
{
printf("enter data for node %d:\n",i);
scanf("%d",&no);
new_node->info=no;
new_node->next=NULL;
temp->next=new_node;
temp=temp->next;
}
}
}
return start;
};
void displaynodelist(struct Node*start)
{
struct Node*temp;
temp=start;
printf("\nLINKED LIST IS:- ");
while(temp!=NULL)
{
printf("%d-> ",temp->info);
temp=temp->next;
}
printf("NULL");
}
int main()
{
int n;
printf("ENTER NO OF NODES YOU WANT TO INSERT IN LINKED LIST!:\n");
scanf("%d",&n);
struct Node*start=createNodeList(n);
displaynodelist(start);
}