| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1967 人关注过本帖
标题:小女子作业遇到困难,请各位GG帮忙^~^
只看楼主 加入收藏
风铃之美
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-4-2
收藏
 问题点数:0 回复次数:11 
小女子作业遇到困难,请各位GG帮忙^~^
设X=(x1, x2, …, xn)和Y=(y1, y2, …, ym) (m<=n)是两个链式存储的线性表,试把它们合并为一个线性表Z,使得:
Z= (x1,y1,x2,y2,x3,y3,x4,y5,........xm,ym,,xm+1,.....xn)当n>=m时
(x1,y1,x2,y2,x3,y3,x4,y5,........xn,yn,,yn+1,.....ym)当n<m时

要求:
从键盘输入两组整数(每组7~8个数据),分别动态产生带头结点的单链表X和Y,并输出结点值。然后合并为Z,并观察输出结果。
搜索更多相关主题的帖子: 小女子 作业 困难 
2008-04-02 15:05
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
合并排序....你最好先把两个表弄有序.....

学习需要安静。。海盗要重新来过。。
2008-04-02 15:12
风铃之美
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-4-2
收藏
得分:0 
第一次编结构体,好多不明白耶。。。最好能有一个答案
2008-04-02 15:40
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
#include<iostream>
using namespace std;
typedef struct List
{
     double  a;
     struct List *next;
}List;

List* build(int n)
{   
    List *lp,*rp;
    List *p;
    
    for(int j=0;j<n;j++)
    {
        if(j==0)
        {
           p=(List*)malloc(sizeof(List));
           lp=p;
           cin>>lp->a;
        }
        else
        {
            rp=(List*)malloc(sizeof(List));
            cin>>rp->a;
            lp->next=rp;
            lp=rp;
        }
    }
    rp->next=NULL;
    return p;
}

void sort(List *p,int n)
{   
    double *a=new double[n];
    int min,i=0;
    List *p1=p;
    do
    {
        a[i++]=p1->a;
    }
    while(p1->next!=NULL&&(p1=p1->next));
      
    for( i=0;i<n;i++)
      {
        min=i;
        for(int j=i;j<n;j++)
        {
            if(a[j]<a[min])
                min=j;
        }
         double temp=a[i];
               a[i]=a[min];
               a[min]=temp;
               cout<<a[i]<<endl;
        
      }
     i=0;     p1=p;
    do
    {   
        p1->a=a[i++];
        
    }
    while(p1->next!=NULL&&(p1=p1->next));
}

void merge(List *p1,List *p2)
{
    while(p1->next!=NULL)
        p1=p1->next;
    p1->next=p2;
}

int main()
{   
    List *p1,*p2;
    int n1,n2;

    cout<<"请输入list1"<<"长度:"<<endl;
    cin>>n1;
    p1=build(n1);

    cout<<"请输入list2"<<"长度:"<<endl;
    cin>>n2;
    p2=build(n2);

    merge(p1,p2);

    sort(p1,n1+n2);

    do
    {
        cout<<p1->a<<"\t";
        
    }
    while(p1->next!=NULL&&(p1=p1->next));
    return 0;
}

学习需要安静。。海盗要重新来过。。
2008-04-02 19:57
sunkaidong
Rank: 4
来 自:南京师范大学
等 级:贵宾
威 望:12
帖 子:4496
专家分:141
注 册:2006-12-28
收藏
得分:0 
我随便写写..你看看..

