重头来过吧
[fly]存在即是合理[/fly]
#include <stdio.h> #include <malloc.h> #include <string.h> #define MaxSize 50 typedef char ElemType; typedef struct { ElemType data[MaxSize]; int length; } SqList; void createList(SqList *&L,int a[],int n) { int i; L=(SqList *)malloc(sizeof(SqList)); for(i=0;i<n;i++) L->data[i]=a[i]; L->length=n; } int LocateElem(SqList *L, ElemType e) //按元素值查找 { int i=0; while (i<L->length && L->data[i]!=e) i++; //查找元素e if (i>=L->length) //未找到时返回0 return 0; else return i+1; //找到后返回其逻辑序号 } static bool ListDelete(SqList *&L,int i,int t) { int j; if(i<1 || i>L->length) return false; for(j=i;j<L->length-t;j++) L->data[j]=L->data[j+t]; L->length-=t; return true; } void DispList(SqList *L) //输出线性表 { int i; for (i=0;i<L->length;i++) printf("%d ",L->data[i]); printf("\n"); } int main() { int i,m,n,x,y,l,a[MaxSize]; SqList *L; bool b; printf("请输入顺序数组a:\n"); for(i=0;i<MaxSize;i++) { scanf("%d",a[i]); if(a[i]=='\n') break; } printf("x="); scanf("%d",&x); printf("y="); scanf("%d",&y); l=sizeof(a)/sizeof(int); createList(L,a,l); n=LocateElem(L,x); m=LocateElem(L,y); m=m-n-1; b=ListDelete(L,n,m); if(b==true) DispList(L); else printf("删除失败!\n"); return 0; }这是新代码 但是main函数中第一个for循环想要的功能在我的编译器上实现不了 (功能是希望输入回车时结束对数组的输入)不知道是不是程序的问题
#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERLOW -2 typedef int Status;源文件
#include <stdio.h> #include <stdlib.h> #include "define.h" /*线性表存储空间的初始化分配量*/ #define LIST_INIT_SIZE 100 /*线性表存储空间的分配增量*/ #define LISTINCREMENT 10 /*线性表结构体*/ typedef struct { int *elem; /*存储空间基址*/ int length; /*当前长度*/ int listsize; /*当前分配的存储容量*/ }SqList; /*初始化线性表or构造一个空表*/ Status InitList_Sq(SqList *L) { L->elem = (int *)malloc(sizeof(int)*LIST_INIT_SIZE); if(!L->elem) exit(OVERLOW); /*分配存储空间失败*/ L->length = 0; /*空表长度为0*/ L->listsize = LIST_INIT_SIZE; /*初始存储容量*/ return OK; } /*在顺序表L中第i个位置之前插入新的元素e*/ Status ListInsert_Sq(SqList *L, int pos, int e) { int *newbase, *p, *q; /*判断i的位置,合法值1<=pos<=ListLength_Sq(L)+1*/ if(pos<1 || pos>L->length+1) return ERROR; /*当前存储空间已满,增加分配*/ if(L->length >= L->listsize) { newbase = (int *)realloc(L->elem, sizeof(int)*(L->listsize + LISTINCREMENT)); if(!newbase) exit(OVERLOW); /*分配失败*/ L->elem = newbase; /*新基址*/ L->listsize += LISTINCREMENT; /*增加存储容量*/ } q = &(L->elem[pos - 1]); /*q为插入位置*/ for(p = &(L->elem[L->length - 1]); p >= q; --p) /*插入位置及以后的元素后移*/ *(p + 1) = *p; *q = e; /*插入元素*/ ++L->length; /*表长加1*/ return OK; } /*创建线性表*/ void CreatList_Sq(SqList *L, int length) { int elem; int i = 1; while(i <= length) { printf("Please input the %d number:",i); scanf("%d", &elem); ListInsert_Sq(L, i, elem); i++; } } /*打印线性表*/ void OutputList_Sq(SqList *L) { int i = 0; if(L->length == 0) puts("This is a empty List.\n"); while(i < L->length) { printf("%d ", L->elem[i++]); } printf("\n"); } /*线性表删除操作*/ Status ListDelete_Sq(SqList *L, int pos, int *e) { /*在顺序线性表L中删除第pos个元素,并用e返回该值*/ int *p, *q; /*pos的合法位置为1<=pos<=ListLength_Sq(L))*/ if(pos<1 || pos>L->length+1) return ERROR; /*i值不合法*/ p = &L->elem[pos - 1]; /*p为被删除元素的位置*/ *e = *p; /*被删除元素的值赋给e*/ q = L->elem + L->length-1; /*表尾元素的位置*/ for(++p; p<=q; ++p) *(p-1) = *p; /*被删除元素之后的元素左移*/ --L->length; return OK; } int main() { SqList *L; int e; if(InitList_Sq(L)) printf("初始化成功\n"); CreatList_Sq(L, 6); OutputList_Sq(L); ListDelete_Sq(L, 3, &e); printf("The Delete value is: %d\n", e); OutputList_Sq(L); return 0; }