| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1050 人关注过本帖, 1 人收藏
标题:关于单链表的题目
只看楼主 加入收藏
landielingwu
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2009-3-29
结帖率:75%
收藏(1)
已结贴  问题点数:20 回复次数:5 
关于单链表的题目
编写程序实现以下功能:
(1)产生10个0~99之间的随机整数,依次保存到单链表;输出单链表各结点值。
(2)从该单链表删除与给定值相等的所有结点。输出变化后的单链表各结点值,并输出单链表的长度。
(3)将该单链表的各结点值逆序保存到数组中,输出数组各元素值。


[ 本帖最后由 landielingwu 于 2010-3-20 11:14 编辑 ]
搜索更多相关主题的帖子: 单链 
2010-03-18 19:22
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
收藏
得分:4 
有时间悠闲不如自己动手写吧

★★★★★为人民服务★★★★★
2010-03-19 08:40
zhuyunshen
Rank: 6Rank: 6
等 级:侠之大者
威 望:2
帖 子:212
专家分:455
注 册:2009-11-22
收藏
得分:4 
malloc 定义动态的空间
2010-03-19 08:45
shaojie519
Rank: 2
等 级:论坛游民
帖 子:12
专家分:21
注 册:2009-7-31
收藏
得分:4 
学了数据结构,有空做一下
2010-03-22 19:08
地狱无明火
Rank: 2
等 级:论坛游民
帖 子:62
专家分:71
注 册:2009-6-11
收藏
得分:4 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct{
    int num;
}DATA;

typedef struct node NODE;

struct node{
    DATA item;
    NODE* link;
};

NODE* buildList(void);
bool  searchNode(NODE* randnum, NODE** pPre, NODE** pCur, int a);
NODE* insertNode(NODE* randnum, NODE* pPre, DATA item);
void printList(NODE* randnum);
NODE* deleteNode(NODE* randnum, NODE* pPre, NODE* pCur);
NODE* deleteData(NODE* randnum);
int*  arydesc(NODE* randnum, int* count);

int main (void)
{
    NODE* randnum;
    randnum = NULL;
    int* ary;
    int count;

    printf("Before delete: \n");
    randnum = buildList();
    printList(randnum);

    printf("\nAfter delete duplicate:\n");
    randnum = deleteData(randnum);
    printList(randnum);

    printf("\nAfter put in array descending:\n");
    ary = arydesc(randnum, &count);
    for(int i = 0; i < count; i++)
        printf("%d\n", ary[i]);
    return 0;
}

NODE* buildList(void)
{
    DATA item;
    NODE* pPre;
    NODE* pCur;
    NODE* randnum;

    randnum = NULL;
    srand(time(NULL));
    for(int i = 0; i < 10; i++)
    {
        item.num = rand()% 10 + 0;
        searchNode(randnum, &pPre, &pCur, item.num);
        randnum = insertNode(randnum, pPre, item);
    }
    return randnum;
}

bool searchNode(NODE* randnum, NODE** pPre, NODE** pCur, int a)
{
    bool found = false;

    *pPre = NULL;
    *pCur = randnum;

    while(*pCur && a > (*pCur)->item.num)
    {
        *pPre = *pCur;
        *pCur = (*pCur)->link;
    }

    if(*pCur && a == (*pCur)->item.num)
        found = true;
    return found;
}

NODE* insertNode(NODE* randnum, NODE* pPre, DATA item)
{
    NODE* pNew;

    if(!(pNew = (NODE*) malloc (sizeof(NODE))))
        printf("Memory overflow when malloc!\n");

    pNew->item = item;
    if(!pPre)
    {
        pNew->link = randnum;
        randnum = pNew;
    }
    else
    {
        pNew->link = pPre->link;
        pPre->link = pNew;
    }
    return randnum;
}

void printList(NODE* randnum)
{
    NODE* pWalker;
    int count = 0;

    pWalker = randnum;
    while(pWalker)
    {
        printf("%d\n", pWalker->item.num);
        pWalker = pWalker->link;
        count++;
    }
    printf("Total length: %d\n", count);
}

NODE* deleteNode(NODE* randnum, NODE* pPre, NODE* pCur)
{
    if(!pPre)
        randnum = pCur->link;
    else
        pPre->link = pCur->link;
    free(pCur);
    return randnum;
}

NODE* deleteData(NODE* randnum)
{
    NODE* pCur;
    NODE* pPre;
    NODE* pHead;
    NODE* pWalker;
    int a = -1;
    pWalker = randnum;

    while(pWalker)
    {
        if(a == pWalker->item.num)
        {
            searchNode(randnum, &pPre, &pCur, pWalker->item.num); //这里偶尔会出错为什么?
            deleteNode(randnum, pPre, pCur);
        }
        else a = pWalker->item.num;
        pWalker = pWalker->link;
    }
    return randnum;
}

int* arydesc(NODE* randnum, int* a)
{
    NODE* pWalker;
    int count = 0;
    int i = 0;
    int* ary;

    pWalker = randnum;

    while(pWalker)
    {
        pWalker = pWalker->link;
        count++;
    }
    *a = count;

    ary = (int*) malloc (count * sizeof(int));
    pWalker = randnum;
    while(i < count)
    {
        ary[count - i - 1] = pWalker->item.num;
        pWalker = pWalker->link;
        i++;
    }
    return ary;
}

2010-03-25 10:45
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:4 
继续 加油
2010-03-25 15:17
快速回复:关于单链表的题目
数据加载中...
 
   



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

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