| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 859 人关注过本帖
标题:单链表哪里出错了
取消只看楼主 加入收藏
hg654321free
Rank: 1
等 级:新手上路
帖 子:15
专家分:5
注 册:2013-4-20
收藏
 问题点数:0 回复次数:3 
单链表哪里出错了
#include <stdio.h>
#include <stdlib.h>
#define ERROR 0
#define OK 1
//typedef int ElemType;
#define ElemType int


typedef struct Node
{
    ElemType data;    //&Ecirc;&yacute;&frac34;&Yacute;&Oacute;ò
    struct Node *next;  //&Ouml;&cedil;&Otilde;&euml;&Oacute;ò
}Node ,*LinkList;


void CreateListHead(LinkList *L, int n)
{
    LinkList p,r;
    int i=1;

    srand(time(0));       //初始化随机种子

    *L = (LinkList)malloc(sizeof(struct Node));        //先建立一个带头结点的单链表

    if(*L == NULL)

    {

        fprintf(stderr, "malloc() error.\n");

        return ERROR;

    }

    (*L)->next = NULL;


    while(i <= n)

    {

        p = (LinkList)malloc(sizeof(struct Node));

        if( p == NULL)

        {

            fprintf(stderr, "malloc() error.\n");

            return ERROR;

        }

        p->data = rand() % 100 + 1;

        p->next = (*L)->next;

        (*L)->next = p;                                    //插入到表头



        i++;

    }


    r=*L;      //尾部结点

    for(i=0; i<n; i++)
    {
        p=(Node *) malloc(sizeof(Node));
        p->data=rand()%100+1;
        r->next =p;
        r=p;
    }
}

int ClearList(LinkList *L)
{
    LinkList p,q;

    p=(*L)->next;

    while(p)
    {
        q=p->next;
        free(p);
        p=q;
    }

    (*L)->next=NULL;

    return OK;
}

int printList(LinkList L)

{

    LinkList p;

    p = L->next;

    if(p == NULL)

    {

        printf("链表为空.\n");

        return ERROR;

    }

    while(p)

    {

        printf("%d ", p->data);

        p = p->next;

    }

    printf("\n");



    return OK;

}


int main()
{
    LinkList L;
    printf("Hello world!\n");

    CreateListHead(&L,1);
    printList(L);
    return 0;
}
搜索更多相关主题的帖子: next include 
2013-04-24 18:54
hg654321free
Rank: 1
等 级:新手上路
帖 子:15
专家分:5
注 册:2013-4-20
收藏
得分:0 
回复 2楼 邓士林
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ERROR 0
#define OK 1
//typedef int ElemType;
#define ElemType int


typedef struct Node
{
    ElemType data;    //&Ecirc;&yacute;&frac34;&Yacute;&Oacute;ò
    struct Node *next;  //&Ouml;&cedil;&Otilde;&euml;&Oacute;ò
}Node ,*LinkList;


int CreateListHead(LinkList *L, int n)
{
    LinkList p,r;
    int i=1;

    srand(time(0));       //初始化随机种子

        *L = (LinkList)malloc(sizeof(struct Node));        //先建立一个带头结点的单链表

    if(*L == NULL)

    {

        fprintf(stderr, "malloc() error.\n");

        return ERROR;

    }

    (*L)->next = NULL;


    while(i <= n)

    {

        p = (LinkList)malloc(sizeof(struct Node));

        if( p == NULL)

        {

            fprintf(stderr, "malloc() error.\n");

            return ERROR;

        }

        p->data = rand() % 100 + 1;

        p->next = (*L)->next;

        (*L)->next = p;                                    //插入到表头



        i++;

    }


    r=*L;      //尾部结点

    for(i=0; i<n; i++)
    {
        p=(Node *) malloc(sizeof(Node));
        p->data=rand()%100+1;
        r->next =p;
        r=p;
    }
}

int ClearList(LinkList *L)
{
    LinkList p,q;

    p=(*L)->next;

    while(p)
    {
        q=p->next;
        free(p);
        p=q;
    }

    (*L)->next=NULL;

    return OK;
}

int printList(LinkList L)

{

    LinkList p;

    p = L->next;

    if(p == NULL)

    {

        printf("链表为空.\n");

        return ERROR;

    }

    while(p)

    {

        printf("%d ", p->data);

        p = p->next;

    }

    printf("\n");



    return OK;

}


int main()
{
    LinkList L;
    printf("Hello world!\n");

    CreateListHead(&L,2);
    printList(L);
    return 0;
}

还是不行呀
2013-04-24 19:27
hg654321free
Rank: 1
等 级:新手上路
帖 子:15
专家分:5
注 册:2013-4-20
收藏
得分:0 
回复 3楼 hg654321free
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ERROR 0
#define OK 1
//typedef int ElemType;
#define ElemType int


typedef struct Node
{
    ElemType data;    //&Ecirc;&yacute;&frac34;&Yacute;&Oacute;ò
    struct Node *next;  //&Ouml;&cedil;&Otilde;&euml;&Oacute;ò
}Node ,*LinkList;


int CreateListHead(LinkList *L, int n)
{
    LinkList p,r;
    int i=1;

    srand(time(0));       //初始化随机种子

    (*L) = (LinkList)malloc(sizeof(struct Node));        //先建立一个带头结点的单链表

    if((*L) == NULL)

    {

        fprintf(stderr, "malloc() error.\n");

        return ERROR;

    }

    (*L)->next = NULL;


    while(i <= n)

    {

        p = (LinkList)malloc(sizeof(struct Node));

        if( p == NULL)

        {

            fprintf(stderr, "malloc() error.\n");

            return ERROR;

        }

        p->data = rand() % 100 + 1;

        p->next = (*L)->next;

        (*L)->next = p;                                    //插入到表头



        i++;

    }


    r=*L;      //尾部结点

    for(i=0; i<n; i++)
    {
        p=(Node *) malloc(sizeof(Node));
        p->data=rand()%100+1;
        r->next =p;
        r=p;
    }

    r->next = NULL;                                        //表示当前链表结束



    return OK;




}

int ClearList(LinkList *L)
{
    LinkList p,q;

    p=(*L)->next;

    while(p)
    {
        q=p->next;
        free(p);
        p=q;
    }

    (*L)->next=NULL;

    return OK;
}

int printList(LinkList L)

{

    LinkList p;

    p = L->next;

    if(p == NULL)

    {

        printf("链表为空.\n");

        return ERROR;

    }

    while(p)

    {

        printf("%d ", p->data);

        p = p->next;

    }

    printf("\n");



    return OK;

}


int main()
{
    LinkList L;
    printf("Hello world!\n");

    CreateListHead(&L,6);
    printList(L);
    return 0;
}
现在可以了
2013-04-24 19:31
hg654321free
Rank: 1
等 级:新手上路
帖 子:15
专家分:5
注 册:2013-4-20
收藏
得分:0 
回复 2楼 邓士林
现在可以,原来看漏了
2013-04-24 19:32
快速回复:单链表哪里出错了
数据加载中...
 
   



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

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