struct.c


1
#include <iostream>
2
#include <iomanip>
3
#include <stdlib.h>
4
#include <string.h>
5
6
using namespace std ;
7
8
9
//------------------------------------------------------------------------------
10
11
//struct als globaler Datentyp
12
struct ListElm {int Prio; char *name; struct ListElm *next;};                 
13
14
//------------------------------------------------------------------------------
15
16
//Ausgabe eines Elements mit Unterprogramm
17
void PrintElm(struct ListElm *z)                                                
18
{
19
  cout << "------------------------------------"<< endl
20
    << "Prioritaet.....: " << dec << z->Prio << endl
21
    << "Name...........: " <<        z->name << endl
22
    << "Verkettungsinfo: " << hex << z->next << endl
23
    << dec;
24
}
25
26
//------------------------------------------------------------------------------
27
28
//Ausgabe der Liste mit Unterprogramm
29
void PrintList(struct ListElm *Tail)                                      
30
{
31
  while (Tail != NULL)
32
  {
33
    PrintElm(Tail);
34
    Tail = Tail->next;
35
  }  
36
}
37
38
//------------------------------------------------------------------------------
39
40
//2.2 Unterprogramm zur Erzeugung eines neuen Elements 
41
42
struct ListElm* Neues_Element(int prio, const char* s, struct ListElm *naechste)
43
{
44
  struct ListElm *a; 
45
46
  a = new struct ListElm;
47
  a->Prio = prio;
48
49
  a->name = new char[strlen(s)+1];
50
  strcpy(a->name,s);
51
52
  a->next = naechste;
53
54
  return a;    
55
}
56
57
58
59
60
//______________________________________________________________________________
61
//_____________________________main programm____________________________________
62
//______________________________________________________________________________
63
64
65
66
67
int main(int argc, char *argv[])
68
{
69
  struct ListElm *Wurzel;
70
71
  Wurzel = Neues_Element(1, "Sepp", NULL);
72
  PrintList(Wurzel);
73
74
  Wurzel = Neues_Element(2, "Hans", Wurzel);
75
  PrintList(Wurzel);
76
77
  system("pause");
78
  system("cls");
79
80
  Wurzel = NULL;
81
82
83
  //2.3 Aufträge von Konsole einlesen und in Liste ketten
84
85
  cout << "Es werden solange neue Listenelemente erstellt, bis \"stop\" als" 
86
    << "\nName eingegeben wird!\n";
87
88
89
  short vergl; 
90
  char c[32], temp[32];
91
  int Prioritaet, i=1,j=0;
92
93
  do 
94
  {   
95
    cin.sync();
96
    cin.clear();
97
98
    cout << "\n\nListenelement Nr. " << i << ":\n";  
99
    cout << "Name des Auftrags: ";
100
    cin  >> c;
101
102
103
    //Prüfen ob "stop" eingegeben wurde:
104
105
    for(; c[j] != '\0'; j++)
106
    {
107
      temp[j] = tolower(c[j]); //umwandeln in Kleinbuchstaben
108
    }         
109
    temp[j] = '\0';
110
111
    vergl = strcmp(temp,"stop");
112
    if (vergl == 0) break;
113
114
    cout << "Prioritaet des Auftrags: ";
115
    cin  >> Prioritaet;
116
117
    if(i==1) Wurzel = Neues_Element(Prioritaet, c, NULL);
118
    else Wurzel = Neues_Element(Prioritaet, c, Wurzel);
119
120
    i++;    
121
  }
122
123
  while (vergl != 0); 
124
  cout << "\n\nEnde der Liste. ";
125
126
127
  system("pause");
128
  system("cls");
129
130
131
132
  cout << "Ausgabe der Liste:\n\n";
133
  PrintList(Wurzel);
134
135
136
137
138
  system("PAUSE");  
139
  return 0;
140
}