只為不段前行 发表于 2014-12-7 16:32:22

小甲鱼老师的代码怎么出错拉!!!大家进来看看

#include <stdio.h>
#include <stdlib.h>

/*链表存储结构的定义*/
typedef struct CLinkList
{
    int data;
    struct CLinkList *next;
}node;

/************************************************************************/
/* 操作                                                                  */
/************************************************************************/

/*初始化循环链表*/
void ds_init(node **pNode)
{
    int item;
    node *temp;
    node *target;

    printf("输入结点的值,输入0完成初始化\n");

        while(1)
        {
      scanf("%d", &item);
      fflush(stdin);

                if(item == 0)
            return;

      if((*pNode) == NULL)//在主函数中phead==Null为了创建第一个接点
                { /*循环链表中只有一个结点*/
                        *pNode = (node*)malloc(sizeof(struct CLinkList));
                        if(!(*pNode))
                                exit(0);
                        (*pNode)->data = item;
                        (*pNode)->next = *pNode;//头插法使得自己指向自己
                }
      else
                {
            /*找到next指向第一个结点的结点*/
            do(target=(*pNode);target->next!=(*pNode);target=target->next)
                       
                       
                        //有了target方便与和temp 交替存入数据。 (*pNode)始终指向第一个结点方便与与表尾连接
                       

            /*生成一个新的结点*/
            temp = (node *)malloc(sizeof(struct CLinkList));

                        if(!temp)
                                exit(0);

                        temp->data = item;
            temp->next = *pNode;
            target->next = temp;

         
      }
    }
}

/*插入结点*/
/*参数:链表的第一个结点,插入的位置*/
void ds_insert(node **pNode , int i)
{
    node *temp;
    node *target;
    node *p;
    int item;
    int j = 1;

    printf("输入要插入结点的值:");
    scanf("%d", &item);

    if(i == 1)
        { //新插入的结点作为第一个结点
      temp = (node *)malloc(sizeof(struct CLinkList));

                if(!temp)
            exit(0);

                temp ->data = item;

      /*寻找到最后一个结点*/
      for(target = (*pNode); target->next != (*pNode); target = target->next)
                        ;

                temp->next = (*pNode);
      target->next = temp;
      *pNode = temp;
    }
    else
        {
      target = *pNode;

      for( ; j < (i-1); ++j )
                {
                        target=target->next;
                }

                temp = (node *)malloc(sizeof(struct CLinkList));

                if(!temp)
            exit(0);

                temp ->data = item;
      p = target->next;
      target->next = temp;
      temp->next = p;
    }
}

/*删除结点*/
void ds_delete(node **pNode, int i)
{
    node *target;
    node *temp;
    int j = 1;

    if(i == 1)
        { //删除的是第一个结点
      /*找到最后一个结点*/
      for(target = *pNode; target->next != *pNode;target = target->next)
                        ;

                temp = *pNode;
      *pNode = (*pNode)->next;
      target->next = *pNode;
      free(temp);
    }
    else
        {
      target = *pNode;

                for( ; j < i-1; ++j )
                {
                        target = target->next;
                }

                temp = target->next;
      target->next = temp->next;
      free(temp);
    }
}

/*返回结点所在位置*/
int ds_search(node *pNode, int elem)
{
    node *target;
    int i = 1;

    for(target = pNode; target->data != elem && target->next != pNode; ++i)
        {
                target = target->next;
        }

        if(target->next == pNode) /*表中不存在该元素*/
      return 0;
    else
      return i;
}

/*遍历*/
void ds_traverse(node *pNode)
{
    node *temp;
    temp = pNode;
    printf("***********链表中的元素******************\n");

        do
        {
      printf("%4d ", temp->data);
    }while((temp = temp->next) != pNode);

        printf("\n");
}

