审查此链表程序

时间:2019-05-27 11:47:37

标签: c pointers linked-list

这是链接列表上的程序,该程序打印给定的输入。但是不用使用if(start==NULL) ...else ..语句就可以做到吗?

#include <stdio.h>
#include <malloc.h>
struct node
{
    int data;
    struct node* next;
}* start = NULL;

void main()
{
    struct node* tmp;
    struct node *ptr, *tmp1;

    int n = 0;
    int choice = 2;

    while (1)
    {
        printf("Enter a number less than 3 to continue");
        scanf("%d", &choice);
        if (choice >= 3)
            break;

        printf("Enter the input");
        scanf("%d", &n);
        // n=n+10;
        if (start == NULL)
        {
            tmp = (struct node*)malloc(sizeof(struct node));
            start = tmp;
            start->data = n;
            ptr = start;
        }
        else
        {
            tmp = (struct node*)malloc(sizeof(struct node));
            ptr->next = tmp;
            tmp->data = n;
            ptr = tmp;
        }
    }
    tmp->next = NULL;


    printf("\nThe output of the linked list is\n");
    n = 0;
    ptr = start;
    while (ptr != NULL)
    {
        printf("\n  1 ptr =           %d", ptr->data);
        ptr = ptr->next;
    }
}

3 个答案:

答案 0 :(得分:1)

start变量保留链接列表的

条件if (start == NULL)检查一个空列表。

如果列表为空,则需要创建列表的开头。从第二个元素开始,您需要将下一个元素链接到列表的最后一个元素。 else条件对此很小心。

换一种方式来看,如果您没有if条件,那么您将获得

 ptr->next = tmp;

由于ptr目前未初始化,因此您将具有未定义的行为,并且很可能会遇到分段错误。

答案 1 :(得分:1)

  

是否可以不使用if(start == NULL)... else ..语句来做到这一点?

创建头节点,并仅使用其.next成员。

#include <stdio.h>
#include <malloc.h>

struct node {
  int data;
  struct node* next;
};

int main(void) {
  struct node head = {.next = NULL};  // only need .next 
  struct node *ptr = &head;

  while (1) {
    // ...

    int n;
    printf("Enter the input ");
    fflush(stdout);
    if (scanf("%d", &n) != 1) {
      break;
    }
    ptr->next = malloc(sizeof *ptr->next);
    if (ptr->next == NULL) {
      break;
    }
    ptr = ptr->next;
    ptr->data = n;
    ptr->next = NULL;
  }

  printf("\nThe output of the linked list is\n");
  ptr = head.next; // The start of the list is here
  while (ptr) {
    printf("  1 ptr =           %d\n", ptr->data);
    ptr = ptr->next;
  }
}

答案 2 :(得分:-1)

嗯,很多事情都是不必要的。

您可以简单地将节点定义为

struct node 
{
    int data;
    node * next; // Will hold the value of next node's address
};

现在,您可以使用方法简单地创建数据结构的类。

class LinkedList {
  private:
    node *head, *current, *temp; // 3 Main pointers
  public:

    LinkedList() {
      head = temp = current = NULL; // Set them to NULL
    }

    void insert(int data) { // to add a new node
      if(head == NULL) { // if there is no node in the memory
        head = new node; // use head to make a new node
        head->data = data; // set the data of the node to data
        head->next = NULL; // set the next to NULL
        temp = current = head; // 3 pointers sitting at the head node
      }
      else { // If the node is not the first one in the memory
        current = new node; // use current pointer to make new node
        temp->next = current; // temp pointer one step behind the current node
        current->next = NULL; // make the next of new node NULL
        temp = current; // move the pointer to current
        current->data = data;
      }
    }
};

此功能将在每次调用时简单地添加节点。

要显示列表, 只需将临时指针移到head并开始迭代

void display()
{
    temp = head; // move to head
    while(temp != NULL) // while temp doesn't reach the end
    {
        cout<<temp->data<< " "; // print the node's data
        temp = temp->next; // move the pointer to next node
    }
}