双向表链表

来源:百度知道 编辑:UC知道 时间:2024/07/02 02:54:30
建立一个双向链表保存学生信息,并进行查找
大家看看哪里出错了
还有如果是二叉树如何实现?
#include <stdio.h>
#include <malloc.h>
#define N 10

typedef struct node
{
char name[20];
struct node *llink,*rlink;
}stud;

stud * creat(int n)
{
stud *p,*h,*s;
int i;
if((h=(stud *)malloc(sizeof(stud)))==NULL)
{
printf("Can not allocate the memory!");
exit(0);
}
h->name[0]=’\0’;
h->llink=NULL;
h->rlink=NULL;
p=h;
for(i=0;i<n;i++)
{
if((s= (stud *) malloc(sizeof(stud)))==NULL)
{
printf("Can not allocate the memory!");
exit(0);
}
p->rlink=s;
printf("Please input the No.%d name:",i+1);
scanf("%s",s->name);
s->llink=p;
s->rlink=NULL;
p=s;
}
h->llink=s;
p->rlink=h;
return(h);
}

stud * search(st

1.
h->name[0]='\0'; // '半角字符
查找函数有问题,找到前提下才输出,
否则打印为找到。
以下是在你编写的基础上改正
2. 第2个你问的有些笼统
若2叉树最简单的办法你建立好
二叉树,然后对其遍历(先,中,后)

最简单的就是采用递归方式建立及遍历
在遍历过程中进行比对查找

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define N 5

typedef struct node
{
char name[20];
struct node *llink,*rlink;
}stud;

stud *creat(int n)
{
stud *p,*h,*s;
int i;
if((h=(stud *)malloc(sizeof(stud)))==NULL)
{
printf("Can not allocate the memory!");
exit(0);
}
h->name[0]='\0';
h->llink=NULL;
h->rlink=NULL;
p=h;
for(i=0;i<n;i++)
{
if((s= (stud *) malloc(sizeof(stud)))==NULL)
{
printf("Can not allocate the memory!");
exit(0);
}
p->rlink=s;
printf("Please input the No.%d name:"