1 | #include <stdio.h>
|
2 | #include <stdlib.h>
|
3 | #include <string.h>
|
4 |
|
5 | //------------------------------------------------------------------------------
|
6 |
|
7 | //1.1 struct als globaler Datentyp
|
8 | struct ListElm {int Prio; char *name; struct ListElm *next;};
|
9 |
|
10 | //------------------------------------------------------------------------------
|
11 |
|
12 | //1.3 Ausgabe eines Elements mit Unterprogramm
|
13 | void PrintElm(struct ListElm *z)
|
14 | {
|
15 | printf("-----------------------------------------------------------------");
|
16 | printf("\nPrioritaet: %d", z->Prio);
|
17 | printf("\nName: %s", z->name);
|
18 | printf("\nVerkettungsinfo: %x\n\n", z->next);
|
19 | }
|
20 |
|
21 | //------------------------------------------------------------------------------
|
22 |
|
23 | //1.6 Ausgabe der Liste mit Unterprogramm
|
24 | void PrintList(struct ListElm *Tail)
|
25 | {
|
26 | while (Tail != NULL)
|
27 | {
|
28 | PrintElm(Tail);
|
29 | Tail = Tail->next;
|
30 | }
|
31 |
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 | //______________________________________________________________________________
|
37 | //_____________________________main programm____________________________________
|
38 | //______________________________________________________________________________
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | int main(int argc, char *argv[])
|
44 | {
|
45 | struct ListElm a = {8, "Hase", NULL};
|
46 | struct ListElm *Wurzel, *Hilfe;
|
47 | char bez1[] = "Flo", bez2[] = "Hans", bez3[] = "Sepp";
|
48 |
|
49 | int lSize;
|
50 |
|
51 |
|
52 |
|
53 | //1.2 Ausgabe eines Elements im Programm
|
54 |
|
55 | printf("Groese in Byte: %d ", sizeof (a));
|
56 | printf("\nPrioritaet: %d", a.Prio);
|
57 | printf("\nName: %s", a.name);
|
58 | printf("\nVerkettungsinfo: %x\n\n", a.next);
|
59 |
|
60 |
|
61 |
|
62 | //1.3 Ausgabe eines Elements mit Unterprogramm
|
63 |
|
64 | PrintElm(&a);
|
65 |
|
66 | system("pause");
|
67 | system("cls");
|
68 |
|
69 |
|
70 |
|
71 | //1.4 Erzeugen eines Elements durch malloc mit anschließender Ausgabe
|
72 |
|
73 | lSize = sizeof(struct ListElm);
|
74 | Wurzel = (struct ListElm *) malloc(lSize);
|
75 |
|
76 | Wurzel->Prio = 7;
|
77 |
|
78 | lSize = strlen(bez3)+1;
|
79 | Wurzel->name=(char *) malloc(lSize);
|
80 | strcpy(Wurzel->name,bez3);
|
81 |
|
82 | Wurzel->next = NULL;
|
83 |
|
84 | PrintElm(Wurzel);
|
85 |
|
86 | system("Pause");
|
87 | system("cls");
|
88 |
|
89 |
|
90 |
|
91 | //1.5 Element vorne anhängen
|
92 |
|
93 | lSize = sizeof(struct ListElm);
|
94 | Hilfe = (struct ListElm *) malloc(lSize);
|
95 |
|
96 | Hilfe->Prio = 3;
|
97 |
|
98 | lSize = strlen(bez2)+1;
|
99 | Hilfe->name=(char *) malloc(lSize);
|
100 | strcpy(Hilfe->name,bez1);
|
101 |
|
102 | Hilfe->next = Wurzel;
|
103 |
|
104 | Wurzel = Hilfe;
|
105 |
|
106 |
|
107 | PrintElm(Wurzel);
|
108 | PrintElm(Wurzel->next);
|
109 |
|
110 |
|
111 |
|
112 | //6. Ausgabe der Liste mit Unterprogramm
|
113 |
|
114 | PrintList(Wurzel);
|
115 |
|
116 | system("Pause");
|
117 | system("cls");
|
118 |
|
119 |
|
120 |
|
121 | //7. Neues Element an zweiter Stelle einfügen
|
122 |
|
123 | lSize = sizeof(struct ListElm);
|
124 | Hilfe = (struct ListElm *) malloc(lSize);
|
125 |
|
126 | Hilfe->Prio = 2;
|
127 |
|
128 | lSize = strlen(bez2)+1;
|
129 | Hilfe->name=(char *) malloc(lSize);
|
130 | strcpy(Hilfe->name,bez2);
|
131 |
|
132 | Hilfe->next = Wurzel->next;
|
133 | Wurzel->next = Hilfe;
|
134 |
|
135 |
|
136 | PrintList(Wurzel);
|
137 |
|
138 |
|
139 | system("PAUSE");
|
140 | return 0;
|
141 | }
|
142 | C-Code
|