QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 801|回复: 6

c++ program

[复制链接]
发表于 2003-11-2 15:57:58 | 显示全部楼层 |阅读模式
the first file intSLList.h

#ifdef INT_LINKED_LIST
#define INT_LINKED_LIST

class IntNode{
public:
        int info;
        IntNode *next;
        IntNode(int el,IntNode *ptr = 0){
            info = el; next = ptr;
        }
};

class IntSLList{
public:
        IntSLList(){
              head = tail = 0;
        }
        ~IntSLList();
        int isempty(){
                return head == 0;
        }
        void addToHead(int);
        void addToTail(int);
        int deleteFromHead();
        int deleteFromTail();
    void deleteNode(int);
        bool is IsInList(int) const;
       
private:
        IntNode *Head,*tail;
};

#endif

The second file intSLList.cpp

#include <iostream>
#include "intSLList.h"

using namespace std;

IntSLList::~IntSLList(){
    for(IntNode *p;!isempty();){
                p = head -&gt; next;
                delete head;
                head = p;
        }
}
void IntSLList::addToHead(int el){
    head = new IntNode(el,head);
    if (tail == 0)
                tail = head;
}

void IntSSList::addToTail(int el){
    if (tail != 0){
                tail -&gt; next = new IntNode(el);
                tail = tail -&gt; next;
        }
        else{
                head = tail = new IntNode(el);
        }
}

int IntSLList::deleteFromHead(){
    int el = head -&gt; info;
        IntNode *tmp = head;
        if (head == tail)
                head = tail = 0;
        else head = head -&gt; next;
        delete tmp;
        return el;
}
int IntSLList::deleteFromTail(){
        int el = tail -&gt; info;
        if (head == tail){
                delete head;
                head = tail = 0;
        }
        else{
                IntNode *tmp;
                for (tmp = head;tmp -&gt; next != tail;tmp = tmp -&gt; next)
                        delete tail;
                tail = tmp;
                tail -&gt; next = 0;
        }
        return el;
}
void IntSLList::deleteNode(int el){
        if (head != 0){
                if (head == tail &amp;&amp; el == head-&gt;info){
                        delete head;
                        head = tail = 0;
                }
                else if (el == head -&gt; info){
                        IntNode *tmp = head -&gt; next;
                        head = head -&gt; next;
                        delete tmp;
                }
                else{
                        IntNode *pred,*tmp;
                        for (pred = head;tmp = head-&gt;next; tmp != 0,&amp;&amp;(tmp-&gt;info == el);
                                pred = pred -&gt; next,tmp = tmp -&gt; next){
                                        if (tmp != 0)
                                                pred -&gt; next = tmp -&gt; next;
                                        if (tmp == tail)
                                                tail = pred;
                                        delete tmp;
                                }
                        }
                }
        }
}

bool IntSLList::isIntLList(int el) const{
        IntNode *tmp;
        for (tmp = head;tmp != 0&amp;&amp; !(tmp -&gt; info == el);tmp = tmp -&gt; next)
                ;
        return tmp != 0;
}