int main()
{
    node *pHead = NULL;
    char opp;
    int find;

    printf("------------------- 广 告 位 招 租 --------------------\n\n");
    printf("欢迎来到鱼C论坛和大家一起交流互助:http://bbs.fishc.com\n\n");
    printf("喜欢鱼C就加入成为终身VIP会员吧:http://fishc.taobao.com\n\n");
    printf("-------------------------------------------------------\n\n");
    printf("1.初始化链表 \n\n2.插入结点 \n\n3.删除结点 \n\n4.返回结点位置 \n\n5.遍历链表\n\n0.退出 \n\n请选择你的操作:");
    while(opp != '0')
        {
      scanf("%c", &opp);
      switch(opp)
                {
            case '1':
                ds_init(&pHead);
                printf("\n");
                ds_traverse(pHead);
                break;

            case '2':
                printf("输入需要插入结点的位置?");
                scanf("%d",&find);
                ds_insert(&pHead, find);
                printf("在位置%d插入值后:\n",find);
                ds_traverse(pHead);
                printf("\n");
                break;

            case '3':
                printf("输入需要删除的结点位置?");
                scanf("%d",&find);
                ds_delete(&pHead, find);
                printf("删除第%d个结点后:\n",find);
                ds_traverse(pHead);
                printf("\n");
                break;

            case '4':
                printf("你要查找倒数第几个结点的值?");
                scanf("%d",&find);
                printf("元素%d所在位置:%d\n",find,ds_search(pHead, find));
                //ListTraverse(L);
                printf("\n");
                break;

            case '5':
                ds_traverse(pHead);
                printf("\n");
                break;

            case '0':
                exit(0);
      }
    }

    return 0;
}

