| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 820 人关注过本帖
标题:顺序表的一些算法
只看楼主 加入收藏
wu550399048
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2012-10-25
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:5 
顺序表的一些算法
#include<iostream.h>
class List{
private:
    int a[50];
    int L;
public:
    List(int b[],int l){
    L=l;   
    for(int i=0;i<L;i++)
            a[i]=b[i];
        
    }

    void same(List &m,List &n);
    void output();};


    void List::output(){
        int i;
        for(i=0;i<L;i++)
            cout<<a[i]<<'\t';
    cout<<'\n';}

   

void List::same(List&m,List&n){
    {
        int i,j;
        for(i=0;i<m.L;i++)
            for(j=0;j<n.L;j++)
                if(m.a[i]<n.a[j])
                    break;
                else if(m.a[i]=n.a[j])
                {break;
                a[L]=m.a[i];
                L++;}
}



   
   
   

        
void main()
{int a[]={2,4,5,6};int b[]={2,4,6,8,10};
List l1(a,4),l2(b,5);
l1.output();l2.output();List l3(l1,l2);
l3.output();}
帮忙看一下哪里错了。。。
搜索更多相关主题的帖子: void include public private 
2012-10-25 15:52
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:7 
List l3(l1,l2);   没定义这种构造函数
2012-10-25 20:36
爱闹的娃
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:3
帖 子:265
专家分:975
注 册:2011-10-23
收藏
得分:7 
回复 楼主 wu550399048
   二楼的BZ说得正解啊.....LZ的list l3(l1,l2)...LZ是传的两个list对象,而在list类中没有   
  定义出这样的构造函数,再则你这样传进去的目的是什么?想用谁的a[]作为当前对象的a[]  
  和用谁的L去作为当前对象的L??当搞清楚后...再写一个构造函数出来就OK
2012-10-25 23:42
梁朝斌
Rank: 4
等 级:业余侠客
帖 子:192
专家分:288
注 册:2012-10-21
收藏
得分:7 
放一个程度源码出来供大家参考;数据结构-单链表
#include<stdio.h>
 #include<malloc.h>
 #include<stdlib.h>
 #define N 100
 #define OK 1
 #define Error 0
 int yes=0;
 typedef char elemtype;
 typedef struct lnode
 {
     char data;
     struct lnode *next;
 }lnode,*linklist;
 void createlist(linklist *l)
 {
     int i,n;
     linklist p;
     char ch;
     printf("请输入单链表的长度:\n");
     scanf("%d",&n);
     *l=(linklist)malloc(sizeof(lnode));
     (*l)->next=NULL;
     printf("请输入单链表中元素:\n");
     for(i=n;i>0;i--)
     {
         p=(linklist)malloc(sizeof(lnode));
         scanf("%c",&ch);
         if((ch=getchar())!='\n')
         {
             p->data=ch;
             p->next=(*l)->next;
             (*l)->next=p;
         }
     }
     yes=1;
 }
 int length(linklist *l)
 {
     int i=0;
     linklist p;
     p=(*l)->next;
     while(p!=NULL)
     {
         i++;
         p=p->next;
     }
     return i;
 }
 display(linklist *l)
 {
     linklist q;
     int i;
     printf("此时单链表元素:\n");
     for(i=1;i<=length(l);i++)
     {
         printf("%3d",i);
     }
     printf("\n");
     q=(*l)->next;
     while(q)
     {
         printf("%3c",q->data);
         q=q->next;
     }
     printf("\n");
 }
 listdelete(linklist l,int i,char*e)
 {
     linklist p=l,q;
     int j=0;
     while(p&&j<i-1)
     {
         p=p->next;
         j++;
     }
     if(!p||j>i-1)
         return -1;
     q=p->next;
     *e=q->data;
     free(q);
     return *e;
 }
 int listinsert(linklist l,int i,char e)
 {
     linklist p=l,s;
     int j=0;
     while(p&&j<i-1)
     {
         p=p->next;
         j++;
     }
     if(!p||j>i-1)
         return -1;
     s=(linklist)malloc(sizeof(lnode));
     s->data=e;
     s->next=p->next;
     p->next=p->next;
     p->next=s;
     return OK;
 }
 destroylist(linklist l)
 {
     linklist p=l,q=l->next;
     while(q)
     {
         free(p);
         p=q;
         q=q->next;
     }
     free(q);
     yes;
 }
 listempty(linklist l)
 {
     return (l->next==NULL);
 }
 void menu()
 {
     printf("                 单链表基本操作实验                   \n");
     printf("******************************************************\n");
     printf("请选择命令序号(0-7):                                 *\n");
     printf("*1创建链表;                                   *\n");
     printf("*2链表插入;                                          *\n");
     printf("*3链表删除;                                          *\n");
     printf("*4求链表长度;                                        *\n");
     printf("*5判断链表是否为空;                                  *\n");
     printf("*6链表输出;                                          *\n");
     printf("*7单链表销毁;                                        *\n");
     printf("*0程序结束.                                          *\n");
     printf("******************************************************\n");
 }
 void function()
 {
     linklist l;
     int i,sel;
     char ch,e;
     while(l)
     {
         menu();
         printf("请输入命令号(0-7):\n");
         scanf("%d",&sel);
         switch(sel)
 
        {
             case 1:
                 {
                     createlist(&l);
                     printf("单链表创建成功!\n");
                     break;
                 }
             case 2:if(yes==0)
                    {
                        printf("此时表未创建!不能插入元素!\n");
                        break;
                    }
                 else
                 {
                     printf("请输入要插入的位置:\n");
                     scanf("%d",&i);
                     printf("请输入要插入的值:\n");
                     scanf("%c",&e);
                     if((e=getchar())!='\n')
                         listinsert(l,i,e);
                     break;
                 }
             case 3:if(yes==0)
                    {
                        printf("此时表未创建!不能删除元素!\n");
                        break;
                    }
                 else
                 {
                     printf("请输入要删除位置:\n");
                     scanf("%d",&i);
                     listdelete(l,i,&ch);
                     break;
                 }
             case 4:if(yes==0)
                    {
                        printf("此时表未创建!不能求单链表长度!\n");
                        break;
                    }
                 else
                 {
                     printf("单链表的长度是%d\n",length(&l));
                     break;
                 }
             case 5:if(yes==0)
                    {
                        printf("此时表未创建!\n");
                        break;
                    }
                 else if(listempty(l))
                     printf("当前的单链表是空表!\n");
                 else
                     printf("当前单链表为非空表!\n");
                 break;
             case 6:if(yes==0)
                    {
                        printf("此时表未创建!不能输出线性表!\n");
                        break;
                    }
                 else
                 {
                     display(&l);
                     break;
                 }
             case 7:if(yes==0)
                    {
                        printf("此时表未创建!不能销毁单链表!\n");
                        break;
                    }
                 else
                 {
                     destroylist(l);
                     printf("单链表已被销毁!\n");
                     break;
                 }
             case 0:exit(1);break;
             default:
                 printf("输入命令错误!请重新输入:\n");
                 break;
         }
     }
 }
 int main(void)
 {
     function();
     return OK;
 }
 

菜鸟也疯狂
2012-10-29 12:46
wu550399048
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2012-10-25
收藏
得分:0 
谢谢  知道了 是l3.same(l1,l2);
怎么创建一个二叉树?明天用
2012-10-29 22:37
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:0 
回复 5楼 wu550399048
可以看看后面的帖子   搜索 过滤下
收到的鲜花
  • wu5503990482012-11-07 22:29 送鲜花  3朵  
2012-10-29 23:37
快速回复:顺序表的一些算法
数据加载中...
 
   



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

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