xmuC语言程序实践week 3 大作业

时间:2022-07-25
本文章向大家介绍xmuC语言程序实践week 3 大作业,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

本周作业较简单,单链表实现插入排序(一百来行)

最终实现什么效果呢?

1.在线插入,在线查询输出排列后有序链表,并且输出新加入元素的插入位置

2.如输入负数,则删除其相反数,有多个的话删除编号最小的即可

3.一些文字提示balabala的

最后废话一点,删除元素思路:

置pre now双指针,找到要删的元素pre指向now后一个链表元素地址,相当于直接将要删除元素架空

插入排序思路:

类似人打扑克牌,开始时第一个元素是有序区默认已排好序,即L->next,毕竟是带头结点的单链表嘛,就是头结点之后的那个结点,每次取无序区元素作为分析对象,注意,这个时候要遍历的是有序区的元素,想想看是不是和打牌很像,一张张插入,但是你每次都在已经排好序的区域找要放的这张牌的位置那就是该位置你要插入的值大于左边的值,小于右边的值

一些细节在代码注释中有说到~

#include<bits/stdc++.h>
#define rg register long long
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 5005
const double eps=1e-8;
using namespace std;
typedef int Elemtype;
typedef struct LNode
{
    Elemtype data;
    struct LNode* next;
} LNode, *LinkList;
inline ll read()
{
    char ch=getchar();
    ll s=0,w=1;
    while(ch<48||ch>57)
    {
        if(ch=='-')
            w=-1;
        else
        {
            cout<<"Input Error!"<<endl;
            return inf;
        }
        ch=getchar();
    }
    while(ch>=48&&ch<=57)
    {
        s=(s<<1)+(s<<3)+(ch^48);
        ch=getchar();
    }
    return s*w;
}
inline void write(ll x)
{
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+48);
}
ll n;
inline void Display(LinkList L)
{
    LinkList p=L->next;
    //cout<<p->data<<endl;
    while(p)
    {
        p->next==NULL?cout<<p->data:cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
}
inline void Sort(LinkList &L)
{
    LinkList p, p1, q, pre;
    if (L->next != NULL)
    {
        p = L->next->next;
        L->next->next = NULL;
        while (p != NULL)
        {
            pre = L;  //pre指向q的前驱
            q = pre->next;
            while (q != NULL&&q->data < p->data)//从链表第二个结点开始找比当前插入值大的结点
            {
                pre = q;
                q = q->next;
            }
            p1 = p->next;//将p插入到结点pre和p之间
            p->next = pre->next;
            pre->next = p;
            p = p1;
        }
    }
}
inline void List_insert(LinkList &L,ll index)
{
    LinkList p=L;
    while(p->next!=NULL)
    {
        p=p->next;
    }
    LinkList temp=(LinkList)malloc(sizeof(LNode));
    temp->data=index;
    temp->next=NULL;
    //cout<<temp->data<<endl;
    p->next=temp;
    //cout<<p->next->data<<endl;
    //Display(L);
    //Display(L);
    //cout<<"a"<<endl;
    //free(temp);
}
inline ll Search_pos(LinkList &L,ll index)
{
    if(L->next==NULL)return 0;
    ll ans=0,flag=0;
    LinkList p=L->next;
    while(p)
    {
        ans++;
        if(p->data==index)
        {
            flag=1;
            return ans;
        }
        p=p->next;
    }
    return flag==1?ans:0;
}
inline void Delete_index(LinkList &L,ll index)
{
    LinkList p=L->next,pre=L;
    while(p)
    {
        if(p->data==index)
        {
            pre->next=p->next;
            free(p);
            return ;
        }
        pre=p;
        p=p->next;
    }
    return ;
}
inline void work()
{
    while(1)
    {
        cout<<"please input numbers of figures:";
        n=read();
        if(n<=1024&&n>0)
            break;
        if(n>1024)
        {
            cout<<"Too many!"<<endl;
            return ;
        }
    }
    LinkList L;
    L = (LinkList)malloc(sizeof(LNode));
    L->next=NULL;
    cout<<"Input begin:"<<endl;
    for(rg i=0; i<n; i++)
    {
            ll index=read();
            if(index==inf)  return ;
            if(index>=0)
            {
                if(index==0)
                {
                    cout<<"Insert failed,another try!"<<endl;
                    i--;
                    continue;
                }
                //cout<<index<<endl;
                List_insert(L,index);
                //Display(L);
                Sort(L);
                cout<<"Insert Position:"<<Search_pos(L,index)<<endl;
                cout<<"Current Sequence:";
                Display(L);
            }
            else if(index<0)
            {
                index*=-1;
                //cout<<index<<endl;
                if(Search_pos(L,index))
                {
                    cout<<"Delete Position:"<<Search_pos(L,index)<<endl;
                    Delete_index(L,index);
                    cout<<"Current Sequence:";
                    Display(L);
                }
                else
                    cout<<"Not found,delete failed,another try!"<<endl;
            }
    }
    cout<<"All is done!"<<endl;
}
int main()
{
    work();
    return 0;
}