-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.c
More file actions
77 lines (71 loc) · 1.5 KB
/
SinglyLinkedList.c
File metadata and controls
77 lines (71 loc) · 1.5 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
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//#include<alloc.h>
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
// c function to get a node from the available list
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf(" OUT OF MEMORY \n");
exit(0);
}
return x;
}
// c function to insert a node at the front end of the list
NODE insert_front(int item,NODE first)
{
NODE temp;
temp=getnode();
temp->info=item;
temp->link=first;
return temp;
}
// c function to display the contents of singly linked list
void display(NODE first)
{
NODE temp;
if(first==NULL)
{
printf(" List is empty \n");
return;
}
printf(" The contents of the list are : \n");
temp=first;
while(temp!=NULL)
{
printf("%d\n",temp->info);
temp=temp->link;
}
printf("\n");
}
// main
void main()
{
NODE first=NULL; //to start,list is empty
int choice,item;
for(;;)
{
printf(" 1.Insert at front end 2.Display 3.Quit \n");
printf(" Enter choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf(" Enter item to be inserted\n");
scanf("%d",&item);
first=insert_front(item,first);
break;
case 2:display(first);
break;
default:exit(0);
}//end of switch
}//end of for loop
}//end of main