【数据结构_顺序表_List_0943】顺序表插入操作的实践

来源:互联网 发布:nuc977dk62y数据手册 编辑:程序博客网 时间:2024/05/16 03:36

建立长度为n的顺序表,在指定的数据元素item之前插入数据元素data。如果指定的数据元素item不存在,则将data插入到顺序表的尾端。(数据类型为整型)

第一行为顺序表的长度n; 
第二行为顺序表中的数据元素; 
第三行为指定的数据元素item; 
第四行为要插入的数据元素data;

输出结果为顺序表中的数据元素。

--------------------------------------------------------------------

10
10 20 30 40 50 60 70 80 90 100
50
55
---------------------------------------------------------------------

10 20 30 40 55 50 60 70 80 90 100

--------------------------------------------------------------------


#include <stdio.h>#include <iostream>#include <string>using namespace std;struct node{    int data[105];    int Length;}L;void Insert(node *L,int tar,int insert){    int i,j;    for(i=0;i<L->Length;i++)    {        if(L->data[i]==tar)        {            for(j=L->Length-1;j>=i;j--)            {                L->data[j+1]=L->data[j];            }            L->Length++;            L->data[i]=insert;            break;        }             }    if(i==L->Length)    {        L->data[i]=insert;        L->Length++;    }}void Traverse(){    for(int i=0;i<L.Length;i++)        cout<<L.data[i]<<" ";    //cout<<endl;}int main(){    int num,tar,insert;    int i;    cin>>num;    L.Length=num;    for(i=0;i<num;i++)        cin>>L.data[i];    cin>>tar>>insert;    Insert(&L,tar,insert);    Traverse();    return 0;}


0 0
原创粉丝点击