标题:请教一下线性表的问题?
取消只看楼主
CodeWays
Rank: 2
等 级:论坛游民
帖 子:62
专家分:61
注 册:2010-2-7
结帖率:66.67%
已结贴  问题点数:20 回复次数:6 
请教一下线性表的问题?
程序代码:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
struct date {
        char data[MAXSIZE];
        int length;
};
void create_list(struct date *l, char str[], int n);
void init_list(struct date *l);
void destory_list(struct date *l);
int list_empty(struct date *l);
int list_length(struct date *l);
void disp_list(struct date *l);
int get_elem(struct date *l, int i, char *c);
int locate_elem(struct date *l, char c);
int list_insert(struct date *l, int i, char c);
int list_delete(struct date *l, int i, char *c);
void create_list(struct date *l, char str[], int n)
{
        int i;
        l = (struct date *) malloc(sizeof(struct date));
        for (i = 0; i < n ;i++)
                l -> data[i] = str[i];
        l -> length = n;
}
void init_list(struct date *l)
{
        l = (struct date *) malloc(sizeof(struct date));
        l -> length = 0;
}
void destroy_list(struct date *l)
{
        free(l);
}
int list_empty(struct date *l)
{
        return (l -> length == 0);
}
int list_length(struct date *l)
{
        return l -> length;
}
void disp_list(struct date *l)
{
        int i;
        if (!list_empty(l))
                return;
        for (i = 0; i < l -> length; i++)
                printf(" %c", l -> data[i]);
        printf("\n");
}
int get_elem(struct date *l, int i, char *c)
{
        if (i < 1 || i > l -> length)
                return 0;
        c = l -> data[i-1];
        return 1;
}
int locate_elem(struct date *l, char c)
{
        int i = 0;
        while (i < l -> length && l -> data[i] != c)
                i++;
        if (i >= l -> length)
                return 0;
        else
                return i + 1;
}
int list_insert(struct date *l, int i, char c)
{
        int j;
        if (i < 1 || i > l -> length + 1)
                return 0;
        i--;
        for (j = l -> length; j > i; j--)
                l -> data[j] = l -> data[j-1];
        l -> data[i] = c;
        l -> length++;
        return 1;
}
int list_delete(struct date *l, int i, char *c)
{
        int j;
        if (i < 1 || i > l -> length)
                return 0;
        i--;
        c = l -> data[i];
        for (j = i; j < l -> length - 1; j++)
                l -> data[j] = l -> data[j+1];
        l -> length--;
        return 1;
}
int main(void)
{
        struct date info;
        char str[MAXSIZE];
        fgets(str, MAXSIZE, stdin);
        create_list(&info, str, MAXSIZE);
        disp_list(&info);
        return 0;
}

测试了一下程序。。。就出问题了,create_list没有改变结构体内容,为什么呢?
搜索更多相关主题的帖子: 线性 
2010-03-03 17:54
CodeWays
Rank: 2
等 级:论坛游民
帖 子:62
专家分:61
注 册:2010-2-7
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXSIZE 100
struct date {
        char data[MAXSIZE];
        int length;
};
void create_list(struct date **l, char str[], int n);
void init_list(struct date *l);
void destory_list(struct date *l);
int list_empty(struct date *l);
int list_length(struct date *l);
void disp_list(struct date *l);
int get_elem(struct date *l, int i, char *c);
int locate_elem(struct date *l, char c);
int list_insert(struct date *l, int i, char c);
int list_delete(struct date *l, int i, char *c);
void create_list(struct date **l, char str[], int n)
{
        int i;
        *l = (struct date *) malloc(sizeof(struct date));
        for (i = 0; i < n ;i++)
                (*l) -> data[i] = str[i];
        (*l) -> length = n;
}
void init_list(struct date *l)
{
        l = (struct date *) malloc(sizeof(struct date));
        l -> length = 0;
}
void destroy_list(struct date *l)
{
        free(l);
}
int list_empty(struct date *l)
{
        return (l -> length == 0);
}
int list_length(struct date *l)
{
        return l -> length;
}
void disp_list(struct date *l)
{
        int i;
        if (list_empty(l))
                return;
        for (i = 0; i < l -> length; i++)
                printf(" %c", l -> data[i]);
        printf("\n");
}
int get_elem(struct date *l, int i, char *c)
{
        if (i < 1 || i > l -> length)
                return 0;
        c = l -> data[i-1];
        return 1;
}
int locate_elem(struct date *l, char c)
{
        int i = 0;
        while (i < l -> length && l -> data[i] != c)
                i++;
        if (i >= l -> length)
                return 0;
        else
                return i + 1;
}
int list_insert(struct date *l, int i, char c)
{
        int j;
        if (i < 1 || i > l -> length + 1)
                return 0;
        i--;
        for (j = l -> length; j > i; j--)
                l -> data[j] = l -> data[j-1];
        l -> data[i] = c;
        l -> length++;
        return 1;
}
int list_delete(struct date *l, int i, char *c)
{
        int j;
        if (i < 1 || i > l -> length)
                return 0;
        i--;
        c = l -> data[i];
        for (j = i; j < l -> length - 1; j++)
                l -> data[j] = l -> data[j+1];
        l -> length--;
        return 1;
}
int main(void)
{
        struct date *info;
        char str[MAXSIZE];
        fgets(str, MAXSIZE, stdin);
        create_list(&info, str, strlen(str));
        disp_list(info);
        return 0;
}
搞定了~

