顺序表

顺序表

Time Limit:   1000MS      Memory Limit:   65535KB
Submissions:   226      Accepted:   50
Description

实现顺序表类Seqlist,完成下面内容: (1)在顺序表类Seqlist 中增加一个成员函数,要求删除顺序表中等于item的所有数据元素。 (2)编写程序实现把顺序表类Seqlist的对象B连接到顺序表类Seqlist的对象A的尾部。 (3)编写程序,把顺序表类Seqlist的对象A中的数据元素原地反转。

Input

第一行输入对象A的长度 第二行输入对象B的长度 第三行依次输入对象A的数据元素,建立对象A 第四行依次输入对象B的数据元素,建立对象B 第五行输入要删除的数据元素

Output

第一行输出删除item后的对象A的所有数据元素 第二行输出对象B连接到对象A后的所有数据元素 第三行输出对象A原地反转后的数据元素

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;
}

 

posted on 2012-05-26 09:11  即为将军  阅读(521)  评论(0)    收藏  举报

导航