小實驗1
sq_list
#include
#include
#include
using namespace std;
const int SqList_Init_Size = 10; //初始化線性表大小
const int SqListIncrement = 5; //動態擴容大小
const int Size = 5; //陣列大小
typedef struct{ //順序表結構
char *data;
int size;
int length;
}SqList;
bool InitList(SqList *L);
bool GetList(SqList *L,int *cont);
bool InsertList(SqList *L,int insloc);
bool DeleteList(SqList *L,int delloc);
bool ChangeList(SqList *L,int chanloc);
bool SearchList(SqList L,int searloc);
void ShowList(SqList L);
void ShowMenu();
int main()
{
cout.width(45);
cout << “System(Sequential_List)\n”;
SqList L;
InitList(&L); //初始化順序表
cout.width(44);
cout << “Initial List Success.\n”;
ShowMenu();
char oops;
while(1){
cout.width(20);
cout << "Enter a number that you need(# to quit): “; //使用者操作介面
cin >> oops;
switch(oops)
{
case ‘1’: int cont;
GetList(&L,&cont);
cout << cont << " have readed.\n”; break;
case ‘2’: int insloc;
cout.width(15);
cout << "Enter insert locate: ";
cin >> insloc;
InsertList(&L,insloc);
cout << “Insert Done.\n”; break;
case ‘3’: int delloc;
cout << "Enter delete locate: ";
cin >> delloc;
DeleteList(&L,delloc);
cout << “Delete Done.\n”; break;
case ‘4’: int chanloc;
cout << "Enter change locate: ";
cin >> chanloc;
ChangeList(&L,chanloc);
cout << “CHange Done.\n”; break;
case ‘5’: int searloc;
cout << "Enter a search locate: ";
cin >> searloc;
SearchList(L,searloc); break;
case ‘6’: ShowList(L);
cout << “This is your List.\n”; break;
default : cout << “Done.\n”; return 0;
}
}
return 0;
}
bool InitList(SqList *L)
{
L->data = new char[Size]; //動態開闢資料空間
if(!L->data) //開空間失敗
{
cerr << “Initial Error. Err1\n”;
return false;
}
L->size = SqList_Init_Size; //順序表大小賦初值
L->length = 0; //初始化長度0
return true;
}
void ShowMenu()
{
cout << “Press 1 to Get Item.\n”;
cout << “Press 2 to Insert.\n”;
cout << “Press 3 to Delete.\n”;
cout << “Press 4 to Change.\n”;
cout << “Press 5 to Search.\n”;
cout << “Press 6 to Show List.\n”;
}
bool GetList(SqList *L,int *cont)
{
int i = 0;
cin >> L->data[i];
while(L->data[i] != ‘0’){
i++;
cin >> L->data[i];
}
*cont = i+1;
L->length = *cont;
if(L->length >= L->size)
{
L->size += SqListIncrement;
L->length = L->size;
}
return true;
}
void ShowList(SqList L)
{
if(!L.length) //判斷是否為空表
{
cerr << “Empty List.\n”;
exit(EXIT_FAILURE);
}
int i;
for(i = 0; i<L.length; i++){ //輸出表裡內容
cout << "The List is: " << L.data[i] << endl;
}
}
bool InsertList(SqList *L,int insloc)
{
SqList temp;
temp.data = new char[Size];
int k, i = 0;
if(L->length == L->size) //判斷是否表滿
{
cerr << “Run out. Err3.\n”;
return false;
}
if(insloc < 1 || insloc > L->length) //判斷插入位置是否合法
{
cerr << “Run out. Err4.\n”;
return false;
}
cout << "Enter the new item: "; //插入資料
cin >> temp.data[i];
if(insloc < L->length)
{
for(k = L->length-1; k >= insloc-1; k–){ //從表尾到插入位置後一位以此後移
L->data[k+1] = L->data[k];
}
L->data[insloc-1] = *(temp.data); //插入資料
}
L->length++;
return true;
}
bool DeleteList(SqList *L,int delloc)
{
int k;
if(L->length == 0) //判斷是否空表
{
cerr << “It’s Empty. Err5.\n”;
return false;
}
if(delloc < 1 || delloc > L->length) //判斷刪除位置是否合法
{
cerr << “Run out. Err6.\n”;
return false;
}
if(delloc < L->length)
{
for(k = delloc; k < L->length; k++){ //從刪除位置起到表尾依次前移
L->data[k-1] = L->data[k];
}
}
L->length–;
return true;
}
bool ChangeList(SqList *L,int chanloc)
{
int k, i = 0;
if(L->length == 0) //判斷是否空表
{
cerr << “It’s Empty. Err7.\n”;
return false;
}
if(chanloc < 1 || chanloc > L->length) //判斷修改位置是否合法
{
cerr << “Run out. Err8.\n”;
return false;
}
SqList temp; //建立臨時儲存結構
temp.data = new char[Size]; //臨時結構開空間
cout << "Enter the item to change: "; //輸入資料
cin >> temp.data[i];
L->data[chanloc-1] = *(temp.data); //修改原資料
return true;
}
bool SearchList(SqList L,int searloc)
{
int k;
if(L.length == 0) //判斷是否空表
{
cerr << “It’s Empty. Err9.\n”;
return false;
}
if(searloc < 1 || searloc > L.length) //判斷查詢位置是否合法
{
cerr << “Run out. Err10.\n”;
return false;
}
cout << "Your search item: " << L.data[searloc-1] << endl; //輸出查詢資料
return true;
}
link_list
#include
#include
#include
using namespace std;
const int MaxSize=5;
typedef struct LNode{
int data;
struct LNode *next;
}LNode,*LinkList;
void ShowMenu();
void ShowList(LinkList *L);
void InsertListTail(LinkList *L);
bool ListInsert(LinkList *L,int insloc);
bool ListDelete(LinkList *L,int delloc);
int ListLength(LinkList *L);
int main()
{
//初始化連結串列,並隨機生成一個隨機資料
LNode *head;//head即為單連結串列名
head = new LNode;
if(!head)
{
cout << “Error.\n”;
return 0;
}
srand(time(0));
head->data=rand()%100+1;
head->next=NULL;
cout << “Initial List…\n”;
//尾插法插入資料
InsertListTail(&head);
cout << “Done.\n”;
ShowMenu();
char choice;
int delloc,insloc,count;
while(cin >> choice){
if(!cin >> choice)
{
cout << “Please do that menu:)\n”;
exit(EXIT_FAILURE);
}
switch(choice)
{
case ‘1’: count=ListLength(&head);
cout << "Yout List length: " << count << endl;
break;
case ‘2’: cout << "Please enter a number you want to delete(1~5): ";
cin >> delloc;
ListDelete(&head,delloc);break;
case ‘3’: cout << "Please enter a place&number you want to insert(place:1~5): ";
cin >> insloc;
ListInsert(&head,insloc);break;
case ‘4’: cout << "Your list is: ";
ShowList(&head);break;
default : return 0;
}
cout << "\nPlease enter a number which you wang to do: ";
}
return 0;
}
void ShowMenu()
{
cout << “Press 1 to check your list length.\n”;
cout << “Press 2 to delete.\n”;
cout << “Press 3 to insert.\n”;
cout << “Press 4 to check your list.\n”;
cout << “Press q to quit.”;
cout << "\nPlease enter a number which you wang to do: ";
}
bool ListInsert(LinkList *L,int insloc)
{
LNode *pins,*pinstemp;
pinstemp= new LNode;
int ans=1;
pins=*L;
while(pins&&ans<insloc-1){
pins=pins->next;
++ans;
}
if(!(pins->next)||ans>insloc)
return false;
cout << "Enter the new item: ";
cin >> pinstemp->data;
pinstemp->next=pins->next;
pins->next=pinstemp;
return true;
}
bool ListDelete(LinkList *L,int delloc)
{
LNode *pdel,*pdeltemp;
int ans=1;
pdel=*L;
while(pdel&&ans<delloc-1){
pdel=pdel->next;
++ans;
}
if(!(pdel->next)||ans>delloc)
return false;
pdeltemp=pdel->next;
pdel->next=pdeltemp->next;
delete(pdeltemp);
return true;
}
int ListLength(LinkList *L)
{
LNode *plen=*L;
int count=0;
if(plen==NULL)
return false;
while(plen){
++count;
plen=plen->next;
}
return count;
}
void InsertListTail(LinkList *L)
{
LNode *pwork,*ptemp;
pwork=*L;
for(int i=1; i<MaxSize; i++){
ptemp=(LinkList)malloc(sizeof(LNode));
cout << "Enter the item: ";
cin >> ptemp->data;
pwork->next=ptemp;
pwork=ptemp;
}
pwork->next=NULL;
}
void ShowList(LinkList *L)
{
LNode *pshow=*L;
while(pshow){
cout << pshow->data << ’ ';
pshow=pshow->next;
}
cout << endl;
}
相關文章
- 實驗1
- BGP小實驗
- ospf小實驗
- 實驗文件1
- 實驗1-
- 實驗1 C++C++
- 實驗 詳解Docker的各種操作小實驗Docker
- 作業系統實驗——實驗1《CPU Scheduling》&&實驗二《Allocation & Reclaim》作業系統AI
- C語言實驗1C語言
- 實驗1 原型設計原型
- MCU點燈實驗小結
- 資料結構實驗1資料結構
- 實驗報告(1和2)
- PICO & Unity VR實戰 經驗(1)UnityVR
- 資料庫8530_實驗(1)資料庫
- c語音實驗1作業
- 1.搭建Hadoop實驗平臺Hadoop
- BGP中next-hop-self 小實驗
- 實驗8-1tensorboard視覺化+實驗8-2tensorboard案例ORB視覺化
- 實驗1 現代C++程式設計初體驗C++程式設計
- 軟體過程與管理實驗1
- 資料結構實驗課五-1資料結構
- 一個關於JAVA GC的小實驗JavaGC
- Taro實踐 – TOPLIFE小程式 開發體驗
- Taro實踐 - TOPLIFE小程式 開發體驗
- [轉] 如何實現 React 寫小程式-1React
- C++程式設計基礎實驗1C++程式設計
- 實驗1-波士頓房價預測
- 實驗2-3-1 求1到100的和 (10分)
- django與小程式實現登入驗證功能Django
- Caffeinated 6.828:實驗 1:PC 的引導過程
- 組合語言實驗1—Debug基礎操作組合語言
- 20222325 2024-2025-1 《網路與系統攻防技術》實驗八實驗報告
- 20222307 2024-2025-1 《網路與系統攻防技術》實驗五實驗報告
- 20222307 2024-2025-1 《網路與系統攻防技術》實驗七實驗報告
- 20222418 2024-2025-1 《網路與系統攻防技術》實驗五實驗報告
- # 20222309 2024-2025-1 《網路與系統攻防技術》實驗7實驗報告
- 20222404 2024-2025-1 《網路與系統攻防技術》實驗七實驗報告