| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 930 人关注过本帖
标题:关于c++和链表的小问题
只看楼主 加入收藏
amy073
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-9-30
收藏
 问题点数:0 回复次数:5 
关于c++和链表的小问题
因为是刚开始自学数据结构,所以关于链表的东西很多不明白。求助各位高手。
看的是mark allen weiss的那本数据结构与算法,是c语言版的,所以链表是建立在结构struct上的,那如果我要用c++的类来做,是不是只要把链表的相关函数封装进类里面,那节点的建立怎么办呢?
求助具体用c++建立链表的方法
搜索更多相关主题的帖子: 链表 
2008-09-30 18:14
很远的那颗星
Rank: 2
等 级:新手上路
威 望:4
帖 子:544
专家分:0
注 册:2008-6-30
收藏
得分:0 
头文件
//SeqList.h
#ifndef SEQLIST_H
#define SEQLIST_H


#include<iostream>
#include<string>
using namespace std;


class seqlist
{
public:
    void setlist(seqlist *L,int n);
    void freelist(seqlist *L);
    int listsize(seqlist *L);
    bool listempty(seqlist *L);
    bool listfull(seqlist *L);
    int getdata(seqlist *L,int pos);
    int locate(seqlist *L,int item);
    void setdata(seqlist *L,int item,int pos);
    void insert(seqlist *L,int item,int pos);
    void insertrear(seqlist *L,int item);
    void deletedata(seqlist *L,int pos);
    void clearlist(seqlist *L);
    void bubble(seqlist *L);
private:
    int* data;
    int max;
    int size;
};


#endif SEQLIST_H

Fighting~~~~~~~~
2008-09-30 19:35
很远的那颗星
Rank: 2
等 级:新手上路
威 望:4
帖 子:544
专家分:0
注 册:2008-6-30
收藏
得分:0 
实现部分
#include "SeqList.h"
void seqlist::setlist(seqlist *L,int n)
{
    int *p=new int[n];
    if(p==NULL)
        cout<<"locate memory fail!"<<endl;
    L->data=p;
    L->max=n;
    L->size=0;
}

void seqlist::freelist(seqlist *L)
{
    delete [] L->data;
    L->size=0;
    L->max=0;
}

int seqlist::listsize(seqlist *L)
{
    return L->size;
}

bool seqlist::listempty(seqlist *L)
{
    if(L->size==0)
        return true;
    else
        return false;
}

bool seqlist::listfull(seqlist *L)
{
    if(L->size==L->max)
        return true;
    else
        return false;
}

int seqlist::getdata(seqlist *L,int pos)
{
    if(pos>=L->size&&pos<0)// the range is in 0 ~~ L->size-1 ;
    {
        cout<<"the fetch is out of reach!"<<endl;
        exit(1);
    }
    else
        return L->data[pos];
}

int seqlist::locate(seqlist *L,int item)
{
    int i=0;
    while(i<=L->size-1&&item!=L->data[i])
    {
        i++;
    }
    if(i>=0&&i<=L->size-1)
        return i;
    else
    {
        cout<<"can't find any!"<<endl;
        exit(1);
    }
}

void seqlist::setdata(seqlist *L,int item,int pos)
{
    if(pos>=0&&pos<=L->size-1)
    {
        L->data[pos]=item;
    }
    else
    {
        cout<<"can't set data!";
        exit(1);
    }
}

void seqlist::insert(seqlist *L,int item,int pos)
{
    if(L->size==L->max)
    {
        cout<<"can't insert,cause it's full already!"<<endl;
        exit(1);
    }
    if(pos<0||pos>L->size)
    {
        cout<<"can't insert,cause it's in a wrong position!"<<endl;
        exit(1);
    }
    for(int i=L->size;i>pos;i--)
    {
        L->data[i]=L->data[i-1];
    }
    L->data[pos]=item;
    L->size++;
}

void seqlist::insertrear(seqlist *L,int item)
{
    if(L->size==L->max)
    {
        cout<<"can't insert from the rear,cause it's full already!"<<endl;
        exit(1);
    }
    else
    {
        L->data[L->size]=item;
    }
    L->size++;
}

void seqlist::deletedata(seqlist *L,int pos)
{
    if(pos<0||pos>L->size-1)
    {
        cout<<"can't delete,cause it doesn't exist!"<<endl;
        exit(1);
    }
    if(L->size==0)
    {
        cout<<"no element can be delete,cause nobody inside!"<<endl;
        exit(1);
    }
    for(int i=pos+1;i<L->size;i++)
    {
        L->data[i-1]=L->data[i];
    }
    L->size--;
}

void seqlist::clearlist(seqlist *L)
{
    L->size=0;
}

void seqlist::bubble(seqlist *L)
{
    if(L->size==0)
    {
        cout<<"there is no element inside,so no bubble!"<<endl;
        exit(1);
    }
    else
    {
        for(int i=0;i<L->size-1;i++)
            for(int j=i+1;j<L->size;j++)
            {
                if(L->data[i]>L->data[j])
                {
                    int temp=L->data[i];
                    L->data[i]=L->data[j];
                    L->data[j]=temp;
                }
            }
    }
}

Fighting~~~~~~~~
2008-09-30 19:36
amy073
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-9-30
收藏
得分:0 
虽然还有点看不懂,不过,嗯,真的很感谢,我会努力看懂的~
2008-09-30 23:11
shuaiqi168
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-10-1
收藏
得分:0 
大哥门教教我
我编好的程序怎么让他在电脑上执行啊
2008-10-01 09:24
shuaiqi168
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2008-10-1
收藏
得分:0 
大哥门教教我
程序放在记事本里,拜托.感谢..........这是我QQ(601145688)
2008-10-01 09:26
快速回复:关于c++和链表的小问题
数据加载中...
 
   



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

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