-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple linked list
127 lines (121 loc) · 2.58 KB
/
simple linked list
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <stdio.h>
#include <stdlib.h>
typedef char LEntry;
typedef struct LNode //the strcuture for the nodes in the list
{
int entry;
struct LNode*next;
}LNode;
typedef int position;
typedef struct list
{
int count;
LNode *head;
}list;
/*the funtions declarations*/
/*the function that creates the list */
int CreateList(int n,list*list,int element);
/*function that searches through the list*/
int Search(position p,list*list,LNode**current);
/*function that inserts elements into the list*/
int Insert(position p,int x,list*list);
/*the function that allocates space from the opearing
system and enters the element to be stored*/
LNode *MakeLNode(int element);
/*function that deletes from the elements the list*/
int Delete();
/* the main function for the linked list
with all the required functions*/
int main()
{
int i;// for moving between the list during creation
list*List; // the list
int x; // the elements being entered
int n;// the number of elements
printf ("enter the number of elements in the list\n");
scanf("%d",&n);
printf("enter your elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&x);
CreateList(n,List,x);
}
return 0;
}
/* The create function that will bulld up the list*/
int CreateList(int n,list*list,int element)
{
int i;
position p=0;
list->count=n;
LNode* newnode,*current;
if (list->count<0)
{
printf("there is an attempt to enter what is no in the list");
}
if(p==0)
{
newnode=MakeLNode(element);
newnode->next=list->head;
list->head=newnode;
}else
{
newnode->next=current->next;
current->next=newnode;
}
list->count++;
p++;
}
int Search(position p,list*list,LNode**current) // function is meant to search for a location within the list
{
int count; //used in the loop
LNode *q;//used to move through ou the list
if (p<0||p>=list->count)
{
printf("there is an attempt to enter what is no in the list");
}
else
{
q=list->head;
for(count=0;count<p;count++)
{
q =q->next;
}
*current=q;
}
}
int Insert(position p,int x,list*list)// it wiil insert after
{
LNode * newnode,*current;
if (p<0||p>=list->count)
{
printf("there is an attempt to enter what is no in the list");
}
else
{
newnode=MakeLNode(x);
if (p==0)
{
newnode->next=list->head;
list->head=newnode;
}
else
{
Search(p-1,list,¤t);
newnode->next=current->next;
current->next=newnode;
}
list->count++;
}
}
LNode *MakeLNode(int x)
{
LNode *p=malloc(sizeof(LNode));// get space from the system
if (p)
{
p->entry=x;
p->next=NULL;
}else
printf("not enough space for additional information");
return p;
}