| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 684 人关注过本帖
标题:代码应该如何写?
取消只看楼主 加入收藏
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
结帖率:38.67%
收藏
 问题点数:0 回复次数:3 
代码应该如何写?
#include "stdafx.h"
#include "stdio.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define OK 1
#define Error 0
#define OVERFLOW -1
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef int ElemType;
typedef int Status;
typedef struct
{
    ElemType *elem;
    int length;
    int listsize;
}SqList;
Status InitList_Sq(SqList &L)
{
          int i;
          L.elem = (ElemType * )malloc(LIST_INIT_SIZE*sizeof(ElemType));
          if (! L.elem) exit (OVERFLOW);
         
          for(i=1;i<=5;i++)
          {
             scanf("%d",&L.elem[i-1]);
          }
          L.length = 5;
          L.listsize = LIST_INIT_SIZE;
          return OK;
}

Status PrintList_Sq(SqList L)     
{
          int i;
          ElemType newbase;
          printf("Linklist yuansu is:\n ");
          for (i=1;i<=L.length;i++)
          {
               printf("%4d",L.elem[i-1]);
          }
            printf("\n");
          return OK;
}

Status ListInsert_Sq(SqList &L,int i,ElemType e)//插入函数应该如何编写代码?
{
        return OK;
        
}
Status ListDelete_Sq(SqList &L, int i ,ElemType &e)
{
       ElemType *p,*q;
       if ((i<1) || (i>L.length))
        return Error;
       p = & (L.elem[i-1]);  
       e = *p;
       q= L.elem + L.length-1;
       for (++p;p<=q;++p)
           *(p-1)= *p;
       --L.length;
       return OK;
}


int main(int argc, char* argv[])
{
   
    SqList L;
    ElemType e;
    int select,i;

    InitList_Sq(L);
    PrintList_Sq(L);
    ListDelete_Sq(L,i,e);
    printf("input i:\n");
    scanf("%d",&i);
    if (ListDelete_Sq(L,i,e)==Error)
        printf("i is error!\n");
    else

        PrintList_Sq(L);
    return 0;
}
 
搜索更多相关主题的帖子: 代码 
2009-09-24 14:00
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
是要在插入函数里里面实现插入功能的代码
2009-09-24 15:13
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
还是不能执行
#include "stdafx.h"
#include "stdio.h"  
#include "string.h"  
#include "stdlib.h"  
#define OK 1  
#define Error 0  
#define OVERFLOW -1  
#define LIST_INIT_SIZE 100  
#define LISTINCREMENT 10  
typedef int ElemType;  
typedef int Status;  
typedef struct  
{  
    ElemType *elem;  
    int length;  
    int listsize;  
}*SqList;  
 
Status InitList_Sq(SqList L)   
{  
          int i;  
          L->elem = (ElemType * )malloc(LIST_INIT_SIZE*sizeof(ElemType));   
          if (! L->elem) exit (OVERFLOW);  
            
          for(i=1;i<=5;i++)  
          {  
             scanf("%d",&L->elem[i-1]);  
          }  
          L->length = 5;  
          L->listsize = LIST_INIT_SIZE;  
          return OK;  
}  
 
Status PrintList_Sq(SqList L)      
{  
          int i;  
          //ElemType newbase;  
          printf("Linklist yuansu is:\n ");  
          for (i=1;i<=L->length;i++)  
          {  
               printf("%4d",L->elem[i-1]);  
          }  
            printf("\n");  
          return OK;  
}   
 
Status ListInsert_Sq(SqList L,int i,ElemType e)//这段代码还是不能执行
{  
        ElemType* newbase;  
        ElemType* p;  
        ElemType* q;  
 
        if (i < 1 || i > L->length + 1)  
        {  
            return Error;  
        }  
        if (L->length >= L->listsize)  
        {  
            newbase = (ElemType *) realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(ElemType));  
            if (!newbase)  
            {  
                exit(OVERFLOW);  
            }  
            L->elem = newbase;  
            L->listsize += LISTINCREMENT;  
        }  
        q = &(L->elem[i - 1]);  
        for (p = &(L->elem[L->length - 1]); p >= q; --p)  
        {  
            *(p + 1) = *p;  
        }  
        *q = e;  
        ++ L->length;
 
        return OK;  
         
}  
Status ListDelete_Sq(SqList L, int i ,ElemType *e)  
{  
       ElemType *p,*q;  
       if ((i<1) || (i>L->length))   
        return Error;  
       p = & (L->elem[i-1]);   
       *e = *p;   
       q= L->elem + L->length-1;   
       for (++p;p<=q;++p)  
           *(p-1)= *p;  
       --L->length;   
       return OK;  
}  
 
 
 
int main(int argc, char* argv[])
{
      
    SqList L;  
    ElemType e;  
    int i;  
 
    InitList_Sq(L);  
    PrintList_Sq(L);  
    ListInsert_Sq(L, i, e);  
    PrintList_Sq(L);
 
    ListDelete_Sq(L,i,e);  
    printf("input i:\n");  
    scanf("%d",&i);  
    if (ListDelete_Sq(L,i,&e)==Error)   
        printf("i is error!\n");   
    else  
 
        PrintList_Sq(L);  
     
    return 0;
}
2009-09-24 17:42
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
收藏
得分:0 
错误的提示:
D:\Program Files\Microsoft Visual Studio\MyProjects\2009_9_24\2009_9_24.cpp(36) : error C2371: 'SqList' : redefinition; different basic types
        D:\Program Files\Microsoft Visual Studio\MyProjects\2009_9_24\2009_9_24.cpp(20) : see declaration of 'SqList'
2009-09-24 22:45
快速回复:代码应该如何写?
数据加载中...
 
   



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

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