3、实验思路(实验内容、数据处理方法及实验步骤等): (一)顺序表的初始化 Init-sequeunlist 通过malloc函数申请分配存储空间 令L->last=0设置顺序表的长度为0,再返回L include<iostream> using namespace std; // - - - - 线性表的动态分配顺序存储结构 - - - - #define LIST_INIT_SIZE 100 //存储空间的初始分配量 #define INCREMENT 10 //存储空间的分配增量 #define ElemType char
typedef struct{ //线性表的存储结构 ElemType *elem; //存储空间基地址 int length; //当前长度 int listsize; //当前分配的存储容量 }SqList;
//线性表的初始化 int InitList_Sq(SqList *L){ L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));//动态分配存储空间 if (L->elem == NULL) return 0; L->length = 0; L->listsize = LIST_INIT_SIZE; return 0; } (二)顺序表的插入 Insert-sequenlist 判断该位置能插入后令L->data【i+1】=L->data【i】将数据插入到第i个位置并将插入第i个位置以后的结点往后移动 #include<iostream> using namespace std; // - - - - 线性表的动态分配顺序存储结构 - - - - #define LIST_INIT_SIZE 100 //存储空间的初始分配量 #define INCREMENT 10 //存储空间的分配增量 #define ElemType char
typedef struct{ //线性表的存储结构 ElemType *elem; //存储空间基地址 int length; //当前长度 int listsize; //当前分配的存储容量 }SqList;
//线性表的初始化 int InitList_Sq(SqList *L){ L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));//动态分配存储空间 if (L->elem == NULL) return 0; L->length = 0; L->listsize = LIST_INIT_SIZE; return 0; }
int ListInsert_Sq(SqList *L, int i, ElemType e){ //在顺序表L的第i个位置之前插入元素e if (i<1 || i>L->length + 1) cout << "i值不合法" << endl; if (L->length >= L->listsize){ //当前空间已满,增加分配 ElemType *newbase = (ElemType *)realloc(L->elem, (L->listsize + INCREMENT)*sizeof(ElemType)); if (newbase = NULL){ cout << "分配失败" << endl; return 0; } L->elem = newbase; //新基地址 L->listsize += INCREMENT;//增加存储容量 } ElemType *q = &(L->elem[i - 1]);//q为插入位置 for (ElemType *p = &(L->elem[L->length - 1]); p >= q; --p) *(p + 1) = *p; //插入位置及其后的元素右移
*q = e; //插入元素 +L->length; //表长加1 return 1; } (三)顺序表的删除 Delete-sequenlist 令L->data【i-1】=L->data【i】将结点i赋值给i-1再L->last- -让整个表减一 #include<iostream> using namespace std; // - - - - 线性表的动态分配顺序存储结构 - - - - #define LIST_INIT_SIZE 100 //存储空间的初始分配量 #define INCREMENT 10 //存储空间的分配增量 #define ElemType char
typedef struct{ //线性表的存储结构 ElemType *elem; //存储空间基地址 int length; //当前长度 int listsize; //当前分配的存储容量 }SqList;
//线性表的初始化 int InitList_Sq(SqList *L){ L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));//动态分配存储空间 if (L->elem == NULL) return 0; L->length = 0; L->listsize = LIST_INIT_SIZE; return 0; }
int ListDelete_Sq(SqList *L, int i, ElemType *e){ //顺序表中删除第i个元素,并用e返回其值 if (i<1 || i>L->length + 1){ //i的合法值为1到length cout << "i值不合法" << endl; return 0; } ElemType *p = &(L->elem[i - 1]); //p为被删除的元素的位置 *e = *p; //把p的值赋给e ElemType *q = L->elem + L->length - 1;//表尾元素的位置 // for (++p; p <= q; ++p) // *(p - 1) = *p; // --L->length; return 0; } (四)顺序表的遍历 Print-sequenlist 通过for函数来输出顺序表中的数据 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<malloc.h> #define list 10 #define max 10
typedef int Elemtype; typedef struct sqlist *List; struct sqlist{ Elemtype *elem; int length; int listsize; };
void initlist(List L){ L->elem=(Elemtype *)malloc(list*sizeof(Elemtype)); L->length=0; L->listsize=max; }
void creat(List L){ initlist(L); int i,n; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&L->elem[i]); L->length++; if(L->length==L->listsize) { int *new_elem = (Elemtype *)malloc((L->listsize+list)*sizeof(Elemtype)); memcpy(new_elem,L->elem,L->length); L->elem = new_elem; L->listsize+=list; } } }
void print(List L) { for(int i=0; i<L->length;i++) { if(i==L->length-1) printf("%d",L->elem[i]); else printf("%d ",L->elem[i]); } }
int main(){ List L = (List)malloc(sizeof(struct sqlist)); initlist(L); creat(L); print(L); return 0; } (五)顺序表的查找 Search-sequenlist 在for函数中使用if函数一个个进行数据比对 #include <stdio.h> #define MAXSIZE 100
typedef int KeyType; typedef char InfoType[10]; typedef struct { KeyType Key; InfoType data; }NodeType; typedef NodeType SeqList[MAXSIZE];
int Search(SeqList R,int n,KeyType k) { int i; i = n; while(R[i].Key!=k)//直接从表尾进行查找,不用每次循环都判断i会不会大于n { i--; } return i;
}
int main() { SeqList R; int n = 10,i; KeyType k = 9; printf("输入序列内容(十个不同的数字):"); for(i=0;i<10;i++) { scanf("%d",&R[i].Key); } printf("序列为:"); for(i=0;i<n;i++) { printf("%d,",R[i].Key); } printf("\n请输入需要查找的关键字为:"); scanf("%d",&k); printf("需要搜索的关键字为%d",k); if((i =Search(R,n,k))!=-1) { printf("\n%d在序列的第%d位置",k,i+1); } } |