-1

私はこのプロジェクトに1日以上取り組んでいますが、本当に混乱しています。誰かがこれを理解するのを手伝ってくれることを祈ります。関数に問題がありますinsertItemListStringテスト ファイル内のを に取得する方法がわかりませんlinked list。これは宿題です。プロジェクトはこれよりもはるかに大きいですが、私が助けを必要としているものだけにまとめました。私が間違っていることを誰かが知っていますか?

List.h

       #ifndef _list_h
       #define _list_h

      /*
      * Defines a single list item.
      */

      typedef char *ListItemT;

      /*
      * Defines ListP to be a POINTER to a list struct.
      * (Define the list struct in the (.c) file.
      */

      typedef struct ListT *ListP;

      /*
      * Returns a pointer to a new empty list.
      * You may implement this list as a singly or doubly
      * linked list.
      */

      ListP newList();

      /*
      * Creates a new node, inserts the item into the
      * new node and inserts the new node into the list
      * after the current node.
      * Current in then moved to the newly inserted node.
      * NOTE: Impossible to insert at head of list.
      */

      void insertItemList(ListP, ListItemT);

      #endif

List.c

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include "List.h"


      struct ListT
      { 
         char INFO[20]; 
         struct ListT *Next; 
      }*Head;



      /*
      * Returns a pointer to a new empty list.
      * You may implement this list as a singly or doubly
      * linked list.
      */

      ListP newList()

      {
      // allocate memory for new list 

      struct ListT *newnode = malloc(sizeof(struct ListT));

      return newnode; 
      }

      /*
      * Creates a new node, inserts the item into the
      * new node and inserts the new node into the list
      * after the current node.
      * Current in then moved to the newly inserted node.
      * NOTE: Impossible to insert at head of list.
      */

      //where I'm having trouble

     void insertItemList(ListP LOC, ListItemT DATA)

     {
     struct ListT *temp;

     temp=(struct ListT *)malloc(sizeof(struct ListT)); 

     strcpy(temp->INFO, DATA);
     }

ListTest.c

     #include <stdio.h>
     #include <string.h>
     #include "List.h"

     int main()
     {
     // Create two lists
     ListP list1 = newList();
     ListP list2 = newList();
     printf("\nList Creation Successful!\n");

     // Insert one name into the first list
     insertItemList( list1, "Alice" ); //Ive never seen a parameter like this before.
     }
4

2 に答える 2

0

Nextリストノードのフィールドを設定するコードには何もないようです。

于 2012-07-14T19:47:51.707 に答える
0

List.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "List.h"

struct ListT { struct ListNodeT *head, *current; };
struct ListNodeT
{ 
    char INFO[20]; 
    struct ListNodeT *Next; 
};

/*
* Returns a pointer to a new empty list.
* You may implement this list as a singly or doubly
* linked list.
*/
ListP newList()
{
    // allocate memory for new list 
    struct ListT *newlist = malloc(sizeof(struct ListT));
    if (newlist) newlist->head = newlist->current = NULL;
    return newlist; 
}

/*
* Creates a new node, inserts the item into the
* new node and inserts the new node into the list
* after the current node.
* Current is then moved to the newly inserted node.
* NOTE: Impossible to insert at head of list.
*/
void insertItemList(ListP LOC, ListItemT DATA)
{
    struct ListNodeT *temp = malloc(sizeof(struct ListNodeT)); 
    if (!temp) abort();
    strncpy(temp->INFO, DATA, sizeof temp->INFO);
    if (LOC->current)
        temp->Next = LOC->current->Next,
        LOC->current->Next = temp;
    else
        temp->Next = NULL,
        LOC->head = temp;
    LOC->current = temp;
}
于 2014-04-03T09:51:39.207 に答える