| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1201 人关注过本帖
标题:一个线性表的实验,跪求高手给出代码~
只看楼主 加入收藏
isbn0000
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2009-3-21
结帖率:75%
收藏
 问题点数:0 回复次数:4 
一个线性表的实验,跪求高手给出代码~
创建一个大小为5的整数线性表L;
输出该表的长度(为0);在第0个元素之后插入2;在第一个元素之后插入6(至此,线性表为
2,6);寻找并输出第一个元素(为2);输出当前表的长度(为2);删除并输出第一个元素。
搜索更多相关主题的帖子: 代码 实验 线性 
2009-10-23 22:56
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
我非常讨厌线性表。

—>〉Sun〈<—
2009-10-24 00:34
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
// 没编译器,所以未经编译。
#include <stdio.h>
#include <stdlib.h>
 
typedef int Item;
 
typedef struct List {
    int m_count;       // 线性表,以有元素数量
    int m_length;      // 线性表,最大元素数量
    int m_index;       // 线性表,元素索引
    Item * m_pitem;    // 线性表指针
} List;
 
List*  destroy_list(List *p);          // 摧毁列表
int    error_list(List *p);            // 判断列表是否有错误
List*  create_list(int n);             // 创建并初始化列表
int    is_empty_list(List *p);         // 列表是否为空
int    is_full_list(List *p);          // 列表是否已满
int    len_list(List *p);                         // 返回该列表的已有项目数量
int    maxlen_list(List *p);                      // 返回该列表的最大项目数量
List*  push_item_list(List *p, Item *pitem);      // 把Item数据送入列表
List*  pop_item_list(List *p, Item *pitem);       // 后进先出
List*  out_item_list(List *p, Item *pitem);       // 先进先出
Item   get_item_list(List*p, int i);              // 以索引形式访问列表数据
 
// 有错误所以删除了

[ 本帖最后由 cosdos 于 2009-10-24 15:31 编辑 ]

—>〉Sun〈<—
2009-10-24 14:59
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
// 修改了一些拼写错误和';',编译通过。
 
#include <stdio.h>
#include <stdlib.h>
 
typedef int Item;
 
typedef struct List {
    int m_count;       // 线性表,以有元素数量
    int m_length;      // 线性表,最大元素数量
    int m_index;       // 线性表,元素索引
    Item * m_pitem;    // 线性表指针
} List;
 
List*  destroy_list(List *p);          // 摧毁列表
int    error_list(List *p);            // 判断列表是否有错误
List*  create_list(int n);             // 创建并初始化列表
int    is_empty_list(List *p);         // 列表是否为空
int    is_full_list(List *p);          // 列表是否已满
int    len_list(List *p);                         // 返回该列表的已有项目数量
int    maxlen_list(List *p);                      // 返回该列表的最大项目数量
List*  push_item_list(List *p, Item *pitem);      // 把Item数据送入列表
List*  pop_item_list(List *p, Item *pitem);       // 后进先出
List*  out_item_list(List *p, Item *pitem);       // 先进先出
Item   get_item_list(List*p, int i);              // 以索引形式访问列表数据
 
//---------------------------------------------
 
#define MAX_LEN 5
int main(void)
{
    List * mylist;
    Item a;
    printf("创建列表,可容纳%d个项目。\n", MAX_LEN);
    mylist = create_list(MAX_LEN);
    printf("现在列表中共有%d个项目。\n", len_list(mylist));
    printf("插入2。\n");
    a = 2;
    push_item_list(mylist, &a);
    printf("插入6。\n");
    a = 6;
    push_item_list(mylist, &a);
    printf("输出第1个元素%d。\n", get_item_list(mylist, 0));
    printf("现在列表中共有%d个项目。\n", len_list(mylist));
    out_item_list(mylist, &a);
    printf("删除并输出第一个项目%d。\n", a);
    printf("现在列表中共有%d个项目。\n", len_list(mylist));
    getchar();
    return 0;
}
 
 
//---------------------------------------------
Item* create_item_array(int n)  // 创建项目数组
{
    if (n > 0)  
        return (Item*)malloc(n * sizeof(Item));
    else if (n < 0) {
        puts("Item error: Qty < 0");
        exit(1);
    }
    return NULL;
}
 
