马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
介绍:我按照“小甲鱼”的数据结构第五十八讲中所讲解的十字链表,自己按照理解写了一个十字链表实现代码,在实现邻接表过程是顺利的但是逆邻接表的实现却遇到了问题,逆邻接表的实现我用的是迭代在main函数中代码如下: for(int i = 0; i < MAX; i++)
{
temp = diss[i].doout;
while(temp != NULL)
{
temps = &diss[temp->headdata].doin;
if(*temps == NULL) //如果
{
*temps = temp;
}
else
{
nice = *temps; //这里的逻辑我一直想不明白为什么我在这里会错误呢,我感觉他是因为指针的问题但是我不并不知道问题是怎么发生的请指教
while(*temps != NULL)
{
*temps = (*temps)->headlink;
}
*temps = temp;
*temps = nice;
}
temp = temp->taillink;
}
}
输入后DOS输入输出页面:
完整代码:#include <stdio.h>
#include <stdlib.h>
#define MAX 4
typedef struct list
{
int taildata, headdata;
struct list *headlink, *taillink;
}list,*lists;
typedef struct like
{
char data[MAX][MAX];
list *doin, *doout;
}like;
void loop_list (list **doin);
void prin_list (list *doout);
void print_list (list *doin);
void print_list (list *doin)
{
while(doin != NULL)
{
printf("%d ", doin->taildata);
printf("%d ", doin->headdata);
doin = doin->headlink;
}
}
void prin_list (list *doout)
{
while(doout != NULL)
{
printf("%d ", doout->taildata);
printf("%d ", doout->headdata);
doout = doout->taillink;
}
}
void loop_list (list **doout)
{
list *temp, *tail;
int tail_vex, head_vex;
while(1)
{
scanf("%d", &tail_vex);
scanf("%d", &head_vex);
if(tail_vex >= 0 && tail_vex <= 9) //输入9以上的数字结束本函数
{
temp = (lists )malloc(sizeof(list ));
temp->taildata = tail_vex;
temp->headdata = head_vex;
temp->taillink = NULL;
temp->headlink = NULL;
if(*doout == NULL)
{
*doout = temp;
}
else
{
tail->taillink = temp;
}
tail = temp;
}
else
{
break;
}
}
}
int main()
{
like diss[MAX];
list *temp;
list **temps;
list *nice;
for(int i = 0; i < MAX; i++)
{
scanf("%s", &diss[i].data[0][0]);
diss[i].doin = NULL;
diss[i].doout = NULL;
loop_list(&diss[i].doout);
}
for(int i = 0; i < MAX; i++)
{
temp = diss[i].doout;
while(temp != NULL)
{
temps = &diss[temp->headdata].doin;
if(*temps == NULL) //如果
{
*temps = temp;
}
else
{
nice = *temps; //这里的逻辑我一直想不明白为什么我在这里会错误呢,我感觉他是因为指针的问题但是我不并不知道问题是怎么发生的请指教
while(*temps != NULL)
{
*temps = (*temps)->headlink;
}
*temps = temp;
*temps = nice;
}
temp = temp->taillink;
}
}
for(int i = 0; i < MAX; i++)
{
printf("%s ", diss[i].data[0]);
prin_list(diss[i].doout);
putchar('\n');
}
putchar('\n');
for(int i = 0; i < MAX; i++)
{
printf("%s ", diss[i].data[0]);
print_list(diss[i].doin);
putchar('\n');
}
return 0;
}
1. 又不写内存释放的代码?
你不写,你在指望谁帮你写?这一次我又帮你写了
2. 没看明白你的逆邻接表的代码
#include <stdio.h>
#include <stdlib.h>
#define MAX 4
typedef struct list {
int taildata, headdata;
struct list *headlink, *taillink;
} list, *lists;
typedef struct like {
char data[MAX][MAX];
list *doin, *doout;
} like;
void loop_list(list **doin);
void prin_list(list *doout);
void print_list(list *doin);
void print_list(list *doin) {
while(doin != NULL) {
printf("%d ", doin->taildata);
printf("%d ", doin->headdata);
doin = doin->headlink;
}
}
void prin_list(list *doout) {
while(doout != NULL) {
printf("%d ", doout->taildata);
printf("%d ", doout->headdata);
doout = doout->taillink;
}
}
void loop_list(list **doout) {
list *temp, *tail;
int tail_vex, head_vex;
while(1) {
scanf("%d", &tail_vex);
scanf("%d", &head_vex);
if(tail_vex >= 0 && tail_vex <= 9) //输入9以上的数字结束本函数
{
temp = (lists)malloc(sizeof(list));
temp->taildata = tail_vex;
temp->headdata = head_vex;
temp->taillink = NULL;
temp->headlink = NULL;
if(*doout == NULL) {
*doout = temp;
} else {
tail->taillink = temp;
}
tail = temp;
} else {
break;
}
}
}
void chain_head(like diss[MAX]) {
for(size_t i = 0; i < MAX; ++i) {
list *current = diss[i].doout;
for(size_t j = 0; current; ++j) {
current->headlink = diss[current->headdata].doin;
diss[current->headdata].doin = current;
current = current->taillink;
}
}
}
void free_list(list *l) {
if(!l) return;
free_list(l->taillink);
free(l);
}
int main() {
like diss[MAX];
/*
list *temp;
list **temps;
list *nice;
*/
for(int i = 0; i < MAX; i++) {
scanf("%s", &diss[i].data[0][0]);
diss[i].doin = NULL;
diss[i].doout = NULL;
loop_list(&diss[i].doout);
}
chain_head(diss);
/*
for(int i = 0; i < MAX; i++) {
temp = diss[i].doout;
while(temp != NULL) {
temps = &diss[temp->headdata].doin;
if(*temps == NULL) //如果
{
*temps = temp;
} else {
nice =
*temps; //这里的逻辑我一直想不明白为什么我在这里会错误呢,我感觉他是因为指针的问题但是我不并不知道问题是怎么发生的请指教
while(*temps != NULL) {
*temps = (*temps)->headlink;
}
*temps = temp;
*temps = nice;
}
temp = temp->taillink;
}
}
*/
for(int i = 0; i < MAX; i++) {
printf("%s ", diss[i].data[0]);
prin_list(diss[i].doout);
putchar('\n');
}
putchar('\n');
for(int i = 0; i < MAX; i++) {
printf("%s ", diss[i].data[0]);
print_list(diss[i].doin);
putchar('\n');
}
for(size_t i = 0; i < MAX; ++i) {
free_list(diss[i].doout);
}
return 0;
}
|