[数据结构] 线性表的实现

1.顺序表

//#pragma GCC optimize(2)
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <ctime>
#include <vector>
#include <fstream>
#include <list>
#include <iomanip>
#include <numeric>
using namespace std;
typedef long long ll;

const int MAXN = 1e6 + 10;

// 
// 线性表的实现 - Zeooolim
// 
 

struct node  //元素
{
    int num;

    bool operator == (const node &b) const
    {
        if(this->num == b.num)
            return true;
        return false;
    }
};

typedef struct  				//表
{
    node *elem; 				//元素数组的首位 

    int length; 				//表的长度 
}Sqlist;

int InitList(Sqlist &L) 		//构造空表
{
    L.elem = new node[MAXN]; 	//申请空间 
    if(!L.elem) exit(-2);		//内存不足 
    L.length = 0;				//初表长度 
    return 1;					
}

int GetElem(Sqlist L, int i, node &e) 		//随机查表
{
    if(i < 1 || i > L.length)	 
        return 0;
    e = L.elem[i - 1];

    return 1;
}

int LocateElem(Sqlist L, node e) 			//元素查找
{
    for(int i = 0; i < L.length; i++)
        if(L.elem[i] == e)
            return i + 1;
    return 0;
}

int ListInsert(Sqlist &L, int pos, node e) 	//插入元素
{
    if(pos < 1 || pos > L.length + 1)
        return 0;
        
    node *p;
    
    for(p = &L.elem[L.length]; p >= &L.elem[pos]; p--)
    	*p = *(p - 1);
    	
    *p = e; 
	
	L.length++;
	
//    数组取址实现: 
//    for(int i = L.length; i >= pos; i--)
//        L.elem[i] = L.elem[i - 1];
//    
//    L.elem[pos - 1] = e;
    return 1;

}

int ListClear(Sqlist &L)
{
	L.length = 0;
	return 1;
}

int ListDelete(Sqlist &L, int pos) 			//删除元素
{
    if(pos < 1 || pos > L.length)
        return 0;

    node *p;
    
    for(p = &L.elem[pos - 1]; p < &L.elem[L.length - 1]; p++) //指针实现
        *p = *(p + 1);

    L.length--;
    
//     数组取址实现
//     for(int i = pos - 1; i < L.length - 1; i++)
//         L.elem[i] = L.elem[i + 1];

return 1;
}

void ListPut(Sqlist L)
{
    for(int i = 0; i < L.length; i++)
    {
        cout<<"实际:  "<<i<<"  逻辑:  "<<i + 1<<"  值:  "<<L.elem[i].num<<endl;
    }
}

void ListSet(Sqlist &L, int n)
{
	L.length = n;
	
	for(int i = 0; i < n; i++)
		L.elem[i].num = i + 1;
		
}

int main()
{
    Sqlist L;

    InitList(L);
	
	cout<<"空表已建立"<<endl;
	
    int mode;

    while(1)
    {
        cout<<"0:输出表 1:取值(随机查找) 2:查找值定位 3:插入元素 4:删除元素 5:顺序填表 6:清空表"<<endl;
        cin>>mode;
        int pos, val;
        node e;
        switch(mode)
        {
			case 0:
                ListPut(L);
                break;
            case 1:
                cout<<"输入查找位置(逻辑):"<<endl;
                cin>>pos;
                GetElem(L, pos, e);
                cout<<e.num<<endl;
                break;
            
            case 2:
                cout<<"输入要查找的值:"<<endl;
                cin>>e.num;
                pos = LocateElem(L, e);
                cout<<e.num<<"的逻辑位置:  "<<pos<<"  实际位置: "<<pos - 1<<endl; 
                break;
            case 3:
               	cout<<"输入插入位置和插入值:"<<endl;
				cin>>pos>>e.num;
				ListInsert(L, pos, e);
                break; 
            case 4:
               	cout<<"输入删除元素位置:"<<endl;
               	cin>>pos;
               	ListDelete(L, pos);
                break;
            case 5:
               	cout<<"输入要填的长度:"<<endl;
               	cin>>pos;
               	ListSet(L, pos);
                break;
            case 6:
            	cout<<"清空完成"<<endl;
				ListClear(L); 
				break;
            default:
            	goto end;
        }  
        
    }
    
    end:
    	
    return 0;
}

数据结构

猜你喜欢

转载自blog.csdn.net/Zeolim/article/details/82823343