WCM Forum

WCM Forum (http://www.wcm.at/forum/index.php)
-   Programmierung (http://www.wcm.at/forum/forumdisplay.php?f=17)
-   -   C- Doppelt verkettete Liste (http://www.wcm.at/forum/showthread.php?t=59156)

3of4 09.06.2002 20:02

C- Doppelt verkettete Liste
 
Muss bis Dienstag die Hausübung fertig haben, habe aber jetzt schon seit über einem Jahr kein C mehr gemacht so das ich vollkommen draußen bin.
Die aufgabe: eine doppelt verkettete Liste, mit einfügen suchen löschen...

Bei mir hängt er im Moment an der Zeile:
struct node *temp;

Wieso, und kann mir jemand bitte schnell helfen!!!


#include<stdio.h>
#include<stdlib.h>

struct node{
int Katnr;
int Ktostand;
struct node *prev;
struct node *next;
};

void insert(int zahl1, int zahl2, struct node *point);
void main(){
struct node *root=NULL, *found;
char ein[130];
int z1,z2,loop=1,menu;
while(loop){
printf("0 Datensatz eingeben\n1 Datensatz suchen\n2 Datensatz l”schen\n3 Beenden\n");
menu=atoi(gets(ein));
switch(menu)
{
case 0:
struct node *temp;
printf("\nBitte Katalognummer eingeben: ");
z1=atoi(gets(ein));
printf("\nBitte Kontostand eingeben: ");
z2=atoi(gets(ein));
if(root==NULL){
root=malloc(sizeof(struct node));
root->Katnr=z1;
root->Ktostand=z2;
}
else insert(z1,z2,root);
break;
case 3: loop=0;
break;
default: printf("Bitte gltige Zahl eingeben");
}
}
}
void insert(int zahl1, int zahl2, struct node *point){
if(point->next==NULL){
struct node *new;
new = (struct node *)malloc(sizeof(struct node));
new->Katnr=zahl1;
new->Ktostand=zahl2;
new->prev=point;
point->next=new;
}
else
insert (zahl1, zahl2, point->next);
}

irrsinn 09.06.2002 20:28

eine genaue compiler fehlermeldung wär in so einem fall zwar immer hilfreich, aber in so einem fall würd ich sagen:

in wirklich reinem ansi c darfst nur ganz oben vor der ersten anweisung variablen instanzieren. und innerhalb einer switch/case anweisung ists schon gar nicht erlaubt.

3of4 09.06.2002 20:46

stimmt, die deklaration habe ich am Anfang der schleife machen müssen, aber jetzt habe ich schon wieder ein anderes Problem:
Beim Einfügen findet er neue Daten die gar nicht existieren, aber struct node *prev = NULL darf ich auch nicht schreiben.

#include<stdio.h>
#include<stdlib.h>

struct node{
int Katnr;
int Ktostand;
struct node *prev;
struct node *next;
};
void show(struct node *point);
void insert(int zahl1, int zahl2, struct node *point);
void main(){
struct node *root=NULL, *found;
char ein[130];
int z1,z2,loop=1,menu;
struct node *temp;
while(loop){
printf("\n\n0 Datensatz eingeben\n1 Datensatz suchen\n2 Datensatz l”schen\n3 Beenden\n4 Datens„tze ausgeben\n");
menu=atoi(gets(ein));
switch(menu)
{
case 0:
printf("\nBitte Katalognummer eingeben: ");
z1=atoi(gets(ein));
printf("\nBitte Kontostand eingeben: ");
z2=atoi(gets(ein));
if(root==NULL){
root=malloc(sizeof(struct node));
root->Katnr=z1;
root->Ktostand=z2;
}
else insert(z1,z2,root);
break;
case 3: loop=0; break;
case 4: show(root); break;
default: printf("Bitte gltige Zahl eingeben");
}
}
}
void insert(int zahl1, int zahl2, struct node *point){
if(point->next==NULL){
struct node *new;
new = (struct node *)malloc(sizeof(struct node));
new->Katnr=zahl1;
new->Ktostand=zahl2;
new->prev=point;
point->next=new;
}
else
insert (zahl1, zahl2, point->next);
}
void show(struct node *point){
if(point!=NULL){
printf("\nKatalognummer: %3d Guthaben: %3d",point->Katnr,point->Ktostand);
}
else printf("Keine Daten vorhanden");
if(point->next!=NULL){
show(point->next);
}
}

Sonic 09.06.2002 20:49

Zitat:

Original geschrieben von irrsinn
in wirklich reinem ansi c darfst nur ganz oben vor der ersten anweisung variablen instanzieren. und innerhalb einer switch/case anweisung ists schon gar nicht erlaubt.
Und sollte das nicht der Fehler sein brauchen wir mehr Infos!
Achja beim setzen des rootknoten hast du vergessen prev und next auf NULL zu setzen und beim malloc fehlt dir der Typecast ;)

Code:

if(root==NULL){
  root=(struct node*)malloc(sizeof(struct node));
  root->Katnr=z1;
  root->Ktostand=z2;
  root->prev=NULL;
  root->next=NULL;
}

lg

irrsinn 09.06.2002 20:50

wennst null nicht schreiben darfst, musst dir selber das define machen - also

#define NULL 0

oder du schreibst einfach = 0 statt NULL. dass er daten findet, wenn der pointer irgendwo hin zeigt is auch klar.

3of4 12.06.2002 18:06

So kann mir jemand sagen was an der Funktiondeklaration:

Zitat:

struct node *search(int zahl1, struct node *point);
falsch ist? Ich kriege immer einen Expression Syntax Error.

hier ist der Aufruf:

Zitat:

case 1:
printf("\nBitte Katalognummer eingeben: ");
z1=atoi(gets(ein));
temp=search(z1,root);
if(temp!=NULL){
printf("\nKatalognummer: %d gefunden, Kontostand: %d",z1,temp->Ktostand);
}
else{
printf("\nKatalognummer: %d nicht gefunden, bitte Eingabe berprfen",z1);
}
break;
und hier ist die Funktion:
Zitat:

struct node * search(int zahl1, struct node *point){
if(point.Katnr==zahl1){
return point;
}
else if(point->next!=null){
return search(zahl1, point->next);
}
else{
return NULL;
}
}

irrsinn 12.06.2002 18:37

bitte um genaue angaben bei welcher zeile der compiler schreit und welche meldung er von sich gibt. das erspart eine menge arbeit!!!

3of4 12.06.2002 18:45

in der Funktionsdeklaration, (das was ich ganz oben geposted habe) und die Meldung ist Illegal Expression Syntax.

irrsinn 12.06.2002 18:51

wird der typ node oberhalb der funktion definiert bzw eine datei in der diese definition vorkommt vorher inkludiert?

3of4 13.06.2002 19:28

OK dass nächste Problem:

Hier der Source, und die Fehlermeldungen dazu gleich in Quotes darunter.

Ps: vorher hatte ich einfach eine } gelöscht :( :heul: :heul:
Zitat:

#include<stdio.h>
#include<stdlib.h>

struct node{
int Katnr;
int Ktostand;
struct node *prev;
struct node *next;
};
void show(struct node *point);
void insert(int zahl1, int zahl2, struct node *point);
struct node *search(struct node *point, int Zahl1);
void delete(int zahl1, struct node *root);
void save(char pfad[], struct node *root);
struct node *load(char pfad[]);
void main(){
struct node *root=NULL, *found;
char ein[130];
int z1,z2,loop=1,menu;
struct node *temp;
while(loop){
printf("\n\n0 Datensatz eingeben\n1 Datensatz suchen\n2 Datensatz l”schen\n3 Datens„tze ausgeben\n4 Speichern\n5 Laden\n6 Beenden\n Eingabe: ");
menu=atoi(gets(ein));
switch(menu)
{
case 0:
printf("\nBitte Katalognummer eingeben: ");
z1=atoi(gets(ein));
printf("\nBitte Kontostand eingeben: ");
z2=atoi(gets(ein));
if(root==NULL){
root=malloc(sizeof(struct node));
root->Katnr=z1;
root->Ktostand=z2;
root->next=NULL;
root->prev=NULL;
}
else insert(z1,z2,root);
getch();
break;
case 1:
printf("\nBitte Katalognummer eingeben: ");
z1=atoi(gets(ein));
temp=search(root, z1);
if(temp!=NULL){
printf("\nKatalognummer: %d gefunden, Kontostand: %d",z1,temp->Ktostand);
}
else{
printf("\nKatalognummer: %d nicht gefunden, bitte Eingabe berprfen",z1);
}
getch();
break;
case 2:
printf("\nBitte Katalognummer eingeben: ");
z1=atoi(gets(ein));
delete(z1,root);
getch();
break;
case 3:
show(root);
getch();
break;
case 4:
printf("\nBitte Pfad und Dateiname eingeben");
gets(ein);
save(ein,root);
getch();
break;
case 6: loop=0;
break;
default: printf("Bitte gltige Zahl eingeben"); getch();
}
}
}
void insert(int zahl1, int zahl2, struct node *point){
if(point->next==NULL){
struct node *new;
new = (struct node *)malloc(sizeof(struct node));
new->Katnr=zahl1;
new->Ktostand=zahl2;
new->prev=point;
new->next=NULL;
point->next=new;
}
else
insert (zahl1, zahl2, point->next);
}

void show(struct node *point){
if(point!=NULL){
printf("\nKatalognummer: %3d Guthaben: %3d",point->Katnr,point->Ktostand);
}
else printf("Keine Daten vorhanden");
if(point->next!=NULL){
show(point->next);
}
}
struct node *search(struct node *point, int zahl1){
if(point->Katnr==zahl1){
return point;
}
else if(point->next!=NULL){
return search(point->next, zahl1);
}
else{
return NULL;
}
}
void delete(int zahl1, struct node *root){
struct node *temp = search( root , zahl1);
if(temp!=NULL){
struct node *tempprev=temp->prev;
struct node *tempnext=temp->next;
printf("Katalognummer %d mit Kontostand %d wurde gel”scht",temp->Katnr,temp->Ktostand);
tempprev->next=tempnext;
tempnext->prev=tempprev;
if(temp==root) root=tempnext;
/*free(temp->Katnr);
free(temp->Ktostand);*/
free(temp);
}
else
printf("\nKatalognummer %d nicht gefunden, bitte Eingabe berprfen",zahl1);
}
void save(char name[], struct node *root){
FILE *fp=fopen(name,"w");
struct node *point=root;
if(fp!=NULL){
while(point!=NULL){
fseek(fp,0L,SEEK_END);
fwrite((void *) &point, sizeof(struct node),1,fp);
point=point->next;
}
printf("\nIn Datei: ");
puts(name);
printf("gespeichert.");
}
else{
printf("\nFehler beim Erstellen der Datei");
}

}
struct node *load(char pfad[]){
struct node *root, *temp;
int n=0;
FILE fp=fopen(pfad,"r");
Zitat:

Error D:\WINDOWS\DESKTOP\LIST.CPP 146: Illegal structure operation in function load
if(fp!=NULL){
Zitat:

Error D:\WINDOWS\DESKTOP\LIST.CPP 147: Illegal structure operation in function load
fseek(fp,0L,SEEK_END);
Zitat:

Error D:\WINDOWS\DESKTOP\LIST.CPP 148: Type mismatch in parameter 'stream' in fseek

Warning D:\WINDOWS\DESKTOP\LIST.CPP 148: Possible use of 'fp' before definition
root=temp=(struct node *)malloc((size_t)ftell(fp));
Zitat:

Error D:\WINDOWS\DESKTOP\LIST.CPP 149: Type mismatch in parameter 'stream' in function load
Warning D:\WINDOWS\DESKTOP\LIST.CPP 149: Possible use of 'fp' before definition
rewind(fp);
while(fread(p,sizeof(struct node),1,fp){
Zitat:

Error D:\WINDOWS\DESKTOP\LIST.CPP 150: Type mismatch in parameter 'stream' in function load
p+=1;
n++;
}
temp=root;
while(n--){
printf("\nKatalognummer: %d Kontostand: %d",p->Katnr,p->Ktostand);
}
}
else
printf("Fehler beim Lesen der Datei, bitte Eingabe berprfen");
}
}


Alle Zeitangaben in WEZ +2. Es ist jetzt 05:15 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© 2009 FSL Verlag