#include<iostream>
#include<stdio.h>
#define LIST_INIT_SIZE 100
#define LIST_INCREMENT 10
using namespace std;
struct Sqlist{
long *elem, *newlist;
int Length;
int listsize;
};
void Error(char *s)
{
cout << s << endl;
exit(1);
}
void InitList_Sq(Sqlist &L) //創建含有若干個資料元素的順序表
{
L.elem = new long[LIST_INIT_SIZE];
if (!L.elem)
Error("Overflow!");
L.Length = 0;
L.listsize = LIST_INIT_SIZE;
}
void Create_Sq(Sqlist *L)
{
int i, num;
cout << "請輸入順序表元素個數:";
cin >> num;
cout << "請輸入資料元素:";
for (i = 0; i<num; i++)
{
cin >> L->elem[i];
L->Length++;
}
cout << "創建順序表成功:!" << endl;;
}
void Increment(Sqlist &L) //為順序表擴展LIST_INCREMEN個資料元素的空間
{
L.newlist = new long[L.listsize + LIST_INCREMENT];//增加LIST_INCREMENT個存盤空間
if (!L.newlist)
Error("Overflow!");//存盤分配失敗
for (int i = 0; i < L.Length; i++) //騰挪原空間中的資料元素到新的資料空間中
{
L.newlist[i] = L.elem[i];
}//釋放元素所占用的原空間
L.elem = L.newlist;//移交空間首地址;//移交空間首地址
delete[] L.elem;
L.listsize += LIST_INCREMENT; //修改當前順序表的最大空間
}
void ListInsert_Sq(Sqlist &L, int i,int e)
//在順序表L中第i個位置前插入元素e;若插入位置不合理則給出相關資訊并退出運行,
//i的合理范圍是1<=i<=L.length+1
{
if ((i<1) || (i>L.Length + 1)) //插入元素的引數不合理
Error("Position Error!");
if (L.Length >= LIST_INIT_SIZE) //若當前存盤空間已滿,則增加空間
Increment(L); //增加空間函式
long *q = &(L.elem[i - 1]); //令指標q指向插入位置
long *p = &(L.elem[L.Length - 1]);
for (p; p >= q; p--) //以此向后移動元素
{
*(p + 1) = *p;
}
*q = e; //在L的第i個位置插入元素e
L.Length++; //修改當前順序表的長度
}
void ListDelete_Sq(Sqlist &L, int i, int &e)
//洗掉順序表中第i個元素并用e回傳其值
{
if (i<1 || i>L.Length) //洗掉元素的引數不合理
Error("Position Error!");
e = L.elem[i - 1]; //將待洗掉的元素的值賦給e
long *p = &(L.elem[i - 1]); //指向待刪處元素的位置
for (++p; p <= (L.elem + L.Length - 1); p++)//以此向前移動元素
{
*(p - 1) = *p;
}
L.Length--;
cout << "洗掉的元素是:";
cout << e << endl;
}//修改L的長度
int LocatElem_Sq(Sqlist L, int e)
{
int j = 1;
long *q = L.elem;
while ((j <= L.Length) && (*q!= e))
{
j++;
q++;
}
if (j < L.Length)
return j;
else
return 0;
}
void getElem_Sq(Sqlist L, int i, int &e) //輸出順序表中要操作的那個元素
{
if ((i<1) || (i>L.Length)) //判斷輸出要求引數是否合理
Error("Position Error!");
e = L.elem[i - 1]; //用e回傳順序表中第i個元素值
cout << e << endl;
}
void printElem_Sq(Sqlist L) //輸出順序表中現有的所有元素
{
cout << " 順序表中的元素如下:" << endl;
int i;
for (i = 0; i< L.Length; i++)
{
cout <<" "<< L.elem[i];
}
cout << endl;
}
int main()
{
Sqlist L;
int e,n,number;
while (1)
{
cout << " 1、創建資訊表" << endl;
cout << " 2、插入元素" << endl;
cout << " 3、查詢元素" << endl;
cout << " 4、洗掉元素" << endl;
cout << " 5、退出程式" << endl;
cout << " 請選擇所要執行的操作:";
cin >> n;
switch (n)
{
case 1:
InitList_Sq(L);
Create_Sq(&L);
printElem_Sq(L);
break;
case 2:
cout << "請輸入插入的位置和元素:";
cin >> number >> e;
while (e != 0)
{
ListInsert_Sq(L, number, e);
printElem_Sq(L);
cout << "請輸入插入的位置和元素:";
cin >> number >> e;
}
break;
case 3:
cout << "請輸入查找的元素:";
cin >> e;
while (e!=0)
{
LocatElem_Sq(L, e);
cout << "該元素所在順序表的位置位置是:" << LocatElem_Sq(L, e) << endl;
cout << "該元素是:";
getElem_Sq(L, LocatElem_Sq(L, e), e);
cout << "請輸入查找的元素:";
cin >> e;
}
break;
case 4:
cout << "請輸入要洗掉的位置 :";
cin >> number;
ListDelete_Sq(L, number, e);
printElem_Sq(L);
break;//程式結束
case 5:
exit(1);
break;//程式結束
default:
cout << "輸入錯誤,請重新輸入!!!!!" << endl;
continue;
}
}
return 0;
}



uj5u.com熱心網友回復:
代碼檔案用cpp后綴uj5u.com熱心網友回復:
就改了這個就可以了嗎
uj5u.com熱心網友回復:
哪抄的代碼?樓主不知道cin、cout這些是標準的C++寫法嗎?
uj5u.com熱心網友回復:
呃是復制粘貼的⊙_⊙我不懂
uj5u.com熱心網友回復:
就改了這個就可以了嗎
哪抄的代碼?樓主不知道cin、cout這些是標準的C++寫法嗎?
呃是復制粘貼的⊙_⊙我不懂
如果你是學C的,請修改代碼或重新找一個。如果你是學C++的,我不知道該說什么了!
uj5u.com熱心網友回復:
就改了這個就可以了嗎
哪抄的代碼?樓主不知道cin、cout這些是標準的C++寫法嗎?
呃是復制粘貼的⊙_⊙我不懂
如果你是學C的,請修改代碼或重新找一個。如果你是學C++的,我不知道該說什么了!
噢噢⊙_⊙好滴

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/98888.html
標籤:C語言
上一篇:關于列印的排版問題