[ 本帖最后由 CodeWays 于 2010-3-3 18:46 编辑 ]

胖胖的我~
2010-03-03 18:39
CodeWays
Rank: 2
等 级:论坛游民
帖 子:62
专家分:61
注 册:2010-2-7
得分:0 
以下是引用广陵绝唱在2010-3-3 18:38:16的发言:

没看出来你这写的是线性表,那么多的函数也没用上.帮你改了下,谨作参考:
 
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAXSIZE 100
struct date {
        char data[MAXSIZE];
      ...
我是写完了后分步测试的。。。

谢谢~

胖胖的我~
2010-03-03 18:40
CodeWays
Rank: 2
等 级:论坛游民
帖 子:62
专家分:61
注 册:2010-2-7
得分:0 
抱歉。。。。懒得从编译器里复制,直接在论坛上修改了。。。少了一个*号

胖胖的我~
2010-03-03 18:45
CodeWays
Rank: 2
等 级:论坛游民
帖 子:62
专家分:61
注 册:2010-2-7
得分:0 
修改好了

胖胖的我~
2010-03-03 18:46
CodeWays
Rank: 2
等 级:论坛游民
帖 子:62
专家分:61
注 册:2010-2-7
得分:0 
看来有返回值的creat_list更清晰,修改下。。。

胖胖的我~
2010-03-03 18:47
CodeWays
Rank: 2
等 级:论坛游民
帖 子:62
专家分:61
注 册:2010-2-7
得分:0 
程序代码:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
struct date {
        char data[MAXSIZE];
        int length;
};
struct date *create_list(struct date *l, char str[], int n);
void init_list(struct date *l);
void destory_list(struct date *l);
int list_empty(struct date *l);
int list_length(struct date *l);
void disp_list(struct date *l);
int get_elem(struct date *l, int i, char *c);
int locate_elem(struct date *l, char c);
int list_insert(struct date *l, int i, char c);
int list_delete(struct date *l, int i, char *c);
struct date *create_list(struct date *l, char str[], int n)
{
        int i;
        l = (struct date *) malloc(sizeof(struct date));
        for (i = 0; i < n ;i++)
                l -> data[i] = str[i];
        l -> length = n;
        return l;
}
void init_list(struct date *l)
{
        l = (struct date *) malloc(sizeof(struct date));
        l -> length = 0;
}
void destroy_list(struct date *l)
{
        free(l);
}
int list_empty(struct date *l)
{
        return (l -> length == 0);
}
int list_length(struct date *l)
{
        return l -> length;
}
void disp_list(struct date *l)
{
        int i;
        if (list_empty(l))
                return;
        for (i = 0; i < l -> length; i++)
                printf("%c", l -> data[i]);
        printf("\n");
}
int get_elem(struct date *l, int i, char *c)
{
        if (i < 1 || i > l -> length)
                return 0;
        c = l -> data[i-1];
        return 1;
}
int locate_elem(struct date *l, char c)
{
        int i = 0;
        while (i < l -> length && l -> data[i] != c)
                i++;
        if (i >= l -> length)
                return 0;
        else
                return i + 1;
}
int list_insert(struct date *l, int i, char c)
{
        int j;
        if (i < 1 || i > l -> length + 1)
                return 0;
        i--;
        for (j = l -> length; j > i; j--)
                l -> data[j] = l -> data[j-1];
        l -> data[i] = c;
        l -> length++;
        return 1;
}
int list_delete(struct date *l, int i, char *c)
{
        int j;
        if (i < 1 || i > l -> length)
                return 0;
        i--;
        c = l -> data[i];
        for (j = i; j < l -> length - 1; j++)
                l -> data[j] = l -> data[j+1];
        l -> length--;
        return 1;
}
int main(void)
{
        struct date *info;
        char str[MAXSIZE];
        fgets(str, MAXSIZE, stdin);
        info = create_list(info, str, strlen(str));
        disp_list(info);
        return 0;
}
继续测试其它函数。。。

胖胖的我~
2010-03-03 18:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-298496-1-1.html




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

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