标题:小弟菜鸟一枚,请大家关照
只看楼主
blueskyghy
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2022-10-29
结帖率:0
已结贴  问题点数:20 回复次数:1 
小弟菜鸟一枚,请大家关照
#include<stdio.h>
//#include "ghy-list.h"
#define maxsize 100
#include "SqList.cpp"
typedef int Elem Type;
typedef struct
{
    Elem Type data[MaxSize] ;
    int length;
 } SqList;
 
 void InitList(SqList &L)
 {
     L.length=0;
 }
 void DestoryList(SqList L)
 {}
 int GetLength(Sqlist L)
 {
     return L.length;
 }
 int GetElem(SqList L,int i,ElemType &e)
 {
     if(i<1||i>L.length)
     return 0;
     else
     {
         e=L.data[i-1];
         return 1;
     }
 }
 int Locate(SqList L,ElemType x)
 {
     int i=0;
     while(i<L.length&&L.data[i]!=x)
     i++;
     if(i>=L.length) return(0);
     else return(i+1);
 }
 int  InsElem(SqList &L,ElemType x,int i)
 {
     int j;
     if(i<1||i>L.length+1)
     return 0;
     for(j=L.length;j>i;j--)
     L.data[j]=L.data[j-1]
     L.data[i-1]=x;
     L.length++;
     return 1;
      }
int DelElem(SqList&L,int i)
{
    int i;
    if(i<1||i>L.length)
    return 0;
    for(j=i;j<L.length;j++)
    L.data[j-1]=L.data[j];
    L.length--;
    return 1;
      }      
 void DispList(SqList L)
 {
     Int i;
     for(i=0;i<L.length;i++)
     printf("%d",L.data[i]);
     printf("\n");
 }
 void CreatList(SqList &L,ElemType a[],int n)
 {
     inti,k=0;
     for(i=0;i<n;i++)
     {
         L.data[k]=a[i];
         k++;
         
         
      }
      L.length=k;
 }
 int main()
 {
     int i;
     ElemType e;
     SqList L,L1;
     InitList(L);
     InsElem(L,1,1);
          InsElem(L,3,2);
          InsElem(L,1,3);
          InsElem(L,5,4);
          InsElem(L,4,5);
         InsElem(L,2,6);
         printf("测试1\n");
         printf("L:");DispList(L);
         printf("  长度;%d\n",GetLength(L));
         i=3;
         GetElem(L,i,e);
         printf("第%d个元素:%d\n",i,e) ;
         e=1;
         printf("  元素%d是第%d个元素\n",e,Locate(L,e));
         i=4;printf("  删除第%d个元素\n",i);
         DelElem(L,i);
         printf(" L: ");DispList(L);
         printf("测试2\n");
         int a[]={2,5,4,5,6,5,3,1};
         int n=sizeof(a)/sizeof(a[0]);
         printf("  整体创建L1\n");
         CreatList(L1,a,n);
         printf(" L1:");DispList(L1);
         int x=5;
         printf("   第一个值为%d元素的位置是%d\n",x,Locate(L1,x));
         printf("销毁L和L1\n");
         DestoryList(L);
         DestoryList(L1);
 }

//4    22    C:\Users\HP\Desktop\Untitled1.cpp    [Error] SqList.cpp: No such file or directory 这该怎么解决呀?
搜索更多相关主题的帖子: printf return data int length 
2022-10-29 09:45
不会游泳的虾
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:101
专家分:643
注 册:2021-7-1
得分:20 
供参考:
程序代码:
#include<stdio.h>
//#include "ghy-list.h"
//#include "SqList.cpp"
#define MaxSize 100    // maxsize   修改
typedef int ElemType;  // Elem Type;修改
typedef struct
{
    ElemType data[MaxSize];
    int      length;
} SqList;

void InitList(SqList& L)
{
    L.length = 0;
}
void DestoryList(SqList L)
{
    L.length = 0;          //修改
}
int GetLength(SqList L)  //(Sqlist L)修改
{
    return L.length;
}
int GetElem(SqList L, int i, ElemType& e)
{
    if (i<1 || i>L.length)
        return 0;
    else{
        e = L.data[i - 1];
        return 1;
    }
}
int Locate(SqList L, ElemType x)
{
    int i = 0;
    while (i < L.length && L.data[i] != x)
        i++;
    if (i >= L.length) return(0);
    else return(i + 1);
}
int  InsElem(SqList& L, ElemType x, int i)
{
    int j;
    if (i<1 || i>L.length + 1)
        return 0;
    for (j = L.length; j > i; j--)
        L.data[j] = L.data[j - 1];
    L.data[i - 1] = x;
    L.length++;
    return 1;
}
int DelElem(SqList& L, int i)
{
    int j;               //int i;修改
    if (i<1 || i>L.length)
        return 0;
    for (j = i; j < L.length; j++)
        L.data[j - 1] = L.data[j];
    L.length--;
    return 1;
}
void DispList(SqList L)
{
    int i;
    for (i = 0; i < L.length; i++)
        printf(" %d" + (i == 0), L.data[i]);
    printf("\n");
}
void CreatList(SqList& L, ElemType a[], int n)
{
    int i, k = 0;
    for (i = 0; i < n; i++)
    {
        L.data[k] = a[i];
        k++;
    }
    L.length = k;
}
int main()
{
    int i;
    ElemType e;
    SqList L, L1;
    InitList(L);
    InsElem(L, 1, 1);
    InsElem(L, 3, 2);
    InsElem(L, 1, 3);
    InsElem(L, 5, 4);
    InsElem(L, 4, 5);
    InsElem(L, 2, 6);
    printf("测试1\n");
    printf("L:"); DispList(L);
    printf("  长度;%d\n", GetLength(L));
    i = 3;
    GetElem(L, i, e);
    printf("第%d个元素:%d\n", i, e);
    e = 1;
    printf("  元素%d是第%d个元素\n", e, Locate(L, e));
    i = 4; printf("  删除第%d个元素\n", i);
    DelElem(L, i);
    printf(" L:"); DispList(L);
    printf("测试2\n");
    int a[] = { 2,5,4,5,6,5,3,1 };
    int n = sizeof(a) / sizeof(a[0]);
    printf("  整体创建L1\n");
    CreatList(L1, a, n);
    printf(" L1:"); DispList(L1);
    int x = 5;
    printf("   第一个值为%d元素的位置是%d\n", x, Locate(L1, x));
    printf("销毁L和L1\n");
    DestoryList(L);
    DestoryList(L1);
}
2022-10-31 09:27



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




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

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