2.HeadInsert的函数传参和定义函数时不对应
3.函数形参是LinkList L时,引用函数时要用引用传入,就是在对象前加上&符
4.要传入结构体(或对象)时要在主函数或者函数调用之前先定义出来一个结构体(或对象),才能把它们传进去
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
char data;
struct Node*next;
}Node,*LinkList;
void InitList(LinkList *L)//初始化单链表
{
*L=(Node*)malloc(sizeof(Node));//分配一个Node类型大小的内存空间, 并把它赋给ListList型的*L
(*L)->next=NULL;
}
void HeadInsert(LinkList L)//头插法
{
LinkList p;
char data;
p=(LinkList)malloc(sizeof(Node));
p->data=data;
p->next=L->next;
L->next=p;
L->data++;
}
void PrintList(LinkList L)//打印
{
LinkList p;
p=L->next;
while(p)
{
printf("%c",p->data);
p=p->next;
}
}
int main()
{
int i,j;
char name[2]={'u','w'};
for(i=0;i<2;i++)
{
HeadInsert(L,name[i]);
}
PrintList(L);
return 0;
}
更正:
#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
typedef struct Node
{
char data;
struct Node*next;
}Node,*LinkList;
void InitList(LinkList &L)//创建并初始化单链表
{
L=(Node*)malloc(sizeof(Node));
L->next=NULL;
}
void HeadInsert(LinkList &L,char val)//头插法
{
Node *p;//char data;
p=(LinkList)malloc(sizeof(Node));
p->data=val;
p->next=L->next;
L->next=p;//L->data++;
}
void PrintList(LinkList L)//打印
{
LinkList p;
p=L->next;
while(p)
{
printf("%c",p->data);
p=p->next;
}
}
int main()
{
int i,j;
LinkList L;
InitList(L);
char name[2]={'u','w'};
for(i=0;i<2;i++)
{
HeadInsert(L,name[i]);
}
PrintList(L);
return 0;
}
数据结构链表更正
最新推荐文章于 2025-08-13 15:50:21 发布