Linked List



  • Moin zusammen,

    Ich habe gerade begonnen mich ein bissie mit verketteten Listen auseinander zu setzen.
    Im ersten Anlauf hat alles funktioniert.
    Jetzt habe ich aber ein paar Sachen umgebaut. Das heisst ich gebe meiner Funktion NodeEinfuegen() auch den Anfang und das Ende mit.

    -> NODE_PTR NodeEinfuegen(NODE_PTR *start,NODE_PTR *ende, int wert)

    soweit so gut, aber eine Zeile kriege ich nicht hin
    *start->next =neuerNode;

    Ich krieg immer den Fehler: left of "->next" must point to a class/struct/blah.

    Wie muss ich die Zeile schreiben dass es funktioniert? Ich hab schon überall * und & und was mir sonst so eingefallen ist hingeschrieben... nix geht.

    Danke



  • Lass den Stern doch einfach mal weg.



  • oder schreib einen punkt anstatt des pfeils:

    wie MFK meint:
    start->next =neuerNode;

    oder
    *start.next=neuerNode;



  • das wird nicht funktionieren:

    XZ59 schrieb:

    *start.next=neuerNode;

    http://cppreference.com/operator_precedence.html
    Der Derefenenzierungsoperator wird zu erst ausgeführt und dann kommt ein
    " undeklared var error" oder so ähnlich raus

    pVar->blah()
    

    is die kurzfoem für

    (*pVar).blah()
    

    Mfg Shade37337



  • So einfach kannst du es dir nicht machen. Was machst du wenn durch das Einfügen einer Node du einen neuen Anfang oder ein neues Ende hast?

    Schreib am besten eine Klasse Node und eine Klasse LinkedList.



  • So siehts zur Zeit aus... zur Zeit liegt OO-Programmierung noch ausserhalb meiner Möglichkeiten, wesshalb ich das mit den Klassen wahrscheinlich auch gar ned umsetzen könnte.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define WIN32_LEAN_AND_MEAN
    
    typedef unsigned int uint;
    
    typedef struct NODE_TYP 
    {
      uint id_no;
      char name[32];
      uint age;
      struct NODE_TYP *next;
      struct NODE_TYP *prev;
    }NODE, *NODE_PTR;
    
    void traverseList(NODE_PTR head);
    NODE_PTR insertNode(NODE_PTR *head, NODE_PTR *tail, uint id_num,uint age,char *name);
    int deleteNode(NODE_PTR *head, NODE_PTR *tail, uint id_num);
    
    int main(int argc, char argv[])
    {
      //need a head and a tail for every list.
      //atm. List is empty so point to NULL
      NODE_PTR l_head=NULL;
      NODE_PTR l_tail=NULL;
    
      insertNode(&l_head,&l_tail,1,28,"michi");
      insertNode(&l_head,&l_tail,2,29,"muchu");
      traverseList(l_head);
    
      return 0;
    }
    
    void traverseList(NODE_PTR head)
    {
      //this function traverses the list and prints out
      //each node.
    
      //test if head is NULL
      if (head==NULL)
      {
        printf("\nLinked List is empty\n");
        return;
      }//end if
    
      //traverse list while nodes
      while (head==NULL)
      {
        //visit the node, print out,go to next node
        printf("\nNode-Data:\n");
        printf("id...: %d\n", head->id_no);
        printf("name.: %s\n", head->name);
        printf("age..: %d\n", head->age);
        //advance to next node
        head = head->next;
      }//end while
      printf("\n");
    }//end traverseList
    
    NODE_PTR insertNode(NODE_PTR *head, NODE_PTR *tail, uint id_num,uint age,char *name)
    {
      //this function inserts a node at the end of the list
      NODE_PTR newNode =NULL;
    
      //Step1, create new node
      newNode =(NODE_PTR)malloc(sizeof(NODE));
      //fill in data
      newNode->id_no  =id_num;
      newNode->age    =age;
      strcpy_s(newNode->name,name);
      newNode->next   =NULL;
    
      //Step2, check stat of list
      if (head==NULL)
      {
        //empty List, simplest case
        *head=newNode;
        *tail=newNode;
        //return new node
        return(newNode);
      }
      else
      {
        if (*head != NULL && *head == *tail)
        {
          //there is exactly one node in the list
          *head->next =newNode;                    //<- hier tritt der Fehler auf
          *tail =newNode;
          //return new node
          return(newNode);
        }
        else
        {
          //there are 2 or more nodes in the list
          //put new node at the end of the list
          *tail->next =newNode;
          *tail =newNode;
          //return new node
          return(newNode);
        }
      }
    }//end insertNode
    
    int deleteNode(NODE_PTR *head, NODE_PTR *tail, uint id_num)
    {
      //this function deletes a node from the list
      //argument is id_no
      NODE_PTR currPtr =*head;
      NODE_PTR prevPtr =*head;
    
      //test if there is a l.list to delete from
      if(!head)
        return(-1);
    
      //traverse the list and find node to delete
      while (currPtr->id_no != id_num)
      {
        //save this position
        prevPtr =currPtr;
        currPtr =currPtr->next;
      }
    
      //at this point we have found the node
      //or the end of the list
      if (currPtr == NULL)
        return(-1);//EndOfList
    
      //Record was found, delete it, but be carefull
      //need to test cases
    
      //Case1, one element
      if (head==tail)
      {
        free(head);    //delete node
        head=tail=NULL;//fix pointers
    
        printf("Case1.record%d found and deleted",id_num);
        return(id_num);
      }
      else //Case2, front of list
        if (currPtr==*head)
        {
          //move head to next node
          *head=*head->next;
          //delete the node
          free(currPtr);
          printf("Case2.record%d found and deleted",id_num);
          return(id_num);
        }
        else //Case3, end of list
          if (currPtr==*tail)
          {
            //fix prev. pointer to point to NULL
            prevPtr->next;
            //delete last node
            free(currPtr);
            //point tail to prev. node
            *tail=prevPtr;
            printf("Case3.record%d found and deleted",id_num);
            return(id_num);
          }
          else //Case4, node is in the middle of the list
          {
            //connect prev. node to next node
            prevPtr->next =currPtr->next;
            //delete current node
            free(currPtr);
            printf("Case4.record%d found and deleted",id_num);
            return(id_num);
          }
    }//end deleteNode
    

    Wie gesagt ich habe alle Spielarten mit *,. und -> ausprobiert. Aber irgend etwas hab ich anscheinend vergessen oder nicht kapiert...



  • (*head)->next =newNode;                    //<- hier tritt der Fehler nicht mehr auf
    

    Kurt



  • Danke, genau das wars! Auf Klammern wäre ich nie gekommen. Das sieht so...falsch aus. *head steht ja nicht für eine komplette Rechnung. Oder etwa doch?
    Naja, ich werde es erstmal in die Kategorie "Kurioses" speichern und in Zukunft head und tail als Globals definieren.


Anmelden zum Antworten