回 帖 发 新 帖 刷新版面

主题:[讨论]求助` 刚学数据结构`谁能发个链表的初始化`谢

struct SqList
{
ElemType *elem;
int length;
int listsize;
};


void initlist(SqList &L) <-----这行报错:变量列表语法错误
{
L.elem=(ElemType*)malloc(list_init_size*sizeof(ElemType));
if(L.elem)
exit(OVERFLOW);
L.length=0;
L.listsize=list_init_size;
}

回复列表 (共4个回复)

沙发

我的是这样编写的,可以在我的电脑上运行,只能强调在我的电脑上运行,因为我也是学数据结构半年而已。供你参考一下吧
#include<iostream.h>
#include<stdlib.h>
typedef int ElemType;
struct List{
    ElemType *list;
    int size;
    int MaxSize;
};
void InitList(List &L)
{
    L.MaxSize=10;
    L.list=new ElemType[L.MaxSize];
    if(L.list==NULL){
        cout<<"动态可分配的存储空间用完,退出运行!"<<endl;
        exit(1);
    }
    L.size=0;
}

板凳

为了复习迎接考试,自己瞎写的,欢迎指正修改,为了不被人嘲笑去掉了自己写的解释-_-。此类包含所有的基本操作,我做成头文件,但是由于编译的原因发现实用时模板并没有想象中的好用,清华的书里面为了减少操作函数个数,新加了一个游标类,我觉得那个东西没用,就没写
/****************************************************************/
/*    Class:        CListNode,CLinkList                  
/*    Function:    单链表结点类模板,带头节点,尾指针的单链表类模板          
/*    Created:                    
/*    Updated:                     
/*    Author:                                 
/****************************************************************/
template<class T> class CLinkList;
template<class T> class CListNode
{
    friend class CLinkList<T>;
    private:
        T data;
        CListNode<T>* next;
    public:
        CListNode<T>()
        {
            data=NULL;
            next=NULL;
        };
        CListNode<T>(T elem)
        {
            data=elem;
            next=NULL;
        }
};
template<class T> class CLinkList
{
    private:
        int length;
        CListNode<T>* head;
        CListNode<T>* tail;
    public:
        CLinkList<T>():length(0)
        {
            head=new CListNode<T>[1];
            head->data=NULL;
            tail=head;
        }
        CLinkList<T>(T* array,int size)
        {
            InitList(array,size);
        }
        CLinkList<T>(CLinkList<T> &list)
        {
            *this=list;
        }
        CLinkList<T>& operator=(CLinkList<T> &list)
        {
            InitList();
            int len=list.GetLength();
            CListNode<T>* pTemp=list.head->next;
            T *ch=new T[len];
            for(int i=0;i<len;i++)
            {
                ch[i]=pTemp->data;
                pTemp=pTemp->next;
            }
            InitList(ch,len);
            return(*this);
        }
        ~CLinkList<T>()
        {
            CListNode<T>* ptemp;
            while(head!=NULL)
            {
                ptemp=head;
                head=head->next;
                delete [] ptemp;
            }
        }    
        void InitList()
        {
            length=0;
            head=new CListNode<T>;
            tail=head;
        }
        void InitList(T *array,int size)
        {
            InitList();
            for(int i=0;i<size;i++)
            {
                Insert(i+1,array[i]);
            }
        }
        void Create()
        {
            T m_TempData;
            cout<<"请输入要建立链表的数据:(输入0退出)";
            cin>>m_TempData;
            int i=1;
            while(m_TempData!=0&&m_TempData!='0')
            {
                Insert(i,m_TempData);
                i++;
                cout<<"请输入要建立链表的数据:";
                cin>>m_TempData;                
            }
        }
        void ClearList()
        {
            CListNode<T> *node=head->next;;
            while(node)
            {
                node->data=NULL;
                node=node->next;
            }
            
        }

3 楼

        bool IsEmpty()
        {
            if(head->next==NULL)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        CListNode<T>* GetElem(const int pos,T& elem)const
        {            
            CListNode<T>* p_TempNode=head->next;
            if(pos<1||pos>length)
            {
                cout<<"数据读取位置错误"<<endl;
                return(NULL);
            }
            for(int i=1;i<pos;i++)
            {
                p_TempNode=p_TempNode->next;
            }
            elem=p_TempNode->data;
            return(p_TempNode);
        }
        CListNode<T>* Insert(const int pos,const T elem)
        {
            int i=0;            
            CListNode<T>* p_TempNode=head;
            if(pos<1||pos>length+1)
            {
                cout<<"数据插入位置错误"<<endl;
                return(NULL);
            }
            else
            {
                while(p_TempNode&&pos>i+1)
                {
                    p_TempNode=p_TempNode->next;
                    i++;
                }
                CListNode<T>* p_NewNode=new CListNode<T>(elem);
                p_NewNode->next=p_TempNode->next;
                p_TempNode->next=p_NewNode;
                length++;
                if(tail->next!=NULL)
                {
                    tail=tail->next;
                }
                return(p_NewNode);
            }
        }
        CListNode<T>* Remove(const int pos,T &data)
        {
            int i=0;            
            CListNode<T>* p_TempNode=head;
            CListNode<T>* p_DelNode;
            if(pos<1||pos>length)
            {
                cout<<"数据插入位置错误"<<endl;
                return(NULL);
            }
            else
            {
                while(p_TempNode&&pos>i+1)
                {
                    p_TempNode=p_TempNode->next;
                    i++;
                }
                data=p_TempNode->data;
                p_DelNode=p_TempNode->next;
                p_TempNode->next=p_DelNode->next;
                delete [] p_DelNode;
                length--;
            }
            return(p_DelNode);
        }

4 楼


        bool SetElem(int pos,const T e)//对pos位置数据赋值,位置错误则返回false
        {
            if(pos<1||pos>length)
            {
                return(false);
            }
            else
            {
                CListNode<T>* p_TempNode=head->next;
                for(int i=1;i<pos;i++)
                {
                    p_TempNode=p_TempNode->next;
                }
                p_TempNode->data=e;
                return(true);
            }
        }
        int InitLength()
        {
            CListNode<T>* p_TempNode=head->next;
            while(p_TempNode!=NULL)
            {
                length++;
                p_TempNode=p_TempNode->next;                    
            }
        }
        int GetLength()
        {
            return(length);
        }
        CListNode<T>* GetHead()
        {
            return(head);
        }
        CListNode<T>* GetTail()
        {
            return(tail);
        }
        void Reverse()
        {
            tail=head->next;
            CListNode<T> *rear,*prior,*pos;
            prior=NULL;
            pos=head->next;
            while(pos!=NULL)
            {
                rear=pos->next;
                pos->next=prior;
                prior=pos;
                pos=rear;
            }
            head->next=prior;
        }
        void Traverse()
        {
            if(length==0)
            {
                cout<<"链表为空,无法遍历"<<endl;
            }
            else
            {
                cout<<"链表现有数据为:";
                CListNode<T>* pTemp=head->next;
                while(pTemp->next!=NULL)
                {
                    cout<<pTemp->data<<"->";
                    pTemp=pTemp->next;
                }
                cout<<pTemp->data<<endl;
            }
        }
        int LocateElem(const T e) //返回顺序表L中第1个与e相等的数据位置
        {
            int i=1;
            CListNode<T>* pTemp=head->next;
            while(pTemp)
            {
                if(pTemp->data==e)
                {
                    return(i);
                }
                else
                {
                    i++;
                    pTemp=pTemp->next;
                }
            }
            return(0);
        }
        void Union(CLinkList<T> list1,CLinkList<T> list2)
        {
            int data;
            for(int i=1;i<=list2.GetLength();i++)
            {
                list2.GetElem(i,data);
                if(!list1.LocateElem(data))
                {
                    list1.Insert(list1.GetLength()+1,data);
                }
            }
            *this=list1;
        }

};

我来回复

您尚未登录,请登录后再回复。点此登录或注册