List* destroy_list(List *p)
{
    if (p) {
        free(p->m_pitem);
        free(p);
    }
    return NULL;
}
 
int error_list(List *p)
{
    if (!p) {
        puts("List error: NULL pointer!");
        exit(1);
    }
    if ( (p->m_length < 0 || p->m_length < p->m_count  || p->m_length < p->m_index)
            || (p->m_length && !p->m_pitem)
                || (!p->m_length && p->m_pitem) ) {
        p->m_count = p->m_length = p->m_index = 0;
        free(p->m_pitem);
        puts("List error: Member Error!");
        exit(1);
    }
    return 0;
}
 
List* create_list(int n)
{
    List *p = NULL;
    p = (List*)malloc(n * sizeof(Item));
    if (p) {
        p->m_pitem = create_item_array(n);
        p->m_length = n;
        p->m_count = 0;
        p->m_index = 0;
    }
    if (error_list(p))
        return NULL;
    return p;
}
 
int is_empty_list(List *p)
{
    if (error_list(p))
        exit(1);
    if (p->m_count)
        return 0;
    return 1;
}
 
int is_full_list(List *p)
{
    if (error_list(p))
        exit(1);
    if (is_empty_list(p) || p->m_length > p->m_count)
        return 0;
    return 1;
}
 
int    len_list(List *p)
{
    if (error_list(p))
        exit(1);
    return p->m_count;     
}
 
int    maxlen_list(List *p)
{
    if (error_list(p))
        exit(1);
    return p->m_length;     
}
 
List* push_item_list(List *p, Item *pitem)
{
    if (error_list(p))
        exit(1);
    if (is_full_list(p))
        return NULL;
 
    ++p->m_index;
    if (is_empty_list(p) || p->m_index == p->m_length)
        p->m_index = 0;
 
    if (p->m_pitem == NULL && (p->m_pitem = create_item_array(5)) == NULL)
        return NULL;
 
    p->m_pitem[p->m_index] = *pitem;
    ++p->m_count;
    return p;
}
 
List* pop_item_list(List *p, Item *pitem)
{
    if (error_list(p))
        exit(1);
    if (is_empty_list(p))
        return NULL;
 
    *pitem =     p->m_pitem[p->m_index];
    if (p->m_index)
        --p->m_index;
    else
        p->m_index = p->m_length - 1;
    --p->m_count;
    return p;
}
 
List* out_item_list(List *p, Item *pitem)
{
    int st = 0;
    if (error_list(p))
        exit(1);
    if (is_empty_list(p))
        return NULL;
 
    st = (p->m_index + 1) - p->m_count;
    st = (p->m_length + st) % p->m_length;
    *pitem = p->m_pitem[st];
    --p->m_count;
    return p;
}
 
Item get_item_list(List*p, int i)
{
    int st;
    if (error_list(p))
        exit(1);
    if (is_empty_list(p)) {
        puts("List error: Empty List!");
        exit(1);
    }
    if (i >= p->m_count) {
        exit(1);
        puts("Index : Error!");
    }
    st = (p->m_index + 1) - p->m_count;
    i = (p->m_length + i) % p->m_length;
    return p->m_pitem[i];
}


[ 本帖最后由 cosdos 于 2009-10-24 15:32 编辑 ]

—>〉Sun〈<—
2009-10-24 15:29
isbn0000
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2009-3-21
收藏
得分:0 
回复 4楼 cosdos
顶你顶你
2009-11-18 03:40
快速回复:一个线性表的实验,跪求高手给出代码~
数据加载中...
 
   



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

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