the bad information:
magicboy@MagicHackComputer lianbiao]$ g++ intSLList.cpp intSLList.h -o lianbiao
intSLList.cpp:6: syntax error before `::' token
intSLList.cpp:10: ISO C++ forbids declaration of `head' with no type
intSLList.cpp:10: `p' was not declared in this scope
intSLList.cpp:11: parse error before `}' token
intSLList.cpp:13: syntax error before `::' token
intSLList.cpp:19: syntax error before `::' token
intSLList.cpp:22: ISO C++ forbids declaration of `tail' with no type
intSLList.cpp:22: base operand of `-&gt;' is not a pointer
intSLList.cpp:23: parse error before `}' token
intSLList.cpp:29: syntax error before `::' token
intSLList.cpp:31: syntax error before `*' token
intSLList.cpp:38: syntax error before `::' token
intSLList.cpp:42: ISO C++ forbids declaration of `head' with no type
intSLList.cpp:42: redefinition of `int head'
intSLList.cpp:10: `int head' previously defined here
intSLList.cpp:43: parse error before `}' token
intSLList.cpp:46: syntax error before `-&gt;' token
intSLList.cpp:46: ISO C++ forbids declaration of `tmp' with no type
intSLList.cpp:46: base operand of `-&gt;' is not a pointer
intSLList.cpp:46: parse error before `)' token
intSLList.cpp:48: ISO C++ forbids declaration of `tail' with no type
intSLList.cpp:48: redefinition of `int tail'
intSLList.cpp:22: `int tail' previously defined here
intSLList.cpp:49: syntax error before `-&gt;' token
intSLList.cpp:53: syntax error before `::' token
intSLList.cpp:57: ISO C++ forbids declaration of `head' with no type
intSLList.cpp:57: redefinition of `int head'
intSLList.cpp:42: `int head' previously defined here
intSLList.cpp:58: parse error before `}' token
intSLList.cpp:61: ISO C++ forbids declaration of `head' with no type
intSLList.cpp:61: redefinition of `int head'
intSLList.cpp:57: `int head' previously defined here
intSLList.cpp:61: base operand of `-&gt;' is not a pointer
intSLList.cpp:62: parse error before `delete'
intSLList.cpp:66: ISO C++ forbids declaration of `tmp' with no type
intSLList.cpp:66: redefinition of `int tmp'
intSLList.cpp:46: `int tmp' previously defined here
intSLList.cpp:66: base operand of `-&gt;' is not a pointer
intSLList.cpp:66: syntax error before `!=' token
intSLList.cpp:67: ISO C++ forbids declaration of `pred' with no type
intSLList.cpp:67: base operand of `-&gt;' is not a pointer
intSLList.cpp:67: ISO C++ forbids declaration of `tmp' with no type
intSLList.cpp:67: redefinition of `int tmp'
intSLList.cpp:66: `int tmp' previously defined here
intSLList.cpp:67: base operand of `-&gt;' is not a pointer
intSLList.cpp:67: parse error before `)' token
intSLList.cpp:79: syntax error before `::' token
intSLList.cpp:81: syntax error before `!=' token
intSLList.cpp:81: ISO C++ forbids declaration of `tmp' with no type
intSLList.cpp:81: redefinition of `int tmp'
intSLList.cpp:67: `int tmp' previously defined here
intSLList.cpp:81: base operand of `-&gt;' is not a pointer
intSLList.cpp:81: parse error before `)' token
g++: compilation of header file requested

why???
IntSLList::~IntSLList() is error!!!why not??? :-)  :-)  :-)  :-)
 楼主| 发表于 2003-11-2 15:59:00 | 显示全部楼层
this is data structure
回复

使用道具 举报

 楼主| 发表于 2003-11-2 16:00:42 | 显示全部楼层
as soon as quickly,beacase it is my homework.
回复

使用道具 举报

发表于 2003-11-2 21:43:49 | 显示全部楼层
有几个是比较明显的错误:
1)IntSLList类里面定义的private 变量Head改成head,因为你后面用的时候都是head
所以有后面那个->说BASE不是POINTER。
感觉代码贴上来以后有很多地方应该跟你编译的时候不一样了,也许是贴的时候出了一些错吧;应该界面没有什么大问题,都是一些细节的错误。自己先好好找找吧。构造一个链表还是很简单得
要不再把源码贴一次。有几个地方看得不太清楚。
回复

使用道具 举报

 楼主| 发表于 2003-11-2 21:57:18 | 显示全部楼层
Head change into head???
I have not!!!! :-(
回复

使用道具 举报

发表于 2003-11-3 10:25:46 | 显示全部楼层
IntSLList类里面定义的private 变量Head,是不是笔误还是什么啊???
要不把愿代码好好贴一下吧。
或者直接用STL里面的LIST容器,嘿嘿
#include&lt;list&gt;
#include<iostream>
using namespace std;
int main()
{
list<int> object;
}
回复

使用道具 举报

 楼主| 发表于 2003-11-5 22:32:04 | 显示全部楼层
i didn't like use to stl.because i am student.if i worked,i like stl.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-11-9 10:48 , Processed in 0.041758 second(s), 16 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表