这个代码在我的VC 会出错 看看你们的呢能运行不
还有 我怎么感觉在初始化中 有点错误呀 那个else语句中的for循环 当输入第2个数时就不会执行 因为target->next != (*pNode)这个一直都是不满足的 执行不到for 循环 因为当我们数入第一个数存入时 就生成了第一个自己指向自己的接点 到数入第二个数存入时候执行lse语句中的for循环 的target = (*pNode)后 有判断条件 target->next != (*pNode)但是根本不满足呀。target->next 的指向和(*pNode)指向是同一个地址,所以根本不会执行下面的语句。
按我的意思改是把那部分改成
else
                {
            /*找到next指向第一个结点的结点*/
            do
                        {target = (*pNode);
                       
                        //有了target方便与和temp 交替存入数据。 (*pNode)始终指向第一个结点方便与与表尾连接
                       

            /*生成一个新的结点*/
            temp = (node *)malloc(sizeof(struct CLinkList));

                        if(!temp)
                                exit(0);

                        temp->data = item;
            temp->next = *pNode;
            target->next = temp;

            target = target->next;
                        }while(target->next != (*pNode));
为了能执行下面的语句我用了dowhile循环
大家帮忙看看 到底是怎么回事呀
当我改成do while 的时候 只能初始化存入输入的第一个数和最后个数字。语法和逻辑都没问题呀 。。。。。。。。。难道我的VC有问题:dizzy::dizzy:

只為不段前行 发表于 2014-12-7 23:16:50

小甲鱼老师的没错 我弄错了 上面那个 do 是打错了 我重新发下
但是为什么那里有;没有; 就会报错呢
#include <stdio.h>
#include <stdlib.h>

/*链表存储结构的定义*/
typedef struct CLinkList
{
    int data;
    struct CLinkList *next;
}node;

/************************************************************************/
/* 操作                                                                  */
/************************************************************************/

/*初始化循环链表*/
void ds_init(node **pNode)
{
    int item;
    node *temp;
    node *target;

    printf("输入结点的值,输入0完成初始化\n");

        while(1)
        {
      scanf("%d", &item);
      fflush(stdin);

                if(item == 0)
            return;

      if((*pNode) == NULL)
                { /*循环链表中只有一个结点*/
                        *pNode = (node*)malloc(sizeof(struct CLinkList));
                        if(!(*pNode))
                                exit(0);
                        (*pNode)->data = item;
                        (*pNode)->next = *pNode;
                }
      else
                {
            /*找到next指向第一个结点的结点*/
            for(target = (*pNode); target->next != (*pNode); target = target->next)
                                ;

            /*生成一个新的结点*/
            temp = (node *)malloc(sizeof(struct CLinkList));

                        if(!temp)
                                exit(0);

                        temp->data = item;
            temp->next = *pNode;
            target->next = temp;
      }
    }
}

/*插入结点*/
/*参数:链表的第一个结点,插入的位置*/
void ds_insert(node **pNode , int i)
{
    node *temp;
    node *target;
    node *p;
    int item;
    int j = 1;

    printf("输入要插入结点的值:");
    scanf("%d", &item);

    if(i == 1)
        { //新插入的结点作为第一个结点
      temp = (node *)malloc(sizeof(struct CLinkList));

                if(!temp)
            exit(0);

                temp ->data = item;

      /*寻找到最后一个结点*/
      for(target = (*pNode); target->next != (*pNode); target = target->next)
                        ;

                temp->next = (*pNode);
      target->next = temp;
      *pNode = temp;
    }
    else
        {
      target = *pNode;

      for( ; j < (i-1); ++j )
                {
                        target=target->next;
                }

                temp = (node *)malloc(sizeof(struct CLinkList));

                if(!temp)
            exit(0);

                temp ->data = item;
      p = target->next;
      target->next = temp;
      temp->next = p;
    }
}

/*删除结点*/
void ds_delete(node **pNode, int i)
{
    node *target;
    node *temp;
    int j = 1;

    if(i == 1)
        { //删除的是第一个结点
      /*找到最后一个结点*/
      for(target = *pNode; target->next != *pNode;target = target->next)
                        ;

                temp = *pNode;
      *pNode = (*pNode)->next;
      target->next = *pNode;
      free(temp);
    }
    else
        {
      target = *pNode;

                for( ; j < i-1; ++j )
                {
                        target = target->next;
                }

                temp = target->next;
      target->next = temp->next;
      free(temp);
    }
}

/*返回结点所在位置*/
int ds_search(node *pNode, int elem)
{
    node *target;
    int i = 1;

    for(target = pNode; target->data != elem && target->next != pNode; ++i)
        {
                target = target->next;
        }

        if(target->next == pNode) /*表中不存在该元素*/
      return 0;
    else
      return i;
}

/*遍历*/
void ds_traverse(node *pNode)
{
    node *temp;
    temp = pNode;
    printf("***********链表中的元素******************\n");

        do
        {
      printf("%4d ", temp->data);
    }while((temp = temp->next) != pNode);

        printf("\n");
}

int main()
{
    node *pHead = NULL;
    char opp;
    int find;

    printf("------------------- 广 告 位 招 租 --------------------\n\n");
    printf("欢迎来到鱼C论坛和大家一起交流互助:http://bbs.fishc.com\n\n");
    printf("喜欢鱼C就加入成为终身VIP会员吧:http://fishc.taobao.com\n\n");
    printf("-------------------------------------------------------\n\n");
    printf("1.初始化链表 \n\n2.插入结点 \n\n3.删除结点 \n\n4.返回结点位置 \n\n5.遍历链表\n\n0.退出 \n\n请选择你的操作:");
    while(opp != '0')
        {
      scanf("%c", &opp);
      switch(opp)
                {
            case '1':
                ds_init(&pHead);
                printf("\n");
                ds_traverse(pHead);
                break;

            case '2':
                printf("输入需要插入结点的位置?");
                scanf("%d",&find);
                ds_insert(&pHead, find);
                printf("在位置%d插入值后:\n",find);
                ds_traverse(pHead);
                printf("\n");
                break;

            case '3':
                printf("输入需要删除的结点位置?");
                scanf("%d",&find);
                ds_delete(&pHead, find);
                printf("删除第%d个结点后:\n",find);
                ds_traverse(pHead);
                printf("\n");
                break;

            case '4':
                printf("你要查找倒数第几个结点的值?");
                scanf("%d",&find);
                printf("元素%d所在位置:%d\n",find,ds_search(pHead, find));
                //ListTraverse(L);
                printf("\n");
                break;

            case '5':
                ds_traverse(pHead);
                printf("\n");
                break;

            case '0':
                exit(0);
      }
    }

    return 0;
}

只為不段前行 发表于 2014-12-7 23:24:06

我懂了我懂了想通了!!!!!!!!!!!!!!:lol::lol:

只為不段前行 发表于 2014-12-7 23:24:46

自己看明白了 结果是我看走眼了!!!!!:shutup:

赵晓玉 发表于 2014-12-17 22:52:14

呵呵懂了就好不过我还没懂:sad

只為不段前行 发表于 2014-12-18 12:45:34

赵晓玉 发表于 2014-12-17 22:52
呵呵懂了就好不过我还没懂

那不懂:handshake

xueying 发表于 2014-12-29 19:52:22

mark

雪是梅之香 发表于 2015-1-19 09:37:16

请问这是哪一讲里的内容?

Furk 发表于 2015-5-5 17:51:18

同问是哪一讲的内容

8938 发表于 2015-5-14 16:45:14

{:1_1:}

EntU 发表于 2015-5-28 00:56:46

支持楼主!德玛西亚!

溯月0503 发表于 2015-5-28 09:22:21

:sad
页: [1]
查看完整版本: 小甲鱼老师的代码怎么出错拉!!!大家进来看看