| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1444 人关注过本帖
标题:大家有没有好的结构体链表的例子啊。。
只看楼主 加入收藏
幻姬LOVE
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2008-1-20
收藏
 问题点数:0 回复次数:9 
大家有没有好的结构体链表的例子啊。。
我看书看的饶啊饶啊。。看到什么结点就头痛。。。真不知道它是怎么饶的。。。
搜索更多相关主题的帖子: 链表 结构体 例子 
2008-01-20 19:29
闪闪4521
Rank: 1
等 级:新手上路
帖 子:196
专家分:0
注 册:2007-11-30
收藏
得分:0 
要什么例子呀
说明白点
2008-01-20 20:59
幻姬LOVE
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2008-1-20
收藏
得分:0 
这个怎么看的啊。。。盯着看了半天了。。。
struct node* createl()
{
  struct node* head=NULL,*p;
float f;
while(1)
{
  p=(struct node*)malloc(sizeof(struct node));
scanf("%d",&p->num);
if(p->num==0) break;
scanf("%f",&f);p->score=f;
p->next=NULL;
p->next=head;
head=p;
}
free(p);
return head;
}
红字的地方什么意思啊。。。。怎么一会儿空一会儿又赋值的。。。
2008-01-21 19:00
永夜的极光
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:2721
专家分:1
注 册:2007-10-9
收藏
得分:0 
你看的什么书啊,显然写错了,换本书看吧
第一句p->next=NULL;没用的

从BFS(Breadth First Study)到DFS(Depth First Study)
2008-01-21 20:45
闪闪4521
Rank: 1
等 级:新手上路
帖 子:196
专家分:0
注 册:2007-11-30
收藏
得分:0 
这个建链表的函数确实有问题耶,
那一段是不是打错了,不懂他什么意思
2008-01-21 20:53
icelovebai
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2007-9-19
收藏
得分:0 
这样写或许更容易明白点:
typedef struct node{
 int data;
 struct node * next;
}node,*List;
List createLink(List head)
{
    printf("The System is ready to create Link...\tBy:Bai Chunli\n");
    List p,q;
    p=(List)malloc(sizeof(node));
    head=(List)malloc(sizeof(node));
    printf("input data NO.1: >");
    scanf("%d",&p->one);
    printf("input data NO.2: >");
    scanf("%d",&p->exp);
    p->next=NULL;
    head=p;

    printf("------next link-------\n");

    while(1)
    {
        q=(List)malloc(sizeof(node));
        printf("input data NO.1: >");
         scanf("%d",&q->one);
        if(q->one==0)
        {
            printf("------link end--------\n");
            break;
        }
        printf("input data NO.2: >");
        scanf("%d",&q->exp);

        printf("------next link-------\n");

        q->next=NULL;
        p->next=q;
        p=q;
    }
    return head;
}

[[italic] 本帖最后由 icelovebai 于 2008-1-21 22:15 编辑 [/italic]]
2008-01-21 22:13
lypoem
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-1-19
收藏
得分:0 
LZ 的那个建立链表的函数应该不行吧
2008-01-22 00:10
Gnail
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-1-17
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

//链表结构
struct list
{
    char ch;
    struct list *next;
};

//为链表结构创建别名
typedef struct list *LISTPTR;

//函数原形
LISTPTR addToList(char ch,LISTPTR first);
void freeMemorgList(LISTPTR first);
void printList(LISTPTR first);

//程序入口
void main()
{
    LISTPTR first = NULL;
    int i = 0;
    char ch;
    char trash[256];

    while(i++ < 7)
    {
        ch = 0;
        printf("Enter %d",i);
        do
        {
            printf("Must to 'a'-'z'");
            ch = getc(stdin);    //从键盘得到一个字符
            gets(trash);        //清空键盘缓冲区多余字符
        }while((ch < 'a' || ch > 'z') && (ch < 'A' || ch > 'Z'));
        first = addToList(ch,first);
    }

    printList(first);
    freeMemorgList(first);
}

//添加链表节点函数并对节点排序
LISTPTR addToList(char ch,LISTPTR first)
{
    LISTPTR new_rec = NULL;
    LISTPTR tmp_rec = NULL;
    LISTPTR prev_rec = NULL;

    new_rec = (LISTPTR)malloc(sizeof(list));
    if(!new_rec)
    {
        puts("memory error..");
        getch();
        exit(1);
    }
    new_rec->ch = ch;
    new_rec->next = NULL;
    if(first==NULL)
    {
        first = new_rec;
        new_rec->next = NULL;
    }
    else if(new_rec->ch < first->ch)
    {
        new_rec->next = first;
        first = new_rec;
    }
    else
    {
        tmp_rec = first->next;
        prev_rec = first;
        if(tmp_rec==NULL)
        {
            prev_rec->next = new_rec;
        }
        else
        {
            while((tmp_rec->next!=NULL))
            {
                if(new_rec->ch < tmp_rec->ch)
                {
                    new_rec->next = tmp_rec;
                    /*if(new_rec->next != prev_rec->next)//调试代码
                    {
                        puts("ERROR");
                        getch();
                        exit(0);
                    }*/
                    prev_rec->next = new_rec;
                    break;
                }
                else
                {
                    tmp_rec = tmp_rec->next;
                    prev_rec = prev_rec->next;
                }
            }

            if(tmp_rec->next == NULL)
            {
                if(new_rec->ch < tmp_rec->ch)
                {
                    new_rec->next = tmp_rec;
                    prev_rec->next = new_rec;
                }
                else
                {
                    tmp_rec->next = new_rec;
                    new_rec->next = NULL;
                }
            }
        }
    }

    return (first);
}

//打印链表内容
void printList(LISTPTR first)
{
    int counter =1;

    while(first != NULL)
    {
        printf("%X\t",first);
        printf("%d\t%c\t",counter++,first->ch);
        printf("%X\n",first->next);
        first = first->next;
    }
}

//释放链表占用的全部内存
void freeMemorgList(LISTPTR first)
{
    LISTPTR cur_ptr,next_rec;
    cur_ptr = first;

    while(cur_ptr != NULL)
    {
        next_rec = cur_ptr->next;
        free(cur_ptr);
        cur_ptr = next_rec;
    }
}
2008-01-22 00:16
幻姬LOVE
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2008-1-20
收藏
得分:0 
我在研究大家写的东西啊。。。谢谢大家哦。链表好烦啊。这本书是我们大学的上课书啊。
2008-01-22 09:00
a378585
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2010-11-22
收藏
得分:0 
现在也在学习链表,好头大,一下真难理解,有谁有对这方面讲的比较讲义或例子吗?
2010-11-23 19:31
快速回复:大家有没有好的结构体链表的例子啊。。
数据加载中...
 
   



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

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