php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 244|回复: 0

链表

[复制链接]

2614

主题

2621

帖子

9276

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6540
贡献
0
注册时间
2021-4-14
最后登录
2024-4-17
在线时间
665 小时
QQ
发表于 2022-11-11 11:26:40 | 显示全部楼层 |阅读模式
[mw_shl_code=applescript,true]#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;
}
[/mw_shl_code]





上一篇:基本粒子群算法的ieee30节点无功优化
下一篇:合并mysql 字符串到数组
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )51LA统计

GMT+8, 2024-4-19 13:40 , Processed in 0.183535 second(s), 38 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表