33,321
社区成员




#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct LNode{
int data;
struct LNode*next;
}LinkList;
typedef struct LNode LinkList;
void InitLinkList(LinkList *head)
{
(head)=(LinkList*)malloc(sizeof(LNode));
(head)->next=NULL;
(head)->data=0;
}
void Insert(LinkList *head,int data)
{
printf("开始建立节点:\n");
LNode*node=(LNode*)malloc(sizeof(LNode));
node->data=data;
node->next=NULL;
/*开始插入节点*/
printf("开始插入节点:\n");
LNode *p=head;
while(head->next!=NULL)
p=p->next;
p->next=node;
printf("插入节点完毕:");
}
int main()
{
LinkList *head=NULL;
InitLinkList(head);
for(int i=0;i<3;i++)
Insert(head,i);
}
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct LNode{
int data;
struct LNode*next;
}LinkList;
typedef struct LNode LinkList;
void InitLinkList(LinkList **head)//这里改为LinkList**。。否则改变的只是head的副本。。不是head本身。。
{
(*head)=(LinkList*)malloc(sizeof(LNode));
(*head)->next=NULL;
(*head)->data=0;
}
void Insert(LinkList *head,int data)
{
printf("开始建立节点:\n");
LNode*node=(LNode*)malloc(sizeof(LNode));
node->data=data;
node->next=NULL;
/*开始插入节点*/
printf("开始插入节点:\n");
LNode *p=head;
while(p->next!=NULL)//这里是p->next不是head->next。。
p=p->next;
p->next=node;
printf("插入节点完毕:");
}
int main()
{
LinkList *head=NULL;
InitLinkList(&head);
for(int i=0;i<3;i++)
Insert(head,i);
}
void InitLinkList(LinkList **head)
{
(*head)=(LinkList*)malloc(sizeof(LNode));
(*head)->next=NULL;
(*head)->data=0;
}
int main()
{
LinkList *head;
InitLinkList(&head);
}
这样就可以啦,更加困惑了
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<iostream>
using namespace std;
struct LNode
{
int data;
LNode* next;
};
struct LinkList
{
LNode* head;
};
void InitLinkList(LinkList& list)
{
list.head = (LNode*)malloc(sizeof(LNode));
list.head->data = 0;
list.head->next = NULL;
}
void Insert(LinkList& list,int data)
{
printf("开始建立节点:\n");
LNode* node=(LNode* )malloc(sizeof(LNode));
node->data = data;
node->next=NULL;
/*开始插入节点*/
printf("开始插入节点:\n");
LNode *p = list.head;
while(p->next != NULL)
{
p = p->next;
}
p->next=node;
printf("插入节点完毕:\n");
}
void print(LinkList& list)
{
printf("开始输出节点:\n");
LNode *p = list.head->next;
if(p==NULL)
printf("链表是空的,无法输入!\n");
while(p)
{
if(p->next!=NULL)
printf("%d -->",p->data);
else
printf("%d ",p->data);
p = p->next;
}
printf("输出节点完毕:\n");
}
int main()
{
LinkList list;//局部内存空间已分配
InitLinkList(list);
cout<<list.head->data<<endl;
for(int i=0; i<3; i++)
Insert(list,i);
print(list);
system("pause");
return 0;
}
这是刚刚请教同学的