簡體   English   中英

鏈表C程序不產生任何輸出

[英]Linked list C program produces no output

我在閱讀鏈表時,唯一能找到的好資料就是斯坦福CS圖書館的資料。 我希望實現從中學到的知識,並在我的編譯器上運行它。 該程序將在{1,2,3}的鏈表中查找元素的數量。

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

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

int main()
{
  struct node* BuildOneTwoThree()
  {
    struct node* head   = NULL;
    struct node* second = NULL;
    struct node* third  = NULL;

    head   = malloc(sizeof(struct node)); // allocate 3 nodes in the heap
    second = malloc(sizeof(struct node));
    third  = malloc(sizeof(struct node));

    head->data = 1; // setup first node
    head->next = second; // note: pointer assignment rule

    second->data = 2; // setup second node
    second->next = third;

    third->data = 3; // setup third link
    third->next = NULL;

    return head;

    int Length(struct node* head)
    {
      struct node* current = head;
      int count = 0;

      while (current != NULL)
      {
        count++;
        current = current->next;
      }

      printf("%d",count);
      return count;
    }

  }
  return 0;
}

它返回空白。 我不明白O在哪里犯了錯誤,我在做什么錯?

首先,您嘗試在main()函數中定義函數。 BuildOneTwoThree是在main內部定義的,而Length似乎是在BuildOneTwoThree內部BuildOneTwoThree 你為什么那樣做? C語言沒有這種功能。 您不能嵌套函數定義。 所有功能必須在文件級別分別定義。

其次,您永遠不會調用您定義的任何函數。 您的main()函數除了return 0;之外什么也不做return 0;

查看任何有效的C程序,您應該立即弄清楚應如何定義函數。 請注意,盡管某些編譯器將嵌套函數定義作為非標准擴展來支持,但您仍然必須在某些時候調用函數。

我認為這類帖子屬於其他地方(也許是stackoverflow?)。 無論如何,您都在定義一個函數BuildOneTwoThree(),並且您不會對其進行調用,因此它不會輸出任何內容。

您缺少大括號來結束BuildOneTwoThree()函數,那么您必須在main中調用該函數。 嘗試這個:

#include <stdio.h>
#include <stdlib.h>
struct node {

int data;
struct node* next;

};

struct node* BuildOneTwoThree() {
struct node* head = NULL;
            struct node* second = NULL;
            struct node* third = NULL;
            head = malloc(sizeof(struct node)); // allocate 3 nodes in the heap
            second = malloc(sizeof(struct node));
            third = malloc(sizeof(struct node));
            head->data = 1; // setup first node
            head->next = second; // note: pointer assignment rule
            second->data = 2; // setup second node
            second->next = third;
            third->data = 3; // setup third link
            third->next = NULL;
            return head;
        }

int Length(struct node* head) {
            struct node* current = head;
            int count = 0;
            while (current != NULL) {
                count++;
                current = current->next;
                }
            return count;
        }

int main(){
    struct node* newNode = BuildOneTwoThree();
    printf("%d",Length(newNode));
}

嘗試使用標准C並對代碼進行格式化,以使其易於閱讀。

如果分解事物,則會得到如下結果:

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

typedef struct LLNODE
{
  struct LLNODE *next ;

  int payload ;

} LLNODE ;

LLNODE *create_node( int value )
{
  LLNODE *p = calloc( 1 , sizeof(LLNODE) ) ;

  p->next    = NULL  ;
  p->payload = value ;

  return p ;
}

LLNODE *create_list( int start_value , int count )
{
  LLNODE *root = NULL ;
  LLNODE *tail = NULL ;

  for ( int i = 0 , value = start_value ; i < count ; ++i )
  {
    LLNODE *node = create_node( value++ ) ;

    if ( root == NULL )
    {
      root = tail = node ;
    }
    else
    {
      tail->next = node ;
      tail = node ;
    }

  }

  return root ;
}

int compute_length( LLNODE *root )
{
  int len = 0 ;

  for ( LLNODE *p = root ; p != NULL ; p = p->next )
  {
    ++len ;
  }

  return len ;
}

int main( int argc, char *argv[] )
{
  LLNODE *root   = create_list( 101 , 50 ) ;
  int     length = compute_length( root ) ;

  printf( "list length is %d\n" , length ) ;

  int i = 0 ;
  for ( LLNODE *p = root ; p != NULL ; p = p->next )
  {
    printf( "Node #%d.\tcontains the value %d\n" , ++i , p->payload ) ;
  }

  return 0 ;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM