数据结构顺序表的“增删改查”功能实现
程序员文章站
2022-04-15 19:45:02
今天完成的是顺序表的简单功能实现
#include
#define maxsize 100
using namespace std;
typedef s...
今天完成的是顺序表的简单功能实现
#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' 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
收工!
下一篇: 狂魔型代码改造记