| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 263 人关注过本帖
标题:高手请进,帮帮小弟
只看楼主 加入收藏
ococo123456
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-10-25
结帖率:100%
收藏
已结贴  问题点数:2 回复次数:3 
高手请进,帮帮小弟
帮忙批改一下这个线性表程序。
实验1.rar (1.69 KB)
2009-10-26 20:26
ococo123456
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2009-10-25
收藏
得分:0 
同志们,快啊,就是后面第四个选项实现不了。
2009-10-28 21:11
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
4 ------- Get a element's locationt in the SqList.

这个

—>〉Sun〈<—
2009-10-28 21:41
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:2 
// 修改了多处

 
/*顺序表的基本操作*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 3
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR -1
#define OVERFLOW -2
 
typedef int Status;
typedef int ElemType;
 
typedef struct {
     ElemType *elem;
     int length;
     int listsize;
} SqList;
 
Status Initlist_Sq(SqList &L) /*初始化顺序表*/
{
    L.elem = (ElemType*)malloc(LIST_INIT_SIZE * sizeof(ElemType));
    if(L.elem == NULL)
        return(OVERFLOW);
    L.length = 0;
    L.listsize = LIST_INIT_SIZE;
    return OK;
}
 
void Destroylist(SqList &L) /*销毁顺序表*/
{
    free(L.elem);
    L.elem = NULL;
    L.length = L.listsize = 0;
}
 
void Clearlist_Sq(SqList &L)  /* 清空顺序表, 元素置0 */
{
    int i;
    for(i = 0; i < L.length; i++)
        L.elem[i] = 0;
}
 
Status Listempty_Sq(SqList &L) /*测试顺序表是否为空*/
{
    if(L.length == 0)
        return TRUE;
    else
        return FALSE;
}
 
Status ListInsert_Sq(SqList &L, int i, ElemType e) /*在第i个位置上插入一个元素*/
{
    ElemType *p;
    int j;
    if (i < 1 || i > L.length + 1)
        return ERROR;
    if (L.length + 1 > L.listsize) {
        p = (ElemType*)realloc(L.elem,(L.length + LISTINCREMENT)*sizeof(ElemType));
        if (p == NULL)
            exit(OVERFLOW);
        L.elem = p;
        L.listsize += LISTINCREMENT;
    }
    for (j = L.length; j >= i; --j )
        L.elem[j] = L.elem[j-1];
    L.elem[j]=e ;
    ++L.length ;
    return OK;
}
 
Status ListDelete_Sq(SqList &L, int i, int &e) /*删除第i个位置上的元素*/
{
    if(i < 1 || i > L.length)
        return ERROR;
    e = L.elem[i - 1];
    for(i; i < L.length; i++)
       L.elem[i - 1] = L.elem[i];
    --L.length;
    return OK;
}
 
int LocateElem_Sq(SqList &L, ElemType e) /*返回元素e在顺序表中的位置*/
{
    int i = 0;
    while(i < L.length) {
        if(e == L.elem[i++])
            return i;
    }
    return ERROR;
}
 
void Print_Sq(SqList L) /*输出顺序表*/
{
    int i;
    if (Listempty_Sq(L))
        printf("\nSequential List's length is %d. It's empty!", L.length);
    else
        printf("\nSequential List's length is %d. These element are : ", L.length);
    for(i = 0; i < L.length; i++)
        printf(" %d", L.elem[i]);
}
 
 
void menu()
{  printf("\n");
   printf("\n* * * * * * * * * * * * * * * * * * * * * * * * * *\n");
   printf("  1 ------- Print the Sequential List.\n");
   printf("  2 ------- Insert a data in the Sequential List.\n");
   printf("  3 ------- Delete a data in the Sequential List.\n");
   printf("  4 ------- Get a element's locationt in the SqList.\n");
   printf("  5 ------- Clear the Sequential List.\n");
   printf("  6 ------- Exit.\n");
   printf("* * * * * * * * * * * * * * * * * * * * * * * * * *\n");
   printf("\n");
}
 
void menuselect(SqList L)
{
    int k, i;
    ElemType e;
    do
    {
        menu();
        printf("Please choose: ");
        scanf("%d", &k);
        while((i = getchar()) != '\n' && i != EOF)
            continue;
        switch(k)
        {
            case 1:
                Print_Sq(L);
                break;
            case 2:
                printf("\nInput the location and data you want to Insert: ");
                scanf("%d,%d", &i, &e);
                if (ListInsert_Sq(L,i,e) == ERROR)
                    printf("Insert location is not correct!\n");
                break;
            case 3:
                printf("\nInput the location you want to delete data: ");
                scanf("%d", &i);
                if (ListDelete_Sq(L,i,e) == ERROR)
                    printf("Delete location is not exit!\n");
                else
                    printf("\nDelete data is %d.", e);
                break;
            case 4:
                printf("\nInput the data you want to find: ");
                scanf("%d", &e);
                if (LocateElem_Sq(L,e) != ERROR)
                    printf("\nData %d location in the Sequential List is %d.", e, LocateElem_Sq(L,e));
                else
                    printf("\nData %d is not in the Sequential List.\n", e);
                break;
            case 5:
                Clearlist_Sq(L);
                break;
            case 6:
                return;
                break;
            default :
                printf("Enter Error!\n");
                break;
        }
    }while(1);
}
 
int main(void)
{
    ElemType e;
    SqList La;
    int n, i;
    clrscr();
    Initlist_Sq(La);
    printf("Input a number of the element in the Sequential List (n<=%d):", LIST_INIT_SIZE);
    scanf("%d", &n);
    printf("Enter these elements:");
    for(i = 1; i <= n;i++)
    {
        scanf("%d", &e);
        ListInsert_Sq(La, i, e);
    }
    menuselect(La);
    getchar();
    return 0;
}

—>〉Sun〈<—
2009-10-28 23:09
快速回复:高手请进,帮帮小弟
数据加载中...
 
   



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

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