G药(链表的增、删、查找)

来源:互联网 发布:java前端面试要求 编辑:程序博客网 时间:2024/05/30 04:34

题目描述

新年伊始,飞神得到了一个叫做药的链表…(弱已词穷…初始时链表只有头指针,对链表存在三种操作。增加一个元素,向链表中增加一个元素,增加后要保证链表从前到后为单调不降序列。删除一个元素,从链表中删除一个元素,删除后要保证剩余节点仍为单调不降序列。按序将链表中的元素全部输出。

输入

多组输入。对于每组数据,第一行一个整数n(1 <= n <= 1000),代表有n次操作。接下来的n行,每行描述一次操作,形式如下。A val。表明此时向链表中添加一个元素val(val不会超出int)。D rank。表明此时要在链表中删除第rank个元素,若不存在,则忽略此次操作。Q。按序将链表中的元素全部输出,若链表为空,则忽略此次操作。 输出 对于每次第三种操作,按序将链表中的元素全部输出。 示例输入6A 1A 2A 5QD 3Q

示例输出

1 2 51 2

提示

//#include <bits/stdc++.h>#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <stack>#include <set>#include <queue>#include <algorithm>#define CLR(a, b) memset(a, (b), sizeof(a))#define INF 0x3f3f3f3f#define eps 1e-8typedef long long LL;using namespace std;typedef struct Node{    int date;    struct Node *next;} node;/*链表增加函数*/node *appen(node *head,int b,int cnt){    if(cnt==0)    {        head=(node *)malloc(sizeof(node));        head->next=NULL;        head->date=b;    }//如果链表中没有元素直接加到头上    else    {        node *pr=head;        node *p=(node *)malloc(sizeof(node));        p->date=b;        p->next=NULL;        if(head->date>p->date)        {            node *temp=head;            head=p;            head->next=temp;        }//如果头元素比该元素大,把该元素作为头元素        else        {            int flag=0;            while(pr->next!=NULL)            {                node *pn=pr->next;                if(pr->date<=b&&pn->date>=b)                {                    pr->next=p;                    p->next=pn;                    flag=1;                    break;                }                else                    pr=pr->next;            }//查找合适的位置添加该元素            if(flag==0)            {                pr->next=p;                p->next=NULL;            }//如果比每个元素都大就添加在末尾        }    }    return head;}/*链表的查询函数*/void show(node *head){    if(head==NULL)        return ;    node *pr=head;    while(pr->next!=NULL)    {        printf("%d ",pr->date);        pr=pr->next;    }    printf("%d\n",pr->date);}/*链表的删除函数*/node *delet(node *head,int b){    node *pr=head;    if(b==1)    {        head=pr->next;        free(pr);    }//若只有一个元素删掉就行了    else    {        for(int i=1; i<b-1; i++)        {            pr=pr->next;        }        node *p1=pr->next;        pr->next=p1->next;        free(p1);    }//链表中如果有多个元素,移动到要删除的元素前面的位置,让next指向要删元素的后一个    return head;}int main(){#ifdef LOCAL     freopen("E://in.txt","r",stdin);     freopen("E://out.txt","w",stdout);#endif // LOCAL    int n;    char a[10];    while(scanf("%d",&n)!=EOF)    {        node *head=NULL;        int cnt=0;//记录链表中元素的个数        while(n--)        {            int b;            scanf("%s",a);            if(a[0]=='A')            {                scanf("%d",&b);                head=appen(head,b,cnt);                cnt++;            }            else if(a[0]=='Q'&&cnt)            {                // if(cnt)                show(head);            }            else if(a[0]=='D')            {                scanf("%d",&b);                if(cnt>=b&&b)                {                    head=delet(head,b);                    cnt--;                }            }        }    }    return 0;}
0 0
原创粉丝点击