调用函数打印不出链表数据,在main函数直接打印再调用函数却打印两次数据,搞不懂
#include "iostream"#include "stdlib.h"
#include "time.h"
typedef int ElmeType;
typedef struct Node
{
ElmeType data;
struct Node* next;
}Node;
typedef Node* LinkList;
void CreateList(LinkList *L,int n)
{
LinkList p,r;
int i;
srand(time(0));
*L=(LinkList)malloc(sizeof(Node));
(*L)->next=NULL;
(*L)->data=0;
r=*L;
for(i=0;i<n;i++)
{
((*L)->data)++;
p=(LinkList)malloc(sizeof(Node));
p->data=rand()%100+1;
r->next=p;
r=p;
r->next=NULL;
}
}
void See(LinkList *L)
{
LinkList p;
p=(*L)->next;
while(p)
{
std::cout<<p->data<<"";
p=p->next;
}
}
int main()
{
LinkList *L;
CreateList(L,20);
See(L);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct student
{
int data ;
struct student * next ;
} Node ;
Node * CreateList(Node * L , int n)
{
Node * h , * p , * q ;
h = p = L ;
q = (Node *) malloc(sizeof(Node)) ;
q -> data = n ;
q -> next = NULL ;
if(p) {
while(p -> next) p = p -> next ;
p -> next = q ;
} else {
h = q ;
}
return h ;
}
void See(Node * L)
{
Node * p ;
if(p) {
while(p) {
printf("%d\n" , p -> data) ;
p = p -> next ;
}
} else {
printf("链表为空!\n") ;
}
}
int main()
{
Node * L ;
L = CreateList(NULL , 20) ;
CreateList(L , 10) ;
CreateList(L , 5) ;
CreateList(L , 35) ;
CreateList(L , 45) ;
See(L) ;
}
页:
[1]