using ElementType = int;
#define MaxSize 100struct SeqList{
ElementType data[MaxSize];
int length; /*the size of the seqlist */
};
using PtrList = SeqList*;
using Position = int;
using Length = int;
void InitList(PtrList list_1);
bool IsEmpty(PtrList list_1);
void ClearList(PtrList list_1);
ElementType& GetElem(int position, ElementType& value, PtrList list_1);/*use value to storage the result */
Position FindElement(ElementType value, PtrList list_1);/*if found ,return the position to value */void InsertTail(Position p, ElementType value, PtrList list_1);/*if can, insert the value into list using the given positon*/void InsertHead(Position p, ElementType value, PtrList list_1);
void DeleteElement(Position p, PtrList list_1);
Length ListLength(PtrList list_1);
void PrintList(PtrList list_1);
函数体
#include "标头.h"#include <iostream>void InitList(PtrList list_1){
list_1->length = 0;
std::cout << "Initialize successfully " << std::endl;
}
bool IsEmpty(PtrList list_1){/*if the list is empty,return 1*/return list_1->length == 0;
}
void ClearList(PtrList list_1){
list_1->length = 0;
std::cout << "Clear successfully " << std::endl;
}
ElementType& GetElem(int position, ElementType& value, PtrList list_1){
if (position<0 || position>list_1->length - 1)
exit(0);/*return -1 represent failure */
value = list_1->data[position];
return value;
}
Position FindElement(ElementType value, PtrList list_1){
/*return the first occurrence of value */for (int i = 0; i < list_1->length;++i)
if (list_1->data[i] == value)
return i;
/*if not found,retun -1*/return -1;
}
void InsertTail(Position p, ElementType value, PtrList list_1){
if (p<0 || p>list_1->length - 1){
std::cout << "Wrong position " << std::endl;
return;
}
if (list_1->length >=MaxSize){
std::cout << "The list is full " << std::endl;
return;
}
for (int i = list_1->length; i >=p+2; --i)
list_1->data[i] = list_1->data[i - 1];
list_1->data[p + 1] = value;
++list_1->length;
}
void InsertHead(Position p, ElementType value, PtrList list_1){
if (p<0 || p>list_1->length - 1){
std::cout << "Wrong position " << std::endl;
return;
}
if (list_1->length >= MaxSize){
std::cout << "The list is full " << std::endl;
return;
}
for (int i = list_1->length-1; i >=p; --i)
list_1->data[i+1] = list_1->data[i];
list_1->data[p] = value;
++list_1->length;
}
void DeleteElement(Position p, PtrList list_1){
if (p<0 || p>list_1->length - 1){
std::cout << "Wrong position " << std::endl;
return;
}
if (IsEmpty(list_1)){
std::cout << "The list is empty " << std::endl;
return;
}
for (int i = p; i <=list_1->length-2; ++i)
list_1->data[i] = list_1->data[i + 1];
--list_1->length;
}
Length ListLength(PtrList list_1){
return list_1->length;
}
void PrintList(PtrList list_1){
for (int i = 0; i<list_1->length; ++i)
std::cout << list_1->data[i] << " ";
std::cout << '\n';
}
void CreateList(PtrList list_1){
list_1->length = 5;
for (int i = 0; i < list_1->length; ++i)
list_1->data[i] =i+1;
}
bool FindValue(ElementType value, PtrList list_1){
for (int i = 0; i < list_1->length; ++i){
if (list_1->data[i] == value)
returntrue;
elsecontinue;
}
returnfalse;
}
void Or(PtrList list_1, PtrList list_2){
for (int i = 0; i < list_2->length; ++i){
if (!FindValue(list_2->data[i], list_1))
InsertHead(1, list_2->data[i], list_1);
elsecontinue;
}
std::cout << "Over \n";
}
void And(PtrList list_1, PtrList list_2, PtrList list_result){
for (int i = 0; i < list_1->length; ++i){
if (FindValue(list_1->data[i], list_2))
InsertTail(1, list_1->data[i], list_result);
elsecontinue;
}
std::cout << "Over \n";
}
简单测试
int main(){
/*PtrList list = new SeqList;
CreateList(list);if (IsEmpty(list))
std::cout << "Empty" << std::endl;else std::cout << "Not empty " << std::endl; std::cout << "Print all the elements :";
PrintList(list); std::cout << "Insert 88 after the position of 0 " << std::endl;
InsertTail(0, 88, list); std::cout << "Print all the elements :";
PrintList(list); std::cout << "Insert 88 before the position of 0 " << std::endl;
InsertHead(0, 99, list); std::cout << "Print all the elements :";
PrintList(list); std::cout << "Delete the element of 0 position " << std::endl;
DeleteElement(0, list); std::cout << "Print all the elements :";
PrintList(list); std::cout << "Get the element of 0 position " << std::endl;
int value=GetElem(0, value, list); std::cout << value << std::endl; std::cout << "Find if 88 is in the list " << std::endl; std::cout << FindElement(88, list) << std::endl; std::cout << "Clear list \n";
ClearList(list); std::cout << "Check if the list is empty \n";if (IsEmpty(list))
std::cout << "Empty" << std::endl;else std::cout << "Not empty " << std::endl;
*/
PtrList list_1 = new SeqList;
PtrList list_2 = new SeqList;
PtrList list_result = new SeqList;
CreateList(list_1); std::cout << "Print all the elements int the list_1:";
PrintList(list_1);
CreateList(list_2); std::cout << "Print all the elements in the list_2:";
PrintList(list_2);Or(list_1, list_2); std::cout << "Print all the elements :";
PrintList(list_1);
CreateList(list_result);And(list_1, list_2, list_result); std::cout << "Print all the elements :";
PrintList(list_result);
}
头文件:using ElementType = int;#define MaxSize 100struct SeqList{ ElementType data[MaxSize]; int length; /*the size of the seqlist */};using PtrList = SeqList*;using Position = int;using Le