| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1369 人关注过本帖
标题:链表不知道哪儿错了??调不来!求帮忙!
只看楼主 加入收藏
程火山
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2017-5-3
结帖率:25%
收藏
 问题点数:0 回复次数:4 
链表不知道哪儿错了??调不来!求帮忙!
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

    //Status 为函数的类型,其值是函数结果状态代码,如OK等
typedef int Status;

    // ElemType为数据元素类型,根据实际情况而定,这里假设为int
typedef int ElemType;

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define num 10

 
struct LNode               /* 结点定义 */     
 {
   int data;
   struct LNode *next;
 };


struct LNode *creatlink();

Status ListLength(struct LNode *h);                       //(1)求表的长度函数

Status GetElem(struct LNode *L,int i,ElemType *e);        //(2)取表中的一个元素函数GetElem( L,i,&e)

void  ListInsert(struct LNode *L,int i,ElemType e);            //(4)在表中插入一个元素函数 ListInsert(&L,i,e)

Status ListEmpty(struct LNode *L);                        //(5)判断表是否空函数ListEmpty(L)

void ListDelet(struct LNode *L,int i,ElemType *e);                 //(6)删除表中数据函数ListDelete(&L,i,&e)

void print(struct LNode *h);

int main()
{
 
    int n, e=0,s,i;

    struct LNode *LinkList; /* 表的头指针类型 */

    printf("\n创建链表为:  \n");

    LinkList=creatlink();

    print(LinkList);

    n=ListLength(LinkList);

    printf("\n链表的长度:%d\n",n);

    printf("\n取表中的一个数:");

    scanf("%d",&i);
   
    s=GetElem(LinkList,i,&e);

    if(s==1)
        printf("\n存在   ");
    else
        printf("\n不存在 : ");

    ListInsert(&LinkList,i,e);

    print(LinkList);

    ListDelet(&LinkList,i,&e);

   
    print(LinkList);

    s=ListEmpty(LinkList);

    printf("\n空表即是1,不是空表即是0  :  %d\n",s);
   
    printf("\n");

    print(LinkList);

    return 0;
}

struct LNode *creatlink()
{
    int n;
   
    struct LNode *p,*q,*h;

    h=p=q=(struct LNode *)malloc(sizeof(struct LNode));

    h=NULL;
   
    for(n=0 ;  n<num  ; n++)
    {
        if(n == 0)
        {
            p->data=n;
            h=p;
        }
        else
            q->next=p;
        q=p;
        p=(struct LNode *)malloc(sizeof(struct LNode));
        p->data=n+1;
    }
    q->next =NULL;

    return h;

}

Status ListLength(struct LNode *h)  // 1
{
    int n=0;
    while(h!=NULL)
    {
        n++;
        h = h -> next;
    }
    return n;
}

void print(struct LNode *h)
{
    while(h!=NULL)
    {
        printf("%d —> ",h->data);
        h = h -> next;
    }
    puts("NULL");
}


Status GetElem(struct LNode *L,int i,ElemType *e)  // 2
 {
                                    /* L为单链表的头指针。当第i个元素存在时,其值赋给e并返回OK,否则返回ERROR */
   int j=1;                            /* j为计数器 */
   struct LNode *p=L;                /* p指向第一个结点 */
   while(p&&j<i)                /* 顺指针向后查找, 直到p指向第i个元素 或p为空 */
   {
     p=p->next;
     j++;
   }        
   if(!p||j>i)                     /* 第i个元素不存在 */
     return ERROR;
   *e=p->data;                 /* 取第i个元素 */
   return OK;
 }


Status ListEmpty(struct LNode *L)                //5
{                                /* 初始条件:链式存储的表L已存在。*/
                                /*操作结果:若L为空表,则返回TRUE,否则返回FALSE */
    if(L->next)
            return FALSE;
    else
            return TRUE;
}

//Status LocateElemLocateElem(struct LNode *L,ElemType *e,compare( ));

void ListInsert(struct LNode *L,int i,ElemType e)         //4
{
    int m=0;
    struct LNode *p,*q;
    p=(struct LNode *)malloc(sizeof(struct LNode));
    p->data = e;
    q=L;
    while( m < i )
    {
        q=q->next;
        m++;
    }
    p->next=p->next ;
    q->next = p;
}

void ListDelet(struct LNode *L,int i,ElemType *e)  //6 删除L的第i个数据元素,并用e返回其值,
{

    int m=0;
    struct LNode *p,*q;
    p=L;
    while( m < i )
    {
        q=p;
        p=p->next;
        m++;
    }
    *e=p->data;
    q->next=p->next ;
    free(p);
}

[此贴子已经被作者于2017-5-22 14:54编辑过]

搜索更多相关主题的帖子: include 元素 
2017-05-22 14:43
程火山
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2017-5-3
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册


[此贴子已经被作者于2017-5-22 14:56编辑过]

2017-05-22 14:44
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:0 
指针级别不同……要调用指针的地址~这样才可以通过地址来改变该指针的指向~~

