当前位置:编程学习 > C/C++ >>

c语言链表问题

补充:#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <memory.h>

#define SizeNode sizeof(Node)

typedef struct Node
{
 int data;
 struct Node *pNext;
}Node, *pNode;

 

pNode Creat_list(void);
void Traversal_list(pNode);
bool empty_list(pNode);
int length_list(pNode pHead);

int main(void)
{
 pNode pHead;
 int len;
 pHead = Creat_list();
 Traversal_list(pHead);
 len = length_list(pHead);
 
 

 return 0;
}

bool empty(pNode pHead)
{
 if (NULL == pHead->pNext)
  return true;
 else
  return false;

}

pNode Creat_list(void)
{
 int val;
 int len;

 pNode pHead = (pNode)malloc(SizeNode);

 if(NULL == pHead)
 {
  printf("Error\n");
  exit(-1);
 }
 memset(pHead, 0, SizeNode);
 
 pNode pTail = pHead;
 pTail->pNext = NULL;

 printf("len = ");
 scanf("%d", &len);

 for (int i = 0; i < len; i++)
 {
  pNode pNew = (pNode)malloc(SizeNode);
  if (NULL == pNew)
  {
   printf("Error\n");
   exit(-1);
  }
  memset(pNew, 0, SizeNode);

  printf("输入第%d个值: ", i+1);
  scanf("%d", &val);

  pTail->pNext = pNew;
  pNew->data = val;
  pNew->pNext = NULL;
  pTail = pNew;
 }

 
 return pHead;
}

void Traversal_list(pNode pHead)
{
 pNode pData = pHead->pNext;
 if (NULL == pData)
 {
  printf("链表为空\n");
  exit(-1);
 }

 do
 {
  printf("%d ", pData->data);
  pData = pData->pNext;

 }while(NULL != pData);
 
 printf("\n");

 return;
}

int length(pNode pHead)
{
 int len = 0;
 pNode pTail = pHead->pNext;

 while(NULL != pTail)
 {
  len ++;
  pTail = pTail->pNext;
 }
 
 return len;
}

 

 

 

错误k.obj : error LNK2001: unresolved external symbol "int __cdecl length_list(struct Node *)" (?length_list@@YAHPAUNode@@@Z)
Debug/k.exe : fatal error LNK1120: 1 unresolved externals

追问:你怎么改了下?
答案:#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <memory.h>

#define SizeNode sizeof(Node)

typedef struct Node
{
 int data;
 struct Node *pNext;
}Node, *pNode;

 

pNode Creat_list(void);
void Traversal_list(pNode);
bool empty_list(pNode);
int length_list(pNode pHead);

int main(void)
{
 pNode pHead;
 int len;
 pHead = Creat_list();
 Traversal_list(pHead);
 
 len = length_list(pHead);
 
 

 return 0;
}

bool empty(pNode pHead)
{
 if (pHead->pNext==NULL)
  return true;
 else
  return false;

}

pNode Creat_list(void)
{
 int val;
 int len;

 pNode pHead = (pNode)malloc(SizeNode);

 if(NULL == pHead)
 {
  printf("Error\n");
  exit(-1);
 }
 memset(pHead, 0, SizeNode);
 
 pNode pTail = pHead;
 pTail->pNext = NULL;

 printf("len = ");
 scanf("%d", &len);

 for (int i = 0; i < len; i++)
 {
  pNode pNew = (pNode)malloc(SizeNode);
  if (NULL == pNew)
  {
   printf("Error\n");
   exit(-1);
  }
  memset(pNew, 0, SizeNode);

  printf("ê?è?μú%d???μ: ", i+1);
  scanf("%d", &val);

  pTail->pNext = pNew;
  pNew->data = val;
  pNew->pNext = NULL;
  pTail = pNew;
 }

 
 return pHead;
}

void Traversal_list(pNode pHead)
{
 pNode pData = pHead->pNext;
 if (pData==NULL)
 {
  printf("á′±í?a??\n");
  exit(-1);
 }

 do
 {
  printf("%d ", pData->data);
  pData = pData->pNext;

 }while(NULL != pData);
 
 printf("\n");

 return;
}

int length_list(pNode pHead)
{
 int len = 0;
 pNode pTail = pHead->pNext;

 while(NULL != pTail)
 {
  len ++;
  pTail = pTail->pNext;
 }
 
 return len;
}

 

//可以了

上一个:怎么样学好C语言?
下一个:什么是C语言?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,