学习需要安静。。海盗要重新来过。。
2008-04-02 19:57
风铃之美
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-4-2
收藏
得分:0 
好长也,太棒了,我认真研究研究,谢谢您
2008-04-03 12:17
风铃之美
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2008-4-2
收藏
得分:0 
#include "stdio.h"
#include "malloc.h"
typedef struct node
{
  int data;
  int *next;
  };
 struct node * creat(int n)
{
  struct node *head,*p1,*p2;
  head=(struct node *)malloc(sizeof(struct node));
  head=NULL;
  head->next=p1;                                       /*头指针指向p1*/
  printf("enter data");
  while(p1->data!=NULL)
  {
    p1->next=p2;
    p1=(struct node *)malloc(sizeof(struct node));
    scanf("%d ",&p1->data);
    p2->next=p1;
    }
    return(head);
}
struct node * insert(struct node *x,struct node *y,struct node *z)                                                                                                                                                                                       
{
  struct node *headz;int lx,ly;
  headz=NULL;
  headz->next=z;                                
    if(lx>ly)
  {     
     while(y->next!=NULL)
  {
     z=(struct node *)malloc(sizeof(struct node));
     z->next=x;x=x->next;
     z=(struct node *)malloc(sizeof(struct node));
     z->next=y;y=y->next;
  }
     while(x->next!=NULL)
       {
          z=(struct node *)malloc(sizeof(struct node));
          z->next=x;x=x->next;
         }
   }
   else
   {
   while(x->next!=NULL)
   {
     z=(struct node *)malloc(sizeof(struct node));
     z->next=x;x=x->next;
     z=(struct node *)malloc(sizeof(struct node));
     z->next=y;y=y->next;
  }
  while(y->next!=NULL)
       {
          z=(struct node *)malloc(sizeof(struct node));
          z->next=y;y=y->next;
         }

   }
   return(headz);
}
struct node * print(struct node *z)
{
  do
  {
    printf("%d ",z->data);
    z=z->next;
  }while(z->next!=NULL);
  return(z);
  }
main()
{
  struct node *hx,*hy,*hz;
  int lx,ly;                                     /*输入X,Y的项数即长度*/
  printf("enter lx,ly:\n");                     
  scanf("%d %d",&lx,&ly);                        
  printf("\n creat hx \n");                                       
  hx=struct node * creat(lx);
  print("hx");
  printf("\n creat hy \n");
  hy=struct node * creat(ly);
  print(hy);
  hz=struct node * insert(*hx,*hy,*hz);
  print(hz);
  }
我自己写的,可是老无法通过,而且还有语法错误,可我找不出来了。
2008-04-03 12:51
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
4 楼居然C++

6 楼的合并不符合题目啊.

[[it] 本帖最后由 cosdos 于 2008-4-3 20:24 编辑 [/it]]

—>〉Sun〈<—
2008-04-03 18:42
hangeng
Rank: 2
等 级:论坛游民
帖 子:424
专家分:39
注 册:2007-7-23
收藏
得分:0 
#include "stdio.h"
#include "malloc.h"
typedef struct node
{
  int data;
  int *next;
  };
struct node * creat(int n)
{
  struct node *head,*p1,*p2;
  head=(struct node *)malloc(sizeof(struct node));
  head=NULL;
  head->next=p1;                                       /*头指针指向p1*/
  printf("enter data");
  while(p1->data!=NULL)
  {
    p1->next=p2;
    p1=(struct node *)malloc(sizeof(struct node));
    scanf("%d ",&p1->data);
    p2->next=p1;
    }
    return(head);
}
struct node * insert(struct node *x,struct node *y,struct node *z)                                                                                                                                                                                       
{
  struct node *headz;int lx,ly;
  headz=NULL;
  headz->next=z;                                
    if(lx>ly)
  {     
     while(y->next!=NULL)
  {
     z=(struct node *)malloc(sizeof(struct node));
     z->next=x;x=x->next;
     z=(struct node *)malloc(sizeof(struct node));
     z->next=y;y=y->next;
  }
     while(x->next!=NULL)
       {
          z=(struct node *)malloc(sizeof(struct node));
          z->next=x;x=x->next;
         }
   }
   else
   {
   while(x->next!=NULL)
   {
     z=(struct node *)malloc(sizeof(struct node));
     z->next=x;x=x->next;
     z=(struct node *)malloc(sizeof(struct node));
     z->next=y;y=y->next;
  }
  while(y->next!=NULL)
       {
          z=(struct node *)malloc(sizeof(struct node));
          z->next=y;y=y->next;
         }

   }
   return(headz);
}
struct node * print(struct node *z)
{
  do
  {
    printf("%d ",z->data);
    z=z->next;
  }while(z->next!=NULL);
  return(z);
  }
int main(void)
{
  struct node *hx,*hy,*hz;
  int lx,ly;                                     /*输入X,Y的项数即长度*/
  printf("enter lx,ly:\n");                     
  scanf("%d %d",&lx,&ly);                        
  printf("\n creat hx \n");                                       
  hx=struct node * creat(lx);
  print("hx");
  printf("\n creat hy \n");
  hy=struct node * creat(ly);
  print(hy);
  hz=struct node * insert(*hx,*hy,*hz);
  print(hz);
  }

  雨水冲不进窗来,在玻璃上痛哭。但它至少奋斗过。
