数据结构第三周项目二
来源:互联网 发布:hex editor怎么修改mac 编辑:程序博客网 时间:2024/05/20 18:55
- #define Maxsize 100
- typedef int Elemtype;
typedef struct list { Elemtype data[Maxsize]; int length; } Sqlist; void CreateList(Sqlist *&l,Elemtype a[],int n); void DispList(Sqlist *l); bool ListEmpty(Sqlist *l); int ListLength(Sqlist *l); bool GetElem(Sqlist *l,int i,Elemtype &e); int LocateElem(Sqlist *l,Elemtype e); bool ListInsert(Sqlist *&l,int i,Elemtype e); bool ListDelete(Sqlist *&l,int i,Elemtype &e); void InitList(Sqlist *&l); void DestroyList(Sqlist *&l); list.cpp:
- #include <stdio.h>
- #include <malloc.h>
- #include "list.h"
- void CreateList(Sqlist *&l,Elemtype a[],int n)
- {
- int i;
- l=(Sqlist *)malloc(sizeof(Sqlist));
- for(i=0;i<n;i++)
- l->data[i]=a[i];
- l->length=n;
- }
- void DispList(Sqlist *l)
- {
- int i;
-
-
- for(i=0;i<l->length;i++)
- printf("%d ",l->data[i]);
- printf("\n");
- }
- bool ListEmpty(Sqlist *l)
- {
- return (l->length==0);
- }
- int ListLength(Sqlist *l)
- {
- return (l->length);
- }
- bool GetElem(Sqlist *l,int i,Elemtype &e)
- {
- if(i<1 || i>l->length)
- return false;
- e=l->data[i-1];
- return true;
- }
- int LocateElem(Sqlist *l,Elemtype e)
- {
- int i=0;
- while(i<l->length && l->data[i]!=e)
- i++;
- if(i>=l->length)
- return 0;
- else
- return i+1;
- }
- bool ListInsert(Sqlist *&l,int i,Elemtype e)
- {
- int j;
- if(i<1 || i>l->length+1 || l->length>=Maxsize)
- return false;
- i--;
- for(j=l->length-1;j>i;j--)
- l->data[j]=l->data[j-1];
- l->data[i]=e;
- l->length++;
- return true;
- }
- bool ListDelete(Sqlist *&l,int i,Elemtype &e)
- {
- int j;
- if(i<1 || i>l->length+1)
- return false;
- i--;
- e=l->data[i];
- for(j=i;j<l->length-1;j++)
- l->data[j]=l->data[j+1];
- l->length--;
- return true;
- }
- void InitList(Sqlist *&l)
- {
- l=(Sqlist *)malloc(sizeof(Sqlist));
- l->length=0;
- }
- void DestroyList(Sqlist *&l)
- {
- free(l);
- }
main:
- #include <iostream>
- #include <malloc.h>
- #include <cstdio>
- #include "list.h"
- using namespace std;
-
- int main()
- {
- Sqlist *l;
- Elemtype a[10]={1,2,3,4,5,6,7,8,9,10};
- Elemtype b[5]={5,8,7,4,6};
- Elemtype e;
- int loc;
- CreateList(l,a,10);
- cout<<"建立的顺序表中各元素为:"<<endl;
- DispList(l);
- cout<<"此线性表长度为:"<<ListLength(l)<<endl;
- if(GetElem(l,3,e))
- printf("找到了第3个元素值为:%d\n", e);
- else
- printf("第3个元素超出范围!\n");
-
- if(GetElem(l,15,e))
- printf("找到了第15个元素值为:%d\n",e);
- else
- printf("第15个元素超出范围!\n");
-
- if((loc=LocateElem(l,8))>0)
- printf("找到了,值为8的元素是第 %d 个\n",loc);
- else
- printf("值为8的元素木有找到!\n");
-
- if((loc=LocateElem(l,17))>0)
- printf("找到了,值为17的元素是第 %d 个\n",loc);
- else
- printf("值为17的元素木有找到!\n");
- DestroyList(l);
- cout<<"此顺序表被销毁"<<endl;
- cout<<endl;
-
- CreateList(l,b,5);
- cout<<"建立的顺序表中各元素为:"<<endl;
- DispList(l);
- cout<<"此线性表长度为:"<<ListLength(l)<<endl<<endl;
-
- ListInsert(l,4,2);
- cout<<"在4位置插入元素2后的顺序表为:"<<endl;
- DispList(l);
-
- ListDelete(l,2,e);
- cout<<"在2位置删除元素8后的顺序表为:"<<endl;
- DispList(l);
-
- DestroyList(l);
- cout<<"此顺序表被销毁"<<endl;
- cout<<endl;
-
- return 0;
- }