XiaoHui.net 笑汇程序员论坛首页
工作并快乐着,职业并休闲着
寻梦的岁月从不言辛苦几许,
不问收获几多……
» 游客:  申请新用户 | 登录 | 会员 | 统计 | 帮助 » XiaoHui.Net 笑汇程序员论坛 | 纯文字版 | 全站索引 | XiaoHui.com


[其他] 第一次来这,有一道题不会做,哪位前辈能帮忙下?

RSS 订阅当前论坛  

上一主题 下一主题
     

标题: [其他] 第一次来这,有一道题不会做,哪位前辈能帮忙下?  
 
chriscc
小水手
Rank: 1



UID 25367
精华 0
积分 2
帖子 2
阅读权限 10
注册 2006-11-15
状态 离线
第一次来这,有一道题不会做,哪位前辈能帮忙下?

提供两个容器类,分别叫作ArrayList和LinkedList。 ArrayList采用数组存储元素,而LinkedList采用链表存储元素。这两个类可以实现以下方法:
getAt( )                //获得指定位置的元素
setAt( )                //设置指定位置的元素
insertAt( )                //在指定位置插入元素
removeAt( )        //删除指定位置的元素
find( )                //传入一个元素值,查找第一个符合该值的元素的
                                //位置
count( )                //容器中的元素数量
reverse( )                //将容器中的元素的反向
        注:
                1。上述方法的参数和返回值自行设定
                2。为实现类的功能,可以自行增加其他方法和属性
编写一个全局函数,该函数可以同样的代码访问ArrayList和LinkedList的对象
允许向ArrayList和LinkedList的不同实例存放不同类型的数据单元,但是同一个ArrayList或LinkedList实例只存贮同一类型的数据单元。
2006-11-15 22:03#1
查看资料  Blog  发短消息  顶部
 
蓝魔
小水手
Rank: 1



UID 25080
精华 0
积分 5
帖子 5
阅读权限 10
注册 2006-11-9
来自 xinjiang
状态 离线
这是我的程序,你大概可以看一下了 
链式存储
# include "stdio.h"
# define MAXSIZE  100
struct node
{
        int data;
        struct node *next;
};
struct node l;
void create(struct node *head)
{
        struct tail;
        struct node * p;
        int x;
        head=tail=(struct node*)malloc(sizeof(struct node));
        head->next=Null;
        printf("please input the data:");
        scanf("%4d",&x);
        while (x!=0)
        {
                p=(struct node *)malloc(sizeof(struct node));
                p->data=x;
                p->next=Null;
                tail->next=p;
                printf("please input the data:");
                scanf("%4d",&x);
        }
}
void creatlist(struct node *head)
{
        int x;
        struct node *p;
        head=(struct node *)malloc(sizeof(struct node));
        head->next=Null;
        printf("please input the data:");
        scanf("%4d",&x);
        while(x!=0)
        {
                p=(struct node *)malloc(sizeof(struct node));
                p->data=x;
                p->next=head->next;
                head->next=p;
                printf("please input the data:"):
                scanf("%4d",&x);
        }
}
int listinsert(int i, int x,struct *head)
{
        int count=0;
        struct node t;
        struct node *p;
        p=head;
        while(count<i-1  &&  p->next!=Null)
        {
                p=p->next;
                count++;
        }
        if(count==i-1)
        {
                t=(struct node *)malloc(sizeof(struct node));
                t->data=x;
                t->next=p->next;
                p->next=t;
                return 1;
        }
        else return 0;
}
int deletelist(struct node *head ,int i)
{
        int count=0;
        struct node *p,*q;
        p=head;
        while(count<i-1  &&  p->next!=Null)
        {
                p=p->next;
                count++;
        }
        if(count = i-1)
        {
                q=p->next;
                p->next=p->next->next;
                free(q);
                return 1;
        }
        else return 0;
}
main()
{
        int x;
        int a;
        int i,j,k;
        printf("if you want to inseart the data please press 1\n if you want to delete the data please press 2\n")
]# include "stdio.h"
# define MAXSIZE  100
struct node
{
        int data;
        struct node *next;
};
struct node l;
void create(struct node *head)
{
        struct tail;
        struct node * p;
        int x;
        head=tail=(struct node*)malloc(sizeof(struct node));
        head->next=Null;
        printf("please input the data:");
        scanf("%4d",&x);
        while (x!=0)
        {
                p=(struct node *)malloc(sizeof(struct node));
                p->data=x;
                p->next=Null;
                tail->next=p;
                printf("please input the data:");
                scanf("%4d",&x);
        }
}
void creatlist(struct node *head)
{
        int x;
        struct node *p;
        head=(struct node *)malloc(sizeof(struct node));
        head->next=Null;
        printf("please input the data:");
        scanf("%4d",&x);
        while(x!=0)
        {
                p=(struct node *)malloc(sizeof(struct node));
                p->data=x;
                p->next=head->next;
                head->next=p;
                printf("please input the data:"):
                scanf("%4d",&x);
        }
}
int listinsert(int i, int x,struct *head)
{
        int count=0;
        struct node t;
        struct node *p;
        p=head;
        while(count<i-1  &&  p->next!=Null)
        {
                p=p->next;
                count++;
        }
        if(count==i-1)
        {
                t=(struct node *)malloc(sizeof(struct node));
                t->data=x;
                t->next=p->next;
                p->next=t;
                return 1;
        }
        else return 0;
}
int deletelist(struct node *head ,int i)
{
        int count=0;
        struct node *p,*q;
        p=head;
        while(count<i-1  &&  p->next!=Null)
        {
                p=p->next;
                count++;
        }
        if(count = i-1)
        {
                q=p->next;
                p->next=p->next->next;
                free(q);
                return 1;
        }
        else return 0;
}
main()
{
        int x;
        int a;
        int i,j,k;
        printf("if you want to inseart the data please press 1\n if you want to delete the data please press 2\n")
}



