| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 542 人关注过本帖
标题:两天改n遍,请哥哥姐姐们再给指下错误啊[链表创建,输出,删除]
只看楼主 加入收藏
xdlearner
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2012-3-7
结帖率:80%
收藏
已结贴  问题点数:20 回复次数:8 
两天改n遍,请哥哥姐姐们再给指下错误啊[链表创建,输出,删除]
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define INFEASIBLE -2;
#define OK 0;
struct node{
    int data;
    struct node *next;
};
typedef struct node linklist;
int creast(linklist *h,int n);
int del(linklist *h);
int scan(linklist *h);
int main(){
    int n,i;
    linklist *h;
    printf("Please input the n:");
    scanf("%d",&n);
    h=(linklist*)malloc(sizeof(struct node));
    creast(h,n);
    for(i=0;i<n;i++){
        scan(h);
        del(h);
    }
    return 0;
}
int creast(linklist *h,int n){
    printf("Please input the data:");
    int i;
    linklist *p;
    h=(linklist*)malloc(sizeof(struct node));
    if(!h)
        return INFEASIBLE;
    h->next=NULL;
    for(i=n;i>0;i--){
        p=(linklist*)malloc(sizeof(struct node));
        if(!p)
            return INFEASIBLE;
        scanf("%d",&p->data);
        p->next=h->next;
        h->next=p;
    }
    return OK;
}
int scan(linklist *h){
    linklist *m;
    if(h=NULL) printf("Empty List!\n");
    m=(linklist*)malloc(sizeof(struct node));
    m=h;
    while(m){
    printf("%d\t",m->next->data);
    m=m->next;
    }
    return OK;
}
int del(linklist *h){
    linklist *q;
    q=(linklist*)malloc(sizeof(struct node));
    q=h->next;
    h->next=q->next;
    free(q);
    return OK;
}
搜索更多相关主题的帖子: next 哥哥 
2012-03-11 20:27
xdlearner
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2012-3-7
收藏
得分:0 
编译可以通过,但是在运行的时候就会停止,只能输入那几个数字,但是不能运行删除和输出,怀疑是存在空指针问题,但是自己不懂啊,希望各位能帮忙给看看,谢谢啦,这个程序我编了一周了。。。
2012-03-11 20:34
adgvcxz
Rank: 2
等 级:论坛游民
帖 子:23
专家分:52
注 册:2009-6-26
收藏
得分:20 
回复 2楼 xdlearner
错误给你找出来了   我觉得你应该找本正规点的书看看。。
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define INFEASIBLE -2;
#define OK 0;
struct node{
    int data;
    struct node *next;
};
typedef struct node linklist;
int creast(linklist *h,int n);
int del(linklist *h);
int scan(linklist *h);
int main(){
    int n,i;
    linklist *h;
    printf("Please input the n:");
    scanf("%d",&n);
    h=(linklist*)malloc(sizeof(struct node));
    creast(h,n);
    for(i=0;i<n;i++){
        scan(h);
        del(h);
    }
    return 0;
}
int creast(linklist *h,int n){
    int i;
    linklist *p;
    printf("Please input the data:");
    //h=(linklist*)malloc(sizeof(struct node));
    if(!h)
        return INFEASIBLE;
    h->next=NULL;
    for(i=n;i>0;i--){
        p=(linklist*)malloc(sizeof(struct node));
        if(!p)
            return INFEASIBLE;
        scanf("%d",&p->data);
        p->next=h->next;
        h->next=p;
    }
    return OK;
}
int scan(linklist *h){
    linklist *m;
    if(h->next==NULL) printf("Empty List!\n");
    m=(linklist*)malloc(sizeof(struct node));
    m=h->next;
    while(m){
    printf("%d\t",m->data);
    m=m->next;
    }
    return OK;
}
int del(linklist *h){
    linklist *q;
    q=(linklist*)malloc(sizeof(struct node));
    q=h->next;
    h->next=q->next;
    free(q);
    return OK;
}