2008-04-03 18:45
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
/*
输入:
1 2 3 4 z
5 6 7 8 9 10 11 z

输出:
1 5 2 6 3 7 4 8 9 10 11

输入中的z用于结束数据输入
*/

#include <stdio.h>
#include <stdlib.h>

typedef struct Node{
    int Item;
    struct Node * pNext;
} * List, Node;


void Initialize(List * pList);            // 初始化列表
void AddItem(List * pList, int NewItem);  // 添加项目
void DelList(List * pList);               // 删除链表,释放内存
void ShowList(List * pList);              // 显示列表内容
List Merge(List * pListA, List * pListB); // 合并, 此函数返回合并后 List


//--------------------------------------------------------------------------
//  主函数  //

int main(void)
{
    List X, Y, Z;
    int t, k;
   
    Initialize(&X);
    Initialize(&Y);
   

    // 为2个列表添加元素  --//
    do{
        k = scanf("%d", &t);
        
        if(k == 1)
            AddItem(&X, t);
        else
            while(getchar() != '\n');
    }while(k == 1);

    do{
        k = scanf("%d", &t);
        
        if(k == 1)
            AddItem(&Y, t);
        else
            while(getchar() != '\n');
    }while(k == 1);
    //----------------------//
   
    ShowList(&X);
    ShowList(&Y);
   
    Z = Merge(&X, &Y);
   
    ShowList(&Z);
   
    puts("\n\nBye");
    system("Pause");
    return 0;
}


//--------------------------------------------------------------------------
//  List 接口函数  //

void Initialize(List * pList)      // 初始化列表
{
    *pList = NULL;
}

void AddItem(List * pList, int NewItem)    // 在列表中增加元素
{
    Node * pNewNode;               // 新节点
    Node * pNodeIndex = *pList;    // 链表索引

    pNewNode = (Node *)malloc(sizeof(Node));     // 创建新节点

    if(pNewNode == NULL)           // 新节点创建失败
    {
        puts("\n内存分配失败!\a");
        system("Pause");
        exit(1);
    }

    // 完成新节点的初始化
    pNewNode->Item = NewItem;
    pNewNode->pNext = NULL;

    if(*pList == NULL)
    {
        *pList = pNewNode;
    }
    else
    {
        while(pNodeIndex->pNext != NULL)        // 找到链表末尾
            pNodeIndex = pNodeIndex->pNext;
        pNodeIndex->pNext = pNewNode;                 // 把新节点放在链表末尾
    }
}

void DelList(List * pList)      // 删除链表,释放内存
{
    Node * pDelNode = *pList;
    Node * pNextNode;

    while(pDelNode != NULL)       // 删除所有节点
    {
        pNextNode = pDelNode->pNext;
        free(pDelNode);
        pDelNode = pNextNode;
    }

    *pList = NULL;
}

void ShowList(List * pList)
{
    Node * next = *pList;
   
    while(next != NULL)
    {
        printf("%d ", next->Item);
        next = next->pNext;
    }
   
    putchar('\n');
}

List Merge(List * pListA, List * pListB)  // 合并, 此函数返回合并后 List
{
    Node * AIndex = *pListA;
    Node * BIndex = *pListB;
    Node * pTemp;
   
    while(AIndex != NULL || BIndex != NULL)
    {
        if(AIndex->pNext != NULL)
        {
            pTemp = AIndex->pNext;
            AIndex->pNext = BIndex;
            AIndex = pTemp;
        }
        else
        {
            AIndex->pNext = BIndex;
            break;
        }
        
        if(BIndex != NULL)
        {
            pTemp = BIndex->pNext;
            BIndex->pNext = AIndex;
            BIndex = pTemp;
        }
        else
        {
            BIndex->pNext = AIndex;
            break;
        }
    }
   
    if(*pListA != NULL)
        return *pListA;
    else
        return *pListB;
}


/* 备注:我英语不行,所以此代码中的变量的命名不好 */

[[it] 本帖最后由 cosdos 于 2008-4-3 20:58 编辑 [/it]]

—>〉Sun〈<—
2008-04-03 20:47
快速回复:小女子作业遇到困难,请各位GG帮忙^~^
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024508 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved