Einfache Kette / Liste
-
hi,
ich möchte eine einfache Kette / Loste erstellen (habe auch eine Anleitung), aber ich erhalten schon beim ersten Schritt eine Fehlermeldung:#include <stdio.h> #include <stdlib.h> typedef struct TNode; typedef struct { int data; TNode *next; } TNode; int main(int argc, char** argv) { printf("Einfach-verkette Liste"); return (EXIT_SUCCESS); }
Mit dieser Fehlermeldung:
main.c:11:16: warning: useless storage class specifier in empty declaration
main.c:16:5: error: expected specifier-qualifier-list before 'TNode'Was ist falsch?
LG
-
Zeile 4 ist falsch - wenn du einen typedef anlegen willst, mußt du dem Typ auch einen Namen geben. Versuch's mal so:
typedef struct node { int data; struct node *next; } TNode;
-
typedef struct TNode TNode; struct TNode { int data; TNode *next; };
-
Vielen Dank euch, nun bin ich soweit:
/* * File: main.c * Author: Andreas * * Created on 20. Juni 2011, 17:36 */ #include <stdio.h> #include <stdlib.h> //typedef struct TNode; typedef struct TNode { int data; struct TNode *pNext; } TNode; TNode *firstNodeWith(TNode *pList,int x) { if (pList == NULL) return(NULL); // Eintrag nicht gefunden else { if (pList->data == x) { return(pList); } else { return(firstNodeWith(pList->pNext,x)); } } } void Forward(TNode *pList) { if (pList != NULL) { printf("%d",pList->data); Forward(pList->pNext); } } void Backward(TNode *pList) { if (pList != NULL) { Backward(pList->pNext); printf(" %d ",pList->data); } } TNode insert_right(TNode list, int data) { TNode new_node = (TNode) malloc(sizeof(struct TNode)); new_node->data = data; new_node->pNext = list->pNext; list->pNext = new_node; return new_node; } int main(int argc, char** argv) { int wahl; printf("Einfach-verkette Liste\n\n"); while(1) { printf("Waehle eine Option!\n"); printf("1. Daten in Liste\n"); printf("2. Daten auslesen\n"); printf("3. Anfang der Liste\n"); scanf ("%d", &wahl); switch(wahl) { case 1:printf("Wahl 1.\n"); break; case 2:printf("Wahl 2.\n"); break; case 3:printf("Wahl 3.\n"); break; case 4:printf("Wahl 4.\n"); break; default: printf("Es wurde eine falsche Zahl eingegeben."); } } return (EXIT_SUCCESS); }
doch es erscheint jetzt folgender Fehler:
main.c:58:48: error: conversion to non-scalar type requested
main.c:59:10: error: invalid type argument of '->' (have 'TNode')
main.c:60:10: error: invalid type argument of '->' (have 'TNode')
main.c:60:24: error: invalid type argument of '->' (have 'TNode')
main.c:61:6: error: invalid type argument of '->' (have 'TNode')Was ist nun falsch?
-
Zeile 58: TNode muss ein Pointer sein. Falls dir nicht klar ist warum, (und das vermute ich mal, sonst hättest du den Fehler lesen können) solltest du mal eben einen kleinen Schritt zurück machen und dir Pointer noch einmal angucken.
-
ja, das war mir schon klar. Ich habe mir auch ausführliche Informationen zu dem Fehler durchgelesen. Doch egal, wie ich es schreibe (**TNode ...*), ich erhalte immer denselben Fehler.
-
supersass1 schrieb:
Doch egal, wie ich es schreibe (**TNode ...*), ich erhalte immer denselben Fehler.
*TNode ist ja auch Käse - syntaktisch korrekt heißt es
TNode* list
(genau wie bei den übrigen Funktionen ;)).