2012-03-11 22:15
kingofhevil
Rank: 1
等 级:新手上路
帖 子:37
专家分:6
注 册:2012-3-7
收藏
得分:0 
《C语言入门经典》By Ivor Horton行不行?
2012-03-11 22:19
蛋蛋花
Rank: 1
等 级:新手上路
帖 子:4
专家分:2
注 册:2010-11-28
收藏
得分:0 
你这个程序有很多问题。
首先int creast(linklist *h,int n)参数有问题。因该是int creast(linklist **h,int n)
在main函数里面没有必要申请内存。creast里面也没有必要申请两次内存               
2012-03-11 23:27
xdlearner
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2012-3-7
收藏
得分:0 
我是软件专业的,数据结构看的是严魏敏的经典教材,c语言看的是谭c
2012-03-12 12:27
xdlearner
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2012-3-7
收藏
得分:0 
回复 3楼 adgvcxz
谢谢啦,十分感谢。。。。
2012-03-12 12:33
xdlearner
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2012-3-7
收藏
得分:0 
程序代码:
#include<stdio.h>
#include<stdlib.h>
#define INFEASIBLE -2;
#define OK 0;
struct node{
     int data;
     struct node *next;

 };

 typedef struct node linklist;

 int creast(linklist *h,int n);

 int del(linklist *h);

 int scan(linklist *h);

 int mp(linklist *h);

 int main(){
     int n,i;
     linklist *h;
     printf("Please input the n:");
     scanf("%d",&n);
     h=(linklist*)malloc(sizeof(struct node));
     creast(h,n);
     mp(h);
     for(i=0;i<n;i++){
         scan(h);
         del(h);
         printf("\n");
     }
     return 0;

 }

 int creast(linklist *h,int n){
     printf("Please input the data:");
     int i;
     linklist *p;
    //h=(linklist*)malloc(sizeof(struct node));
     if(!h)
         return INFEASIBLE;
     h->next=NULL;
     for(i=n;i>0;i--){
         p=(linklist*)malloc(sizeof(struct node));
         if(!p)
             return INFEASIBLE;
         scanf("%d",&p->data);
         p->next=h->next;
         h->next=p;
     }
     return OK;

 }

 int scan(linklist *h){
     linklist *m;
     if(h==NULL) printf("Empty List!\n");
     m=(linklist*)malloc(sizeof(struct node));
     m=h->next;
     while(m){
     printf("%d\t",m->data);
     m=m->next;
     }
     return OK;

 }

 int del(linklist *h){
     linklist *q;
     q=(linklist*)malloc(sizeof(struct node));
     q=h->next;
     h->next=q->next;
     free(q);
     return OK;

 }

 int mp(linklist *h){
    linklist *e,*f;
    e=(linklist*)malloc(sizeof(struct node));
    f=(linklist*)malloc(sizeof(struct node));
    int a;
    if(h==NULL) return INFEASIBLE;
    for(e=h->next;e!= NULL;e=e->next){
        for(f=h->next;f->next!=NULL;f=f->next){
            if(f->data > f->next->data){
                a=f->data;
                f->data = f->next->data;
                f->next->data=a;
            }
        }
    }
        free(e);
        free(f);
    return OK;

 }

我的这个搞进去冒泡之后就又完蛋了,怎么回事哇。。。。。
2012-03-12 16:30
蛋蛋花
Rank: 1
等 级:新手上路
帖 子:4
专家分:2
注 册:2010-11-28
收藏
得分:0 
#include<stdio.h>
 #include<stdlib.h>

 typedef struct node
 {
     int nData;
     struct node *pLeft;
     struct node *pRight;
 }linklist_t;

bool ListCreate(linklist_t **h, int n);
bool Listdel(linklist_t *h);
bool ListInit(linklist_t *h);

int main()
{
    linklist_t *pstList;
    int n;
    printf("please input the number of list:");
    scanf("%d", &n);
     if(!ListCreate(&pstList, n))
     {
        printf("create error!!!!\n");
        return 0;
     }

     if(!ListInit(pstList))
     {
        printf("init error!!!!\n");
        return 0;
     }
     return 0;
}
bool ListCreate(linklist_t **h,int n)
 {
    linklist_t *pheader = NULL;
    linklist_t *pTemp1 = NULL;
    linklist_t *pTemp2 = NULL;
    int nIndex = 0;

    if(h == NULL)
    {
        return false;
    }
   
    if(n <= 0)
    {
        return false;
    }

    pheader = pTemp1 = (linklist_t *)malloc(sizeof(linklist_t));
    if(pheader == NULL)
    {
        return false;
    }
    pheader->pLeft = NULL;
    nIndex++;
    while(nIndex < n)
    {
        pTemp2 = pTemp1;
        pTemp1 = NULL;
        pTemp1 = (linklist_t*)malloc(sizeof(linklist_t));
        if(pTemp1 == NULL)
        {
            return false;
        }
        pTemp2->pRight = pTemp1;
        pTemp1->pLeft = pTemp2;

        nIndex++;
    }

    pTemp1->pRight = NULL;
    *h = pheader;

    return true;
 }
 bool ListInit(linklist_t *h)
 {
     int i = 0;
     if(h == NULL)
     {
        return false;
     }

     while(h != NULL)
     {
        h->nData = i;
        i++;
        h = h->pRight;
     }
     return true;
 }
 bool Listdel(linklist_t *h)
 {
     return true;
 }
2012-03-13 23:44
快速回复:两天改n遍,请哥哥姐姐们再给指下错误啊[链表创建,输出,删除]
数据加载中...
 
   



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

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