[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2017-05-22 16:17
Emotiona
Rank: 7Rank: 7Rank: 7
等 级:黑侠
帖 子:311
专家分:581
注 册:2017-3-7
收藏
得分:0 
自己对照, 基本没改你的表

程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

    //Status 为函数的类型,其值是函数结果状态代码,如OK等
typedef int Status;

    // ElemType为数据元素类型,根据实际情况而定,这里假设为int
typedef int ElemType;

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define num 10


struct LNode               /* 结点定义 */

 {
   int data;
   struct LNode *next;

 };


struct LNode *creatlink();

Status ListLength(struct LNode *h);                       //(1)求表的长度函数

Status GetElem(struct LNode *L,int i,ElemType *e);        //(2)取表中的一个元素函数GetElem( L,i,&e)

void  ListInsert(struct LNode *L,int i,ElemType e);            //(4)在表中插入一个元素函数 ListInsert(&L,i,e)

Status ListEmpty(struct LNode *L);                        //(5)判断表是否空函数ListEmpty(L)

void ListDelet(struct LNode *L,int i,ElemType *e);                 //(6)删除表中数据函数ListDelete(&L,i,&e)

void print(struct LNode *h);

int main()
{

    int n, e=0,s,i;

    struct LNode *LinkList; /* 表的头指针类型 */

    printf("\n创建链表为:  \n");

    LinkList=creatlink();

    print(LinkList);

    n=ListLength(LinkList);

    printf("\n链表的长度:%d\n",n);

    printf("\n取表中的一个数:");

    scanf("%d",&i);

    s=GetElem(LinkList,i++,&e);

    if(s==1)
        printf("\n存在  val = %d\n\n", e);


    else
        printf("\n不存在 : ");


    ListInsert(LinkList,i++,e);       //指针不需要加地址
    print(LinkList);

    ListDelet(LinkList,i++,&e);
    if(e == -1)
    printf("删除第%d个结点成功\n", i - 1);
    print(LinkList);

    s=ListEmpty(LinkList);

    printf("\n空表即是1,不是空表即是0  :  %d\n",s);

    printf("\n");

    print(LinkList);

    return 0;
}

struct LNode *creatlink()
{
    int n;

    struct LNode *p,*q,*h;

    h=p=q=(struct LNode *)malloc(sizeof(struct LNode));

    h=NULL;

    for(n=0 ;  n<num  ; n++)
    {
        if(n == 0)
        {
            p->data=n;
            h=p;
        }
        else
            q->next=p;
        q=p;
        p=(struct LNode *)malloc(sizeof(struct LNode));
        p->data=n+1;
    }
    q->next =NULL;

    return h;

}

Status ListLength(struct LNode *h)  // 1
{
    int n=0;
    while(h!=NULL)
    {
        n++;
        h = h -> next;
    }
    return n;
}

void print(struct LNode *h)
{
    while(h!=NULL)
    {
        printf("%d —> ",h->data);
        h = h -> next;
    }
    puts("NULL");
}


Status GetElem(struct LNode *L,int i,ElemType *e)  // 2
 {
                                    /* L为单链表的头指针。当第i个元素存在时,其值赋给e并返回OK,否则返回ERROR */
   int j=0;                            /* j为计数器 */  //计数器应该为0,假如找第一个元素,计数器为2,既然用个计数器,又不返回,也不输出,也不返回有什么意义
   struct LNode *p=L;                /* p指向第一个结点 */
   while(p&&j<i)                /* 顺指针向后查找, 直到p指向第i个元素 或p为空 */
   {
     p=p->next;
     j++;
   }
   if(p && j == i)                  /* 第i个元素存在 */
   {
       *e=p->data;
       return OK;
   }
   else               //不存在第i个元素
     return ERROR;



 }


Status ListEmpty(struct LNode *L)                //5
{                                /* 初始条件:链式存储的表L已存在。*/
     printf("%d\n", L->next);                           /*操作结果:若L为空表,则返回TRUE,否则返回FALSE */
    if(L->next)
            return TRUE;           //条件写反了
    else
            return FALSE;
}

//Status LocateElemLocateElem(struct LNode *L,ElemType *e,compare( ));

void ListInsert(struct LNode *L,int i,ElemType e)         //4
{
    int m=0;
    struct LNode *p, *p1,*q;
    p=(struct LNode *)malloc(sizeof(struct LNode));
    p->data = e;
    q=L;
    while( m < i )
    {

        q=q->next;
        m++;
    }
    p1 = q->next;           //这里有问题, 要建立指针来保存找到的下一个地址
    q->next = p;
    p->next = p1;

}

void ListDelet(struct LNode *L,int i,ElemType *e)  //6 删除L的第i个数据元素,并用e返回其值,
{

    int m=0;
    struct LNode *p,*q;
    p=L;
    while( m < i )
    {
        q=p;
        p=p->next;
        m++;
    }
    *e= -1 ;
    q->next=p->next ;
    free(p);
}
2017-05-23 01:18
程火山
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2017-5-3
收藏
得分:0 
真的感谢!!

void ListInsert(struct LNode *L,int i,ElemType e)         //4
{
    int m=0;
    struct LNode *p, *p1,*q;
    p=(struct LNode *)malloc(sizeof(struct LNode));
    p->data = e;
    q=L;
    while( m < i )
    {

        q=q->next;
        m++;
    }
    p1 = q->next;           //这里有问题, 要建立指针来保存找到的下一个地址   
    q->next = p;
    p->next = p1;

}
 p->next=p->next ;
    q->next = p;






void ListInsert(struct LNode *L,int i,ElemType e)         //4
{
    int m=0;
    struct LNode *p,*q;
    p=(struct LNode *)malloc(sizeof(struct LNode));
    p->data = e;
    q=L;
    while( m < i )
    {
        q=q->next;
        m++;
    }
    p->next=p->next ;   这里写错了  发现了  应该是 p->next=q->next!!
    q->next = p;
}
这样就对了
2017-05-23 01:56
快速回复:链表不知道哪儿错了??调不来!求帮忙!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018949 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved