设为首页 加入收藏

TOP

数据结构顺序表的“增删改查”功能实现(一)
2017-08-02 10:22:15 】 浏览:10102
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;
    //system("pause");一定要在return之前 
}

输出展示:

\

一样,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语言宏定义##连接符和#符的使用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目