2013-01-24 32 views
0

我有一个程序要求输入一个数字。然后,如果我接下来输入“0”,它将在前面插入数字/项目(列表顶部)。如果我接下来输入“1”,它将在后面(列表的底部)插入号码/项目。需要帮助解决C指针链表分配问题

我遇到的问题是让后端代码工作。我试图从逻辑上理解我的想法,但这看起来不太合适。 links.h文件具有Item的定义。它包含数据和下一个(指向下一个对象的指针)。

这是我到目前为止的代码。

我负责编写insertFront()和insertRear()函数。前面已经在工作了。据说在else语句后只需要2行代码。

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

int main (void) { 
    Item *head, *tail; 
    Item *newItem; 
    int data, insert; 

    /* Link the empty list */ 
    head = tail = NULL; 

    /* Prompt the user for new data to be inserted into the list */ 
    while (1) { 
    /* Get the data value */ 
     printf ("Enter the positive integer that you want to insert into list (-1 to end): "); 
    fflush(stdout); 
    if (scanf ("%d",&data) != 1) { 
    /* If it isn't valid input, throw away the rest of the line of input */ 
    while (getchar() != '\n') { } 
    fprintf(stderr, "Invalid data found. Try again.\n"); 
    continue; 
    } 

    /* A negative value terminates the list entry */ 
    if (data < 0) break; 

    printf("Enter a 0 to insert data at the beginning or 1 to insert at the end: "); 
    fflush(stdout); 
    if (scanf ("%d",&insert) != 1 || insert < 0 || insert > 1) { 
    /* If it isn't valid, throw away the rest of the line of input */ 
    while (getchar() != '\n') { } 
    fprintf(stderr, "Must be zero or one! Try insertion again.\n"); 
    continue; 
    } 

    if ((newItem = malloc(sizeof(Item))) == NULL) { 
    perror ("Unable to allocate memory for new item"); 
    return EXIT_FAILURE; 
    } 
    newItem->next = NULL; 
    newItem->data = data; 

    if (insert == 0) { 
    insertFront (newItem, &head, &tail); 
    } else { 
    insertRear (newItem, &head, &tail); 
    } 

    /* Print the list in forward order */ 
    printf("List in forward order:\n"); 
    printList (head); 
} 
} 

/* Insert the item into front of the list */ 
void insertFront (Item *item, Item **headptr, Item **tailptr) { 

    if(*headptr == NULL) { 
      *headptr = item; // item is the address 
      *tailptr = item; 
     } else { 

      item->next = *headptr; 
      *headptr = item; 


     } 

    } 

    void insertRear (Item *item, Item **headptr, Item **tailptr) { 

     if(*tailptr == NULL) { 
       *tailptr = item; 
       *headptr = item; 

     } else { 


     item->next = *tailptr; 
     *tailptr = item; 

      } 

    } 

    /* Print the list in forward order */ 
    void printList (Item *head) { 
    Item *current = head; 
    while (current != NULL) { 
     printf ("%d\n", current->data); 
     current = current->next; 
    } 
    } 

回答

1

你的头指针和尾指针不应该有相同的方式!

1

你insertRear()应该是:

void insertRear (Item *item, Item **headptr, Item **tailptr) { 
     if(*tailptr == NULL) { 
       *tailptr = item; 
       *headptr = item; 

     } else { 

      (*tailptr)->next=item; 
     *tailptr = item; 

      } 

    } 

不用说,用品 - >接下来应该是NULL。

+0

那就是我以为我自己。但是编译器在使用* tailptr-> next = item时给了我错误;所以我用了等价的:(** tailptr).next = item;它的工作!谢谢 – user1930558