顺序表的一些简单功能

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef struct Node
{
    struct Node *next;
    int l;//线性表的长度
    int data;
} Node;
Node *head,*head1,*tail,*p,*q;
int S,n;
void Showmain()
{
    cout<<"----------------------"<<endl;
    cout<<"线性表的顺序表示与实现"<<endl;
    cout<<"1.线性表的建立"<<endl;
    cout<<"2.线性表元素的插入"<<endl;
    cout<<"3.线性表元素的删除"<<endl;
    cout<<"4.逆序建立链表"<<endl;
    cout<<"5.线性表元素的查找"<<endl;
    cout<<"6.线性表元素的合并"<<endl;
    cout<<"0.退出程序"<<endl;
    cout<<"请输入选择"<<endl;

}
void Creat()
{
    cout<<"请输入要创建线性表的长度"<<endl;
    cin>>n;
    head=(Node *)malloc(sizeof(Node));
    head->l=n;
    head->next=NULL;
    tail=head;
    cout<<"开始创建长度为"<<n<<"的线性表"<<endl;
    for(int i=1; i<=n; i++)
    {
        p=(Node *)malloc(sizeof(Node));
        if(!p)
        {
            break;
        }
        p->next=NULL;
        p->l=n;
        cin>>p->data;
        tail->next=p;
        tail=p;
    }
}
void display()
{
    p=head;
    while(p->next!=NULL)
    {
        p=p->next;
        cout<<p->data<<" ";
    }
    cout<<endl;
}
int inserch()
{
    int wei;
    cout<<"请输入插入元素的位置"<<endl;
    cin>>wei;//插入元素操作
    if(wei<1||wei>n)
    {
        return -1;
    }
    p=head;
    p->l+=1;
    for(int i=1; i<=n; i++)
    {
        if(i==wei)
        {
            q=(Node *)malloc(sizeof(Node));
            q->next=NULL;
            q->l+=1;
            cout<<"插入元素的值"<<endl;
            cin>>q->data;
            q->next=p->next;
            p->next=q;
            break;
        }
        p=p->next;
    }
    ++n;
    return 0;
}
void delet()
{
    int wei;
    cout<<"删除元素的位置"<<endl;
    cin>>wei;
    p=head;
    q=head->next;
    for(int i=1; i<=n; i++)
    {
        if(i==wei)
        {
            p->next=q->next;
            free(q);
            break;
        }
        p=q;
        q=q->next;
    }
    --n;
}
void nixu()
{
    int geshu,i;
    cout<<"请输入要插入元素的个数 :"<<endl;
    cin>>geshu;
    Node *head5,*pp;
    head5=new Node;
    head5->l=geshu;
    head5->next=NULL;
    for(i=1;i<=geshu;i++)
    {
       pp=new Node;
       cin>>pp->data;
       pp->l=geshu;
       pp->next=head5->next;
       head5->next=pp;
    }
    for(pp=head5->next;pp!=NULL;pp=pp->next)
    {
       if(pp==head5->next)  cout<<pp->data;
       else cout<<" "<<pp->data;

    }
    cout<<endl;

}
void cha()
{
    cout<<"查找线性表中的元素"<<endl;
    int flag=0;
    int wei;
    cin>>wei;
    p=head->next;
    for(int i=1; i<=n; i++)
    {
        if(p->data==wei)
        {
            cout<<"此线性表含有此元素"<<endl;
            flag=1;
            break;
        }
        p=p->next;
    }
    if(flag==0)
        cout<<"此线性表没有这个元素"<<endl;
}
void hebing()
{
    cout<<"请新建一个线性表"<<endl;
    cout<<"请输入要新建线性表的长度"<<endl;
    cin>>n;
    head1=(Node *)malloc(sizeof(Node));
    head1->l=n+head->l;
    head1->next=NULL;
    tail=head1;
    cout<<"开始创建长度为"<<n<<"的线性表"<<endl;
    for(int i=1; i<=n; i++)
    {
        p=(Node *)malloc(sizeof(Node));
        if(!p)
        {
            break;
        }
        p->next=NULL;
        p->l=n+head->l;
        cin>>p->data;
        tail->next=p;
        tail=p;
    }
    tail=head;
    p=tail->next;
    while(p!=NULL)
    {
        tail=p;
        p=p->next;
    }
    tail->next=head1->next;
    free(head1);

}
int main()
{
    Showmain();
    while(scanf("%d",&S)!=EOF)
    {
        if(S==0)
        {
            break;
        }
        else if(S==1)//创建链表
        {
            Creat();
            display();
        }
        else if(S==2)//插入元素
        {
            int tt=inserch();
            if(tt==0)
                display();
            else if(tt==-1)
            {
                cout<<"插入有误,程序不进行插入操作"<<endl;
            }
        }
        else if(S==3)
        {
            delet();
            display();
        }
        else if(S==4)
        {
            nixu();
        }
        else if(S==5)
        {
            cha();
            display();
        }
        else if(S==6)
        {
            hebing();
            display();
        }
        Showmain();
    }
    return 0;
}
作业1:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
struct node
{
    int l;//线性表的所存具体元素的个数
    int data;
} q[10001];
void Showmain()
{
    cout<<"1.创建线性表"<<endl;
    cout << "2.输出元素" << endl;
    cout << "3.插入元素" << endl;
    cout << "4.删除元素" << endl;
    cout << "5.查找元素" << endl;
    cout << "6.合并函数" << endl;
    cout << "0.退出操作" << endl;
    cout << "请输入要选择的操作:";
}
void jianli()
{
    int i,geshu;
    cout<<"请输入要建立顺序表的元素个数:";
    cin>>geshu;
    cout<<"请输入该顺序表的元素:";
    for(i=1; i<=geshu; i++)
    {
        cin>>q[i].data;
        q[i].l=geshu;
    }
    cout<<"已成功建立顺序表"<<endl;
}
void display()
{
    int i;
    for(i=1; i<=q[1].l; i++)
    {
        if(i==1)
            cout << q[i].data;
        else cout<<" "<<q[i].data;
    }
    cout << endl;
}
void inseart()
{
    int t,b,i;
    cout<<"请输入在第几个元素前插入新元素:";
    cin>>t;
    for(int i=1; i<=q[1].l; i++) //线性表长度+1
        q[i].l+=1;
    cout<<"请输入要插入的元素:";
    cin>>b;
    for(i=q[1].l; i>t; i--)
    {
        q[i].data=q[i-1].data;
    }
    q[t].data=b;
    cout<<"已成功插入新元素"<<endl;
}
void delet()
{
    int i,b,j,flag=0;;
    cout<<"请输入要删除的元素:";
    cin>>b;
    for(i=1; i<=q[1].l; i++)
    {
        if(q[i].data==b)
        {
            flag=1;
            q[1].l-=1;//线性表长度-1
            for(j=i; j<=q[1].l; j++)
            {
                q[j].data=q[j+1].data;
            }
        }
    }
    for(i=2; i<=q[1].l; i++) //线性表长度-1
    {
        q[i].l=q[1].l;
    }
    if(flag==1)
    {
        cout<<"删除成功"<<endl;
    }
    else
    {
        cout<<"没有找到该元素,删除失败"<<endl;
    }
}
void chazhao()
{
    int t,i,flag=0;
    cout<<"请输入要查找的元素:";
    cin>>t;
    for(i=1; i<=q[1].l; i++)
    {
        if(q[i].data==t)
        {
            flag=1;
            cout<<"您所查找的元素为第"<<i<<""<<endl;
            break;
        }
    }
    if(flag==0)
    {
        cout<<"没有找到该元素,查找失败"<<endl;
    }
}
void hebing()
{
    int i,j,m,t;
    int b[10001];
    cout<<"请输入您要合并的另一个顺序表的个数:";
    cin>>m;
    t=q[1].l;
    q[1].l=q[1].l+m;
    for(int i=1; i<=q[1].l; i++)
    {
        q[i].l=q[1].l;
    }
    cout<<"请输入另一个顺序表的元素:";
    for(i=1; i<=m; i++)
    {
        cin>>b[i];
    }
    j=1;
    for(i=t+1; i<=q[1].l; i++)
    {
        q[i].data=b[j++];
    }
}
int main()
{
    int choose;
    Showmain();
    while(cin>>choose)
    {
        if(choose==1)
        {
            jianli();
        }
        else if(choose==2)
        {
            display();
        }
        else if(choose==3)
        {
            inseart();
        }
        else if(choose==4)
        {
            delet();
        }
        else if(choose==5)
        {
            chazhao();
        }
        else if(choose==6)
        {
            hebing();
        }
        else if(choose==0)
        {
            cout<<"退出系统"<<endl;
            break;
        }
        Showmain();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zhangmingcheng/p/3962187.html