/*
* SqList.h
*
* Created on: Oct 6, 2015
* Author: chris
*/
#ifndef SQLIST_H_
#define SQLIST_H_
#include<iostream>
#include<cstdlib>
typedef int ElemType;
enum {SQLISTINITSIZE = 10, SQLISTINCSIZE = 10};
struct SqList{
ElemType* data;
size_t len;
size_t siz;
};
bool SqListInit(SqList &);
void SqListDestroy(SqList &);
inline void SqListClear(SqList & SqL) {
SqL.len = 0;
}
inline bool SqListIsEmpty(SqList SqL) {
return SqL.len == 0;
}
inline size_t SqListLength(SqList SqL) {
return SqL.len;
}
inline size_t SqListSize(SqList SqL) {
return SqL.siz;
}
bool SqListInsert(SqList&, size_t, ElemType);
bool SqListDelete(SqList&, size_t, ElemType&);
inline bool SqListAppend(SqList& SqL, ElemType e) {
return SqListInsert(SqL, SqL.len, e);
}
size_t SqListLocate(SqList, ElemType);
void SqListDisplay(SqList);
void SqListSort(SqList & SqL, bool desc = false,
size_t left = 0, size_t right = -1);
#endif /* SQLIST_H_ */
/*
* SqList.cpp
*
* Created on: Oct 6, 2015
* Author: chris
*/
#include "SqList.h"
#include <iostream>
using namespace std;
bool SqListInit(SqList & SqL)
{
SqL.data = new ElemType[SQLISTINITSIZE];
if(!SqL.data) return false;
SqL.siz = SQLISTINITSIZE;
SqL.len = 0;
return true;
}
void SqListDestroy(SqList & SqL)
{
delete SqL.data;
SqL.len = 0;
SqL.siz = 0;
}
bool SqListInsert(SqList& SqL, size_t pos, ElemType e)
{
if(pos > SqL.len) {
cerr << "Index out of bound." << endl;
return false;
}
else if(SqL.len == SqL.siz) {
if (SQLISTINCSIZE <= 0) {
cerr << "Fatal: SQLISTINCSIZE <= 0 " << endl;
exit(EXIT_FAILURE);
}
ElemType* temp = new ElemType[SqL.len + SQLISTINCSIZE];
if(!temp)
return false;
else{
for(size_t i = 0; i < SqL.len; ++i) {
temp[i] = SqL.data[i];
}
delete SqL.data;
SqL.data = temp;
SqL.siz = SqL.len + SQLISTINCSIZE;
}
} //realloc.
if(SqL.len > 0)
for(size_t i = SqL.len - 1; i >= pos; --i)
SqL.data[i+1] = SqL.data[i];
SqL.data[pos] = e;
++SqL.len;
return true;
}
bool SqListDelete(SqList & SqL, size_t pos, ElemType & e)
{
if(pos >= SqL.len) {
cerr << "Index out of bound." << endl;
return false;
}
e = SqL.data[pos];
for(size_t i = pos; i < SqL.len-1; ++i)
SqL.data[i] = SqL.data[i+1];
--SqL.len;
return true;
}
size_t SqListLocate(SqList SqL, ElemType e)
{
for(size_t i = 0; i < SqL.len; ++i){
if(SqL.data[i] == e)
return i;
}
return -1;
}
void SqListDisplay(SqList SqL)
{
for(size_t i = 0; i < SqL.len; ++i)
cout << SqL.data[i] << " ";
cout << endl;
}
void SqListSort(SqList & SqL, bool desc, size_t left, size_t right)
{
if(SqL.len == 0) return;
if(right == -1)
right = SqL.len-1;
if(left >= right) return;
size_t mid = (left+right)/2;
ElemType tmp = SqL.data[mid];
SqL.data[mid] = SqL.data[0];
size_t i = left, j = right;
while(i < j) {
while(i < j && (!desc && tmp <= SqL.data[j] ||
desc && tmp >= SqL.data[j]) ) --j;
SqL.data[i] = SqL.data[j];
while(i < j && (!desc && SqL.data[i] <= tmp ||
desc && SqL.data[i] >= tmp) ) ++i;
SqL.data[j] = SqL.data[i];
}
SqL.data[i] = tmp;
if(left+1 < i) SqListSort(SqL, desc, left, i-1);
if(i+1 < right) SqListSort(SqL, desc, i+1, right);
return;
}
/*
* Main.cpp
*
* Created on: Oct 6, 2015
* Author: chris
*/
#include<iostream>
#include "SqList.h"
using namespace std;
int main(void) {
cout << "Program for implementing SqList." << endl;
SqList sql;
if(!SqListInit(sql))
exit(EXIT_FAILURE);
SqListAppend(sql, 4);
SqListAppend(sql, 2);
SqListAppend(sql, 3);
SqListAppend(sql, 5);
SqListAppend(sql, 1);
bool running = true;
while(running) {
cout << "function: 1:display; 2: insert; 3: delete; 4: locate;" << endl
<< "5: clear; 6: status; 7: sort; 8: exit" << endl;
int ans;
cin >> ans;
switch(ans) {
case 1:
SqListDisplay(sql);
break;
case 2:
{
size_t pos;
ElemType e;
cout << "pos: "; cin >> pos;
cout << "elem: "; cin >> e;
if(SqListInsert(sql, pos, e))
cout << "Insert success." << endl;
else
cout << "Insert failure." << endl;
}
break;
case 3:
{
size_t pos;
ElemType e;
cout << "pos: "; cin >> pos;
if(SqListDelete(sql, pos, e)) {
cout << "Delete success." << endl;
cout << "Elem deleted: " << e << endl;
}else {
cout << "Delete failure." << endl;
}
}
break;
case 4:
{
ElemType e;
cout << "elem: "; cin >> e;
size_t pos;
if((pos = SqListLocate(sql, e)) != -1) {
cout << "Locate success." << endl;
cout << "Elem located at: " << pos << endl;
}else
cout << "Locate failure." << endl;
break;
}
case 5:
SqListClear(sql);
break;
case 6:
cout << "length: " << sql.len << endl;
cout << "size: " << sql.siz << endl;
break;
case 7:
{
bool desc;
cout << "asc(0) or desc(1): "; cin >> desc;
SqListSort(sql, desc);
cout << "sort done." << endl;
}
break;
case 8:
running = false;
break;
}//switch
}//while
SqListDestroy(sql);
return 0;
}