错误代码,写了一个多小时还是错的,而且只有两分,悲伤逆流成河
#include <iostream> #include <map> #include <stdio.h> #include <math.h> #include <algorithm> using namespace std; map<int,int>mp; typedef struct List { int ard,data,nard; struct List *next; }List; List* CreatList(int n) { List *head,*q,*p; head=(List*)malloc(sizeof(List)); head->next=NULL; q=head; for(int i=0;i<n;i++) { p=(List*)malloc(sizeof(List)); scanf("%d %d %d",&p->ard,&p->data,&p->nard); p->next=NULL; q->next=p; q=p; } return head; } List* Delete(List *head,List *head1) { List *q,*p,*k,*t,*s; head->data=99999; q=head; p=head; head1->next=NULL; t=head1; while(q->next!=NULL) { int a=fabs(q->next->data); mp[a]++; if(q->next->data==p->data) { k=q->next; p->next=q->next->next; mp[a]--; s=(List*)malloc(sizeof(List)); s->ard=k->ard; s->data=k->data; s->nard=k->nard; s->next=NULL; t->next=s; t=s; free(k); } else { if(mp[a]>1) { k=q->next; p->next=q->next->next; mp[a]--; s=(List*)malloc(sizeof(List)); s->ard=k->ard; s->data=k->data; s->nard=k->nard; s->next=NULL; t->next=s; t=s; free(k); } else { q=q->next; p=p->next; } } } return head; } List* Sort(int start,int n,List *head) { List *q,*head1,*p,*t,*k; head1=(List*)malloc(sizeof(List)); head1->next=NULL; q=head1; t=head; for(int i=0;i<n;i++) { t=head; while(t->next!=NULL) { if(t->next->ard==start) { p=t->next; k=(List*)malloc(sizeof(List)); start=p->nard; k->ard=p->ard; k->data=p->data; k->nard=start; k->next=NULL; q->next=k; q=k; t->next=p->next; free(p); break; } t=t->next; } } return head1; } void Print(List *head) { head=head->next; while(head!=NULL) { if(head->nard>=0) printf("%05d %d %05d\n",head->ard,head->data,head->nard); else printf("%05d %d -1\n",head->ard,head->data); head=head->next; } } int main() { int start,n; cin>>start>>n; List *p=CreatList(n); List *q=Sort(start,n,p); List *t=Delete(q,p); Print(t); Print(p); }
原文地址:https://www.cnblogs.com/xyfs99/p/10357679.html
时间: 2024-10-12 04:12:10