#include<stdio.h>
#include<stdlib.h>
typedef enum status
{
success, error, fail
}status;
typedef int ElemType;
typedef struct Node
{
ElemType elem;
struct Node* next;
} Node, * ptr;
typedef ptr* SqList;
status List_init(SqList L);
status create_list(SqList L, int n);
status find_list(SqList L, int n);
void printf_list(SqList L);
status List_init(SqList L)
{
status s = error;
ptr p;
if (*L == NULL)
{
p = (ptr)malloc(sizeof(Node));
if (p == NULL)return s;
else
{
*L = p;
(*L)->next = NULL;
s = success;
}
}
return s;
}
status create_list(SqList L, int n)
{
status s = fail;
s = List_init(L);
int i = 1;
ptr r = *L;
while ((*L) && i <= n)
{
ptr p = (ptr)malloc(sizeof(Node));
if (p != NULL)
{
r ->next= p;
p->next = NULL;
r = r->next;
}
}
return s;
}
status find_list(SqList L,int n)
{
status s = error;
create_list(L, n);
ptr r = *L;
printf("Enter ElemType:\n");
while (r->next)
{
ElemType data;
scanf_s("%d", &data);
r->elem = data;
r = r->next;
s = success;
}
printf("Input completely!\n");
return s;
}
void printf_list(SqList L)
{
ptr p;
p = *L;
while (p->next)
{
printf("%d\t", p->elem);
p = p->next;
}
printf("\n");
}
int main()
{
int n, m;
SqList LA, LB;
LA = (SqList)malloc(sizeof(ptr*));
LB = (SqList)malloc(sizeof(ptr*));
printf("Enter n,m:\n");
scanf_s("%d %d", &n, &m);
create_list(LA, n);
create_list(LB, m);
find_list(LA, n);
find_list(LB, n);
printf_list(LA);
printf_list(LB);
return 0;
}
求解答。
这么改,find_list()函数输入链表数据,供参考:
#include<stdio.h>
#include<stdlib.h>
typedef enum status
{
success, error, fail
}status;
typedef int ElemType;
typedef struct Node
{
ElemType elem;
struct Node* next;
} Node, * ptr;
typedef ptr* SqList;
status List_init(SqList L);
status create_list(SqList L, int n);
status find_list(ptr L); //status find_list(SqList L, int n);
void printf_list(ptr L); //void printf_list(SqList L);
status List_init(SqList L)
{
status s = error;
ptr p;
if ((*L) == NULL)
{
p = (ptr)malloc(sizeof(Node));
if (p == NULL)return s;
else
{
(*L) = p;
(*L)->next = NULL;
s = success;
}
}
return s;
}
status create_list(SqList L, int n)
{
status s = fail;
s = List_init(L);
int i = 1;
ptr r = (*L);
while ((*L) && i <= n)
{
ptr p = (ptr)malloc(sizeof(Node));
if (p != NULL)
{
r ->next= p;
p->next = NULL;
r = r->next;
}
i++; //修改
}
return s;
}
status find_list(ptr L) //(SqList L,int n) 修改
{
status s = error;
//create_list(L, n);修改
ptr r = L; //ptr r = *L; 修改
printf("Enter ElemType:\n");
while (r->next)
{
ElemType data;
scanf_s("%d", &data);
r->next->elem = data; //r->elem = data;
r = r->next;
s = success;
}
printf("Input completely!\n");
return s;
}
void printf_list(ptr L) //(SqList L) 修改
{
ptr p;
p = L; //p = *L; 修改
while (p->next)
{
printf("%d\t", p->next->elem); //printf("%d\t", p->elem); 修改
p = p->next;
}
printf("\n");
}
int main()
{
int n, m;
ptr LA = NULL, LB = NULL;
//SqList LA, LB; 修改
//LA = (SqList)malloc(sizeof(ptr*)); 修改
//LB = (SqList)malloc(sizeof(ptr*)); 修改
printf("Enter n,m:\n");
scanf_s("%d %d", &n, &m);
create_list(&LA, n); //create_list(LA, n);修改
create_list(&LB, m); //create_list(LB, m);修改
find_list(LA); //find_list(LA, n); 修改
find_list(LB); //find_list(LB, n); 修改
printf_list(LA);
printf_list(LB);
return 0;
}
把函数的参数的参数和find_list()函数修改了下。
修改如下:
#include<stdio.h>
#include<stdlib.h>
typedef enum status
{
success, error, fail
}status;
typedef int ElemType;
typedef struct Node
{
ElemType elem;
struct Node* next;
} Node, * ptr;
typedef ptr* SqList;
status List_init(ptr L);
status create_list(ptr L, int n);
status find_list(ptr L, int n);
void printf_list(ptr L);
status List_init(ptr * L)
{
status s = error;
ptr p;
// printf("1,L=%p\n",L);
if (*L == NULL)
{
// printf("1.2\n");
p = (ptr)malloc(sizeof(Node));
// printf("2,p=%p\n",p);
if (p == NULL)return s;
else
{
*L = p;
(*L)->next = NULL;
s = success;
// printf("3\n");
}
}
// printf("4\n");
return s;
}
status create_list(ptr* L, int n)
{
status s = fail;
s = List_init(L);
int i = 1;
ptr r = *L;
while ((r) && i <= n)
{
ptr p = (ptr)malloc(sizeof(Node));
if (p != NULL)
{
p->elem=i;
// printf("p=%p,p->elem=%d\n",p,p->elem);
r ->next= p;
p->next = NULL;
r = r->next;
}
i++;
}
return s;
}
status find_list(ptr * L,int n)
{
status s = fail;
// create_list(L, n);
ptr r = *L;
while (r)
{
if ( r->elem == n){
printf("Enter ElemType:\n");
ElemType data;
scanf("%d", &data);
r->elem=data;
s = success;
break;
}
r = r->next;
}
// printf("Input completely!\n");
return s;
}
void printf_list(ptr * L)
{
ptr p;
p = (*L)->next;
while (p)
{
printf("%d\t", p->elem);
p = p->next;
}
printf("\n");
}
// https://ask.csdn.net/questions/7955263?spm=1005.2025.3001.5141
int main()
{
int n, m;
ptr LA=NULL, LB=NULL;
// LA = (SqList)malloc(sizeof(ptr*));
// LB = (SqList)malloc(sizeof(ptr*));
//
printf("Enter n,m:\n");
scanf("%d %d", &n, &m);
create_list(&LA, n);
create_list(&LB, m);
status s=find_list(&LA, n);
// if(s==success){
// printf("找到!\n");
// }else if(s==fail){
// printf("没找到!\n");
// }
s=find_list(&LB, m);
// if(s==success){
// printf("找到!\n");
// }else if(s==fail){
// printf("没找到!\n");
// }
//
printf_list(&LA);
printf_list(&LB);
return 0;
}
数据结构对单链表进行数据排序 http://bbs.csdn.net/topics/392201633
不知道你这个问题是否已经解决, 如果还没有解决的话:真正的猜数字游戏的核心在于编译器产生随机数,并且每次运行所产生的随机数不同,需要用到函数rand和srand,rand用来产生随机数,srand用来初始化rand随机数种子,把时钟作为随机数种子,每次运行程序的时间不同,从而每次产生的随机数是不同的。
整篇就到这里了,本人初学C语言,若该文章有什么问题,欢迎大佬们留言给予建议!谢谢大家的阅览!