数据结构顺序表的“增删改查”功能实现
今天完成的是顺序表的简单功能实现
#include<iostream>
#define maxSize 100using 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' k= int(raw_input()) a.remove(a[k-1]) print "the list is ",a,' now' p,q =map(int,raw_input("enter the position and number you want to modify:\n").split()) a[p-1] = q print "the list is ",a,' now' print 'enter the number you want to find in the list' n = int(raw_input()) if n in a: for i in range(len(a)): if a[i] == n: print "%d is the %dth element in the list"% (n,i+1),a else: print 'Not found!\n'
输出展示:
E:\Python27\python.exe D:/python/python算法/b.py
Enter the list:
1 2 3 4
The list is:
[1, 2, 3, 4]
The length of the list is: 4
Enter the position and number you want to add:
1 2
the list is [2, 1, 2, 3, 4] now
enter the position you want to remove in the list
2
the list is [2, 2, 3, 4] now
enter the position and number you want to modify:
1 5
the list is [5, 2, 3, 4] now
enter the number you want to find in the list
4
4 is the 4th element in the list [5, 2, 3, 4]
Process finished with exit code 0
OK!收工!