第3周实践项目1 顺序表的基本运算
生活随笔
收集整理的這篇文章主要介紹了
第3周实践项目1 顺序表的基本运算
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*copyright (t) 2017,煙臺大學計算機學院
*All rights reserved.
*文件名稱:1.cpp
*作者:邵雪源
*完成日期:2017年9月19日
*版本號:v1.0
*問題描述:用函數實現順序表的10個基本運算(見下),并用main函數完成調試工作
*輸入描述:無
*程序輸出:無
*/
#include <iostream>
#include <malloc.h>
#include <cstdio>
#define Maxsize 100
using namespace std;
typedef int Elemtype; //自定義數據類型
typedef struct list
{Elemtype data[Maxsize]; //存順序表元素int length; //存順序表長度
} Sqlist;
//實現算法的自定義函數以及其他必要的自定義函數
void CreateList(Sqlist *&l,Elemtype a[],int n) //由a中的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;// if(ListEmpty(l))// return;for(i=0;i<l->length;i++)printf("%d ",l->data[i]);printf("\n");
}
bool ListEmpty(Sqlist *l) //布爾型函數判斷順序表是否為空表
{return (l->length==0); //空返回0 非空返回1
}
int ListLength(Sqlist *l) //求順序表長度
{return (l->length);
}
bool GetElem(Sqlist *l,int i,Elemtype &e) //求順序表中某個數據元素值
{if(i<1 || i>l->length) //i為邏輯序號,注意參數錯誤的情況return false; //參數錯誤返回falsee=l->data[i-1];return true; //找到指定下標元素返回true
}
int LocateElem(Sqlist *l,Elemtype e) //按元素值查找順序表中元素
{int i=0;while(i<l->length && l->data[i]!=e) //e不是要查找的元素,繼續往下遍歷i++;if(i>=l->length)return 0; //遍歷一遍未找到返回falseelsereturn i+1; //否則找到返回true
}
bool ListInsert(Sqlist *&l,int i,Elemtype e) //插入數據元素
{int j;if(i<1 || i>l->length+1 || l->length>=Maxsize)//i為邏輯序號,注意參數錯誤的情況return false; //參數錯誤返回falsei--; //順序表邏輯序號轉化為物理序號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) //i為邏輯序號,注意參數錯誤的情況return false; //參數錯誤返回falsei--; //順序表邏輯序號轉化為物理序號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; //置空線性表,其長度為0
}
void DestroyList(Sqlist *&l) //銷毀順序表
{free(l);
}//定義用于驅動測試的main函數
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);elseprintf("第3個元素超出范圍!\n");if(GetElem(l,15,e)) //測試不在范圍內的情形printf("找到了第15個元素值為:%d\n",e);elseprintf("第15個元素超出范圍!\n");if((loc=LocateElem(l,8))>0) //測試能找到的情形printf("找到了,值為8的元素是第 %d 個\n",loc);elseprintf("值為8的元素木有找到!\n");if((loc=LocateElem(l,17))>0) //測試不能找到的情形printf("找到了,值為17的元素是第 %d 個\n",loc);elseprintf("值為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;
} //移動奇數
總結
以上是生活随笔為你收集整理的第3周实践项目1 顺序表的基本运算的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第三周项目4(2)-顺序表应用 将所有奇
- 下一篇: 第3周实践项目7 删除链表元素最大值