用C语言打印单链表怎么成这样
#include <stdio.h>#include <stdlib.h>
#define ERROR 0
#define OK 1
//typedef int ElemType;
#define ElemType int
typedef struct Node
{
ElemType data; //Êý¾ÝÓò
struct Node *next;//Ö¸ÕëÓò
}Node ,*LinkList;
void CreateListHead(LinkList *L, int n)
{
LinkList p,r;
int i=1;
srand(time(0)); //初始化随机种子
*L = (LinkList)malloc(sizeof(struct Node)); //先建立一个带头结点的单链表
if(*L == NULL)
{
fprintf(stderr, "malloc() error.\n");
return ERROR;
}
(*L)->next = NULL;
while(i <= n)
{
p = (LinkList)malloc(sizeof(struct Node));
if( p == NULL)
{
fprintf(stderr, "malloc() error.\n");
return ERROR;
}
p->data = rand() % 100 + 1;
p->next = (*L)->next;
(*L)->next = p; //插入到表头
i++;
}
r=*L; //尾部结点
for(i=0; i<n; i++)
{
p=(Node *) malloc(sizeof(Node));
p->data=rand()%100+1;
r->next =p;
r=p;
}
}
int ClearList(LinkList *L)
{
LinkList p,q;
p=(*L)->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
(*L)->next=NULL;
return OK;
}
int printList(LinkList L)
{
LinkList p;
p = L->next;
if(p == NULL)
{
printf("链表为空.\n");
return ERROR;
}
while(p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return OK;
}
int main()
{
LinkList L;
printf("Hello world!\n");
CreateListHead(&L,1);
printList(L);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
//typedef int ElemType;
#define ElemType int
typedef struct Node
{
ElemType data; //Êý¾ÝÓò
struct Node *next; //Ö¸ÕëÓò
}Node ,*LinkList;
void CreateListHead(LinkList *L, int n)
{
LinkList p,r;
int i=1;
srand(time(0)); //初始化随机种子
*L = (LinkList)malloc(sizeof(struct Node)); //先建立一个带头结点的单链表
if(*L == NULL)
{
fprintf(stderr, "malloc() error.\n");
return ERROR;
}
(*L)->next = NULL;
while(i <= n)
{
p = (LinkList)malloc(sizeof(struct Node));
if( p == NULL)
{
fprintf(stderr, "malloc() error.\n");
return ERROR;
}
p->data = rand() % 100 + 1;
p->next = (*L)->next;
(*L)->next = p; //插入到表头
i++;
}
r=*L; //尾部结点
for(i=0; i<n; i++)
{
p=(Node *) malloc(sizeof(Node));
p->data=rand()%100+1;
r->next =p;
r=p;
}
}
int ClearList(LinkList *L)
{
LinkList p,q;
p=(*L)->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
(*L)->next=NULL;
return OK;
}
int printList(LinkList L)
{
LinkList p;
p = L->next;
if(p == NULL)
{
printf("链表为空.\n");
return ERROR;
}
while(p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return OK;
}
int main()
{
LinkList L;
printf("Hello world!\n");
CreateListHead(&L,1);
printList(L);
return 0;
}
打印单链表创建打印出来的不停的循环。
菜鸟一只,怎么return OK 或者return ERROR改成下面都可以正确返回呀?#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ERROR 5
#define OK 5
//typedef int ElemType;
#define ElemType int
typedef struct Node
{
ElemType data; //Êý¾ÝÓò
struct Node *next;//Ö¸ÕëÓò
}Node ,*LinkList;
int CreateListHead(LinkList *L, int n)
{
LinkList p,r;
int i=1;
srand(time(0)); //初始化随机种子
(*L) = (LinkList)malloc(sizeof(struct Node)); //先建立一个带头结点的单链表
if((*L) == NULL)
{
fprintf(stderr, "malloc() error.\n");
return ERROR;
}
(*L)->next = NULL;
r=*L; //尾部结点
for(i=0; i<n; i++)
{
p=(Node *) malloc(sizeof(Node));
p->data=rand()%100+1;
r->next =p;
r=p;
}
r->next = NULL; //表示当前链表结束
return OK;
}
int ClearList(LinkList *L)
{
LinkList p,q;
p=(*L)->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
(*L)->next=NULL;
return OK;
}
int printList(LinkList L)
{
LinkList p;
p = L->next;
if(p == NULL)
{
printf("链表为空.\n");
return ERROR;
}
while(p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return OK;
}
int main()
{
LinkList L;
printf("Hello world!\n");
CreateListHead(&L,6);
printList(L);
return 0;
}
楼主,改成这样就可以了哦。你创建链表中的第二种方法那是有问题的#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
//typedef int ElemType;
#define ElemType int
typedef struct Node
{
ElemType data; //Êý¾ÝÓò
struct Node *next; //Ö¸ÕëÓò
}Node ,*LinkList;
void CreateListHead(LinkList *L, int n)
{
LinkList p,r;
int i=1;
srand(time(0)); //初始化随机种子
*L = (LinkList)malloc(sizeof(struct Node)); //先建立一个带头结点的单链表
if(*L == NULL)
{
fprintf(stderr, "malloc() error.\n");
return ERROR;
}
(*L)->next = NULL;
while(i <= n)
{
p = (LinkList)malloc(sizeof(struct Node));
if( p == NULL)
{
fprintf(stderr, "malloc() error.\n");
return ERROR;
}
p->data = rand() % 100 + 1;
p->next = (*L)->next;
(*L)->next = p; //插入到表头
i++;
}
/*
r=*L; //尾部结点
//*L是头结点,
for(i=0; i<n; i++)
{
p=(Node *) malloc(sizeof(Node));
p->data=rand()%100+1;
r->next =p;
r=p;
}*/
}
int ClearList(LinkList *L)
{
LinkList p,q;
p=(*L)->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
(*L)->next=NULL;
return OK;
}
int printList(LinkList L)
{
LinkList p;
p = L->next;
if(p == NULL)
{
printf("链表为空.\n");
return ERROR;
}
while(p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return OK;
}
int main()
{
LinkList L;
printf("Hello world!\n");
CreateListHead(&L,23);
printList(L);
ClearList(&L);//释放结点
return 0;
}
页:
[1]