顺序表
顺序表
| Time Limit: 1000MS | Memory Limit: 65535KB |
| Submissions: 226 | Accepted: 50 |
Sample Input
5 10 1 2 3 4 5 11 22 33 44 55 66 77 88 99 100 3
Sample Output
1 2 4 5 1 2 4 5 11 22 33 44 55 66 77 88 99 100 5 4 2 1
【分析】
要反转就最好用双向链表吧
链表架构


插入元素

删除元素

# include<stdio.h> # include<malloc.h> # define Len sizeof(struct node) typedef struct node { int num; struct node *prior; struct node *next; }SeqList; SeqList *sign,*sign1=NULL; SeqList *InitList(int n) { SeqList *head,*p1,*p2; int t=n; head=(SeqList *)malloc(Len); head->next=NULL; head->prior=NULL; while(n--) { p1=(SeqList *)malloc(Len); scanf("%d",&p1->num); if(n==t-1)//这里最应注意 { p1->next=head->next;//置空p1->next head->next=p1; p1->prior=head; p2=p1; t++; } else { p1->next=p2->next;//置空p1->next p2->next=p1; p1->prior=p2; p2=p1; } } sign=p2; return head; } void ListDelete(SeqList *list,int item)//删除该节点 { SeqList *p,*Pre,*leap; p=list->next; Pre=list; while(p->next!=NULL) { if(p->num==item) { leap=p; p->prior->next=p->next;//模版删除 p->next->prior=p->prior; free(leap); p=Pre; } Pre=p; p=p->next; } sign1->prior=Pre; if(sign1->num==item) Pre->next=NULL; } SeqList *Concatenate(SeqList *head1,SeqList *head2)//连接两函数 { SeqList *p1,*head; if(head1->next!=NULL) { head=head1; p1=head1->next; while(p1->next!=NULL) p1=p1->next; p1->next=head2->next; } else head=head2; return head; } void Inversion(SeqList *list,int item)//翻转链表 { SeqList *p; p=sign1; if(p->num==item) p=p->prior; if(list->next!=NULL) { while(p->prior!=list) { printf("%d ",p->num); p=p->prior; } printf("%d\n",p->num); } else printf("\n"); } void print(SeqList *head) { SeqList *p; p=head->next; while(p->next!=NULL) { printf("%d ",p->num); p=p->next; } printf("%d\n",p->num); } void Free(SeqList *head)//释放各节点 { SeqList *p=head; while (head!=NULL)//模版释放 { p = head->next; free(head); head = p; } } int main() { int n1,n2,m; SeqList *HeadList1,*HeadList2,*head; scanf("%d%d",&n1,&n2); HeadList1=InitList(n1); sign1=sign; HeadList2=InitList(n2); scanf("%d",&m); ListDelete(HeadList1,m); print(HeadList1); head=Concatenate(HeadList1,HeadList2); print(head); Inversion(HeadList1,m); Free(head); if(HeadList1->next==NULL)free(HeadList1);//释放开辟的不用空间 else free(HeadList2); return 0; }
浙公网安备 33010602011771号