-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
109 lines (81 loc) · 2.02 KB
/
test.c
File metadata and controls
109 lines (81 loc) · 2.02 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
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
#include <stdio.h>
#include <stdlib.h>
#include "helper.h"
void addpost(struct medlem *);
void printposts();
void printwelcome(){
printf("Welcome, what do you want to do: \n");
printf("Add a Post: (1)\n");
printf("Delete a Post:(2)\n");
printf("Print posts (3)\n");
printf("Exit (4)\n");
}
int main() {
struct medlem * head= NULL;
int RUN = 1;
int selection =0;
do {
printwelcome();
scanf("%d", &selection);
switch (selection) {
case 1:
addpost(head);
printf("%p\n",head);
break;
case 2:
//deletepost();
break;
case 3:
printposts();
break;
case 4:
RUN = 0;
printf("Bye bye \n");
break;
default:
printf("Not valid selection \n");
}
} while (RUN);
return 0;
}
void addpost(struct medlem * head){
printf("addpost allocating:\n");
if(head == NULL ){
head = malloc(sizeof(struct medlem));
if (head == NULL)
printf("Error allocating:\n");
printf("Give a nr:\n");
scanf("%d", &head->nr);
printf("Give a name:\n");
scanf("%s", head->namn);
head->next= NULL;
printf("Added Head at adress %p:\n",head);
}
else if (head != NULL) {
struct medlem * nymedlem = malloc(sizeof(struct medlem));
if (nymedlem == NULL)
printf("Error allocating:\n");
printf("Give a nr:\n");
scanf("%d", &nymedlem->nr);
printf("Give a name:\n");
scanf("%s", nymedlem->namn);
nymedlem->next= NULL;
struct medlem * tmphead= head;
while (tmphead->next != NULL)
tmphead=tmphead->next;
tmphead->next = nymedlem;
printf("Added ny medlem:\n");
}
else
printf("Error allocating memory \n");
}
void printposts()
{
struct medlem * tmphead= NULL;
while (tmphead != NULL) {
printf("Nr is %d\n", tmphead->nr);
printf("Name is %s\n", tmphead->namn);
tmphead=tmphead->next;
}
printf("printpost was called\n");
}