admin 发表于 2022-11-11 11:26:40

链表

#include<iostream>
#include<stdlib.h>
using namespace std;
static int flag=0;
struct node
{
        int data;
        struct node*next;
};
struct node*create();
void output(struct node*head);
void insert(struct node *head,int place,int x);
void delete_(struct node *head,int place);
void list(struct node*head);
void Getelem(struct node*head,int n);
void Tips();
int main()
{
        struct node*head;
        int place,x;
        int i,n;
        int just=0;
        do
        {
                system("cls");
                Tips();
                cout << "请输入你的选择:";
                cin >> i;               
                switch(i)
                {
                case 1:
                        {
                                head=create();
                                cout << "成功初始化一个链表!" << endl;
                                just=1;
                                break;
                        }
                case 2:
                        {
                                list(head);
                                just=0;
                                break;
                        }
                case 3:
                        {
                                if(just)
                                        output(head);
                                else
                                        cout << "链表不存在。" << endl;
                                break;
                        }
                case 4:
                        {
                                if(just)
                                {
                                        cout<<"intsert place:";
                  cin>>place;
                  cout<<endl;
                      cout<<"x:";
                  cin>>x;
                  insert(head,place,x);
                                }
                                else
                                        cout << "链表不存在。" << endl;
                                break;
                        }
                case 5:
                        {
                                if(just)
                                {
                                   cout<<"delete place:";
                   cin>>place;
                   delete_(head,place);
                                }
                                else
                                        cout << "链表不存在。" << endl;
                                break;
                        }
                case 6:
                        {
                                if(just)
                                {
                                        cout<<"请输入查找元素位置:"<<endl;
                                        cin>>n;
                                  Getelem(head,n);
                                }
                                else
                                        cout << "链表不存在。" << endl;
                                break;
                        }
       
                case 7:
                        {
                                cout << "操作结束!" << endl;
                                exit(0);
                        }
                default:
                        {
                                cout << "没有该选项,请重新选择。" << endl;
                                break;
                        }
                }
                system("pause");
        }while(1);
}
struct node*create()//创建一个链表并初始化
{
        struct node*head,*p,*pre;
        head=(struct node*)malloc(sizeof(struct node));
        head->next=NULL;
        pre=head;
        int x;
        cout<<"input numbers end of -1"<<endl;
        while(1)
        {
                cin>>x;
                if(x==-1) break;
                p=(struct node*)malloc(sizeof(struct node));
                p->next=NULL;
                p->data=x;
                pre->next=p;
                pre=p;
        }
        flag++;
        return head;
}
void output(struct node*head)//输出链表内容
{
        struct node *p;
    cout<<"output:"<<endl;
    p=head->next;
    while(p!=NULL)
    {
      cout<<p->data<<"";
      p=p->next;
    }
}
void insert(struct node *head,int place,int x)//插入元素
{
        struct node*pre,*p;
        int i;
        pre=head;
        for(i=0;i<place-1&&pre!=NULL;i++)
        {
          pre=pre->next;        
        }
    if(place<1||pre==NULL)
    {
            cout<<"error"<<endl;
        }
        else{
                        p=(struct node*)malloc(sizeof(struct node));
                p->data=x;
                p->next=pre->next;
                pre->next=p;
                cout<<"ok"<<endl;
        }
}
void delete_(struct node *head,int place)//删除链表元素
{
        struct node*pre,*p;
        int i;
        pre=head;
        for(i=0;i<place-1&&pre->next!=NULL;i++)
        {
          pre=pre->next;        
        }
        if(place<1||pre->next==NULL)
        {
                cout<<"error"<<endl;
        }
        else
        {
                p=pre->next;
          pre->next=p->next;
          free(p);
          cout<<"ok"<<endl;
        }
}
void list(struct node*head)//销毁链表
{
        struct node *p;
        p=NULL;
        if(head&&flag!=0)
        {
                while(head)
                {   
                        p=head;
                        head=head->next;
                        free(p);       
                }
                cout << "链表已销毁。" << endl;
        }
        else
                cout << "链表不存在。" << endl;
        flag++;
}
void Getelem(struct node*head,int n)
{
        struct node*p;
        int e;
        int j=1;
        p=head->next;
        while(p&&j<n)
        {
                p=p->next;
                ++j;
        }
        if(!p||j>n)
                cout << "该位序不存在。" << endl;
        else
        {
                e=p->data;
                cout << "第" << n << "个元素为:" << e << endl;
        }
}
void Tips()//目录
{
        cout << "可执行操作有:" << endl;
        cout << "*********************************************************" << endl;
        cout << "***************1.初始化或重置链表      ****************" << endl;
        cout << "***************2.销毁链表            ****************" << endl;
    cout << "***************3.输出链表内容          ****************" << endl;
        cout << "***************4.插入元素            ****************" << endl;
        cout << "***************5.删除元素            ****************" << endl;
    cout << "***************6.查找元素            ****************" << endl;
    cout << "***************7.退出                  ****************" << endl;
}
页: [1]
查看完整版本: 链表