TOP

數據結構順序表的“增刪改查”功能實現(一)
2017-08-02 10:22:15 】 瀏覽:10354
Tags:

今天完成的是順序表的簡單功能實現

#include<iostream>

#define maxSize 100
using namespace std;
typedef struct{
        int data[maxSize];//data數組用來放數據 
        int length;//length是數組的長度 
}Sqlist;
//建立一個結構體用來表示順序表 

void PrintList(Sqlist &L)//打印順序表 
{
     cout<<"now the list is:"<<endl;
     for(int i=0;i<=L.length-1;i++)
         cout<<L.data[i]<<" ";
     cout<<endl;
}

void InitList(Sqlist &L)//初始化數組,即就是輸入數組初始元素 
{
    int n;
    cout<<"please enter the number of your list:"<<endl;
    cin>>n;
    L.length = n;
    cout<<"please enter the element of your list:"<<endl;
    for(int i=0;i<=L.length-1;i++){
       cin>>L.data[i];              
    }
    cout<<"the length of the list is:"<<L.length<<endl;

}

void InsertList(Sqlist &L,int n,int p)//插入數據操作 
{
    int i;
    if(n<0 || n>L.length)
        cout<<"Error!please reput!"<<endl;
    else
    {
        cout<<"now we insert "<<p<<" to the "<<n<<" th position in the"<<" list"<<endl;
        for(i=L.length-1;i>=n-1;--i)
            L.data[i+1] = L.data[i];
                   L.data[n-1] = p;
               L.length += 1;//數組長度加一 
    }
}

void DeleteElem(Sqlist &L,int n)//刪除數組中的元素 
{
    int i,obj;
    if(n<0 || n>L.length)
        cout<<"Error!"<<endl;
    else{
        obj = L.data[n-1];
        cout<<"now we delete the "<<n<<" th position element "<<obj<<" in the list"<<endl;
        for(i=n;i<=L.length;i++)
            L.data[i-1] = L.data[i];//將刪除點之后的元素都向前移一位     
        L.length -= 1;//數組長度減去一 
    }          
}

void ModifyList(Sqlist &L,int n,int p)//修改數組元素 
{
    if(n<0 || n>L.length-1)
        cout<<"Error!"<<endl;
    else{
        cout<<"now we modify the "<<n<<" th element into the element "<<p<<endl;
        L.data[n-1] = p;//很簡單直接賦值就好 
    }     
}

int FindEle(Sqlist L)//查找數組元素 
{
    int i,a,leap=0;
    cout<<"please enter the element you want to find:"<<endl;
    cin>>a;
    for(i=0;i<=L.length-1;i++)
    {
         if(L.data[i] == a){
             cout<<"the element "<<a<<" is in "<<i+1<<"th position in the list"<<endl;          
                         leap = 1;
             break;
             }
    }
    if(leap==0)
        cout<<"Sorry not found!"<<endl;
}

int main(){
       Sqlist P;
    InitList(P);
    PrintList(P);
    InsertList(P,2,4);
    PrintList(P);
    DeleteElem(P,3);
    PrintList(P);
    ModifyList(P,4,18);
    PrintList(P);
    FindEle(P);    
    system("pause");
    return 0;
   }

輸出展示:

\

一樣,Python再來實現一遍:

#-*-coding:UTF-8-*-
a = map(int,raw_input("Enter the list:\n").split())
print 'The list is:\n'
print a
print 'The length of the list is:',len(a)
def add(list):
    print 'Enter the position and number you want to add: '
    m,n = map(int,(raw_input().split()))
    b = list[:m-1]
    c = list[m-1:]
    b.append(n)
    d = b + c
    print "the list is ",d,' now'
    return d
a = add(a)
print 'enter the position you want to remove in the list'

		

請關注公眾號獲取更多資料



首頁 上一頁 1 2 下一頁 尾頁 1/2/2
】【打印繁體】【】【】 【】【】【】 【關閉】 【返回頂部
上一篇While循環以及 do while 循環介紹 下一篇C語言宏定義##連接符和#符的使用