这程序是为了写个链表,然后输入,查找第几个的数字是多少。
前面是声明的头文件
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType ;
typedef struct Lnode{
ElemType data;
struct Lnode *next;
}Lnode;
typedef Lnode * LinkList;
Status InitList(LinkList &L)
{
Lnode *p;
p=(Lnode *)malloc(sizeof(Lnode));
if(p==NULL)
return ERROR;
L=p;
L->next=NULL;
return OK;
};
Status CreatList(LinkList &L)
{
int a,b;
Lnode *p1,*p2;
printf("input the length:");
scanf("%d",&a);
printf("input list is:\n");
L=p2=(Lnode *)malloc(sizeof(Lnode));
for (b=1;b<=a;b++)
{
p1=(Lnode *)malloc(sizeof(Lnode));
scanf("%d\n",&p1->data);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
return OK;
};
主程序
#include "stdafx.h"
#include"zhou34.h"
int _tmain(int argc, _TCHAR* argv[])
{
ElemType i,e;
LinkList L;
InitList(L);
CreatList(L);
printf("输入要取元素的位置:\n");
scanf("%d",&i);
GetList(L,i,e);
printf("第%d个位置的元素是:%d\n",i,e);
return 0;
那个2跑错了地方,我估计是地址错乱了,哪错了啊?
}
前面是声明的头文件
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Status;
typedef int ElemType ;
typedef struct Lnode{
ElemType data;
struct Lnode *next;
}Lnode;
typedef Lnode * LinkList;
Status InitList(LinkList &L)
{
Lnode *p;
p=(Lnode *)malloc(sizeof(Lnode));
if(p==NULL)
return ERROR;
L=p;
L->next=NULL;
return OK;
};
Status CreatList(LinkList &L)
{
int a,b;
Lnode *p1,*p2;
printf("input the length:");
scanf("%d",&a);
printf("input list is:\n");
L=p2=(Lnode *)malloc(sizeof(Lnode));
for (b=1;b<=a;b++)
{
p1=(Lnode *)malloc(sizeof(Lnode));
scanf("%d\n",&p1->data);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
return OK;
};
主程序
#include "stdafx.h"
#include"zhou34.h"
int _tmain(int argc, _TCHAR* argv[])
{
ElemType i,e;
LinkList L;
InitList(L);
CreatList(L);
printf("输入要取元素的位置:\n");
scanf("%d",&i);
GetList(L,i,e);
printf("第%d个位置的元素是:%d\n",i,e);
return 0;
那个2跑错了地方,我估计是地址错乱了,哪错了啊?
}