线性存储
# include "stdio.h"
# define MAXSIZE 100
struct list
{
        int elem[MAXSIZE];
        int nuninlist;
        };


struct list l;


void init()
{
l.numinlist=0;
}


void append(int x)
{
        l.elem[l.numinlist]=x;
        l.numinlist++;
}


void Creatlist()
{
        int y;
         printf("please input the first data");
        scanf("%4d",&y);
         while (y!=0 && l.numinlist<MAXSIZE)
         {
                 append(y);
                 printf("\n please input the second data:\n ");
                 scanf("4d",&y);
         }
}


void insert(int i, int x)
{
      int j;
      if(i<0 ||i>l.numinlist || l.numinlist>=MAXSIZE)
                return;
      for(j=l.numinlist-1;j>i;j--)
      l.elem[j+1]=l.elem[j];
      l.elem=x;
      l.numinlist++;


}



void delete(int i)
{
        int j;
        if(l.numinlist==0 ||i<0 || i>l.numinlist)
                return;
        for(j=i+1;j<l.numinlist-1;j++)
        l.elem[j-1]=l.elem[j];
        l.numinlist--;

}


int inserch(int x)
{
        int i;
        for(i=0;i<l.numinlist&&l.elem!=x;i++)
        if(i<l.numinlist)
                return 0;
        else return -1;
}



man()
{
        int i,a,j,x;
        init();
        Creatlist();
        printf("\n if you want to insearch the data ,please press 1;\n if you want to delete the data please press 2;\n if you want to search the data please press 3;\n");
        scanf("%4d",&a);
        switch(a)
        {
                case 1:
                        printf("\n please input the position and the data you want to insert:");
                        scanf("%4d,%4d",&i,&x);
                        insert(i,x);
                        break;
                case 2:
                        printf("\n please input the position you want to delete");
                        scanf("%4d",&j);
                        delete(i);
                        break;
                case 3:
                        printf("\n please input the data you want to search:");
                        scanf("%4d",x);
                        inserch(x);
                        break;
                default:printf ("\n error!\n");
        }

}


志当存高远
2006-11-17 16:09#2
查看资料  Blog  发短消息  顶部
 
chriscc
小水手
Rank: 1



UID 25367
精华 0
积分 2
帖子 2
阅读权限 10
注册 2006-11-15
状态 离线
非常感谢!我试试看看
2006-11-18 11:07#3
查看资料  Blog  发短消息  顶部
     


  可打印版本 | 推荐给朋友 | 订阅主题 | 收藏主题  


 


所有时间为 GMT+8, 现在时间是 2008-11-23 04:09 Powered by Discuz! 4.1.0 清除 Cookies - XiaoHui.Net 笑汇程序员论坛 - Archiver