佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 944|回复: 15

C++遇到难题了!!

[复制链接]
xuan_chew 该用户已被删除
发表于 13-3-2006 05:13 PM | 显示全部楼层 |阅读模式
我正在修读C++,但是现在遇到难题了!!
有谁可以帮我?
问题如下,本人看了数十次,还是不得要领。


Doubly Linked List
For certain application, some operations require being able to move from a node to its predecessor.  This kind of linked-list is usually called as Doubly-Linked List.  This can be easily done by using nodes that contain two links, besides the data part, a forward link next pointing to the successor and a backward link prev pointing to its predecessor.  You may look at the following data structures for a node and a doubly linked-list object with three elements.

The Doubly-Linked List should have at least the following functions;
a)        Insert new element at the Head.
b)        Insert new element at the Tail.
c)        Display data in the object by traversing the list from the last element to the first element.
d)        Display data in the object by traversing the list from the first element to the last element.
e)        Count the number of elements in the list.
f)        Check whether the doubly linked-list is empty
g)        Add a new element after a given element in a doubly linked-list.
h)        Add a new element before a given element in the doubly linked-list.
i)        Modify a given element with a new data in a list object.
j)        Delete a given element in the doubly linked-list.
k)        Search for a specified element in the backward direction.

Imagine this is a learning object, and the purpose of developing this project is to help others people to learn the concept of Doubly Linked-List.  You may create a menu driven system and list down the operations that you have for Doubly Linked-List.  When people choose a particular operation, the system will show step by step how the operation is completed.  For example, to add a new node, you need to show the steps to set its backward and forward links to point to its predecessor and successor, respectively. In fact, the best way is to show it graphically.  
So, you may design the system as creative as possible, bonus marks will be given.  Before that, make sure that you successfully implement the doubly linked-list class, and then only think of how to present to the user.

You can assume no two elements inserted into the list are the same. You can decide the data type of the element you want to store.  For extra effort, you can add in more functions members into the Doubly Linked-List class.
回复

使用道具 举报


ADVERTISEMENT

xuan_chew 该用户已被删除
 楼主| 发表于 14-3-2006 05:06 PM | 显示全部楼层
以下是我尝试做的。
不知道酱可以吗?


#include <iostream>
#include <process.h>

using namespace std;

class doubly
{
        doubly * next;
        doubly * pre;
        int data,ch;
       
public:
        doubly * link(doubly * );
        void traverse (doubly * );
};

doubly * doubly::link(doubly * temp)
{
        doubly * newlink;
        doubly * cur;
        newlink = new doubly;
        cout << endl;
    cout << "PLease enter an positive number :";
        cin >> data;
        cur = temp;
        if(temp == NULL)
        {
                newlink->data = data;
                newlink->pre = NULL;
                newlink->next = NULL;
                temp = newlink;
                return temp;
        }

        else
        {
                while(cur->next != NULL)
                {               
                        cur = cur->next ;
                }
                newlink->data = data;
                newlink->pre = cur;
                newlink->next = NULL;
                cur->next = newlink;       
                return temp;
        }
}


void doubly::traverse(doubly *temp)
{
        int ch;
        doubly * dummy;
        dummy = temp;
        cout << endl;
        cout << "[1] Start From Begainning"<<endl;
    cout << "[2] Start From End" << endl << endl;
        cout << "Enter Your Choice :";
        cin >> ch;
        switch(ch)
        {
        case 1:
                if(dummy != NULL)
                {
                        while(temp->next !=NULL)
                        {
                                cout<<temp->data<<endl;
                                temp=temp->next;
                        }
                        if (temp->next == NULL)
                                        cout<<temp->data<<endl;
                }
                break;
        case 2:
                if(temp != NULL)
                {
                        temp = dummy;
                        while(dummy->next !=NULL)
                        {                       
                                dummy=dummy->next;
                        }
                        while(dummy->pre !=NULL)
                        {
                                cout<<dummy->data<<endl;
                                dummy=dummy->pre;
                               
                        }
                        if (dummy->pre == NULL)
                                        cout<<dummy->data<<endl;
                }
                break;
        }
}

int main()
{
        doubly *first=NULL,d1;
        int choice;
        while(1)
        {
                cout << "\n************** DOUBLY LINK LIST **************\n\n";
        cout << "[1] For Insert" << endl;
        cout << "[2] For Traverse" << endl;
        cout << "[3] For Exit" << endl << endl;
                cout << "Enter Your Choice : ";
                cin>>choice;

                switch (choice)
                {
                case 1:
                        first=d1.link(first);
                        break;               
               
                case 2:
                        d1.traverse(first);
                        break;
                case 3:
                        exit(0);

                }
       
        }
}
回复

使用道具 举报

发表于 15-3-2006 02:26 PM | 显示全部楼层
link list 应该用struct 去create node比较好

然后apply 进class。

[ 本帖最后由 asimo 于 15-3-2006 02:28 PM 编辑 ]
回复

使用道具 举报

xuan_chew 该用户已被删除
 楼主| 发表于 15-3-2006 05:21 PM | 显示全部楼层
原帖由 asimo 于 15-3-2006 02:26 PM 发表
link list 应该用struct 去create node比较好

然后apply 进class。


谢谢。
我去试下。
回复

使用道具 举报

发表于 15-3-2006 07:50 PM | 显示全部楼层
哈哈。。。due date over liao... 不可以再submit le ...
block liao...
回复

使用道具 举报

xuan_chew 该用户已被删除
 楼主| 发表于 15-3-2006 08:56 PM | 显示全部楼层
原帖由 Aduka 于 15-3-2006 07:50 PM 发表
哈哈。。。due date over liao... 不可以再submit le ...
block liao...


过了也想知道答案呀。
那么说来,你也是mmu的fist学生??
回复

使用道具 举报

Follow Us
发表于 17-3-2006 04:55 AM | 显示全部楼层
其实全部答案都在lecture 5 的slides 里面。
回复

使用道具 举报

xuan_chew 该用户已被删除
 楼主| 发表于 17-3-2006 10:14 AM | 显示全部楼层
原帖由 Aduka 于 17-3-2006 04:55 AM 发表
其实全部答案都在lecture 5 的slides 里面。


哦~真的吗?
我的programming很差。
这位仁兄,下次我要向你多学习了。
我是拿ST的。
你呢?
回复

使用道具 举报


ADVERTISEMENT

发表于 17-3-2006 05:50 PM | 显示全部楼层
我吗??
DATA COMM 的,
不用跟我学习,
因为我的programming 一pat 屎。。。
ho ho ho
回复

使用道具 举报

发表于 18-3-2006 01:39 AM | 显示全部楼层
看了你们的Assignment我很惭愧。
这个我根本不懂它到底要什么。

哈哈,我是没用的SENIOR。
回复

使用道具 举报

发表于 18-3-2006 05:10 AM | 显示全部楼层
你们如果学着Link list, queue, stack, binary tree 等OOP

link list 是最重要的, 如果不明白link list就不能明白其他的OOP method

binary tree 最难...

Sample Link list

//Copy and paste the below program to test it.
//Then trace is manually then you can more unterstand.

//This program written by asimo

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

struct Node{
        int item;
        Node *next;
};

Node* inputA(){
        Node *head = NULL;
        head = new Node;
        head->item = 1;
        Node *p = head;
        for (int i=2; i<=5; i++)
        {
                p->next = new Node;
                p = p->next;
                p->item = i;
        }
        p->next = NULL;
        return head;
}

void display1(Node *Z){
        if(Z!=NULL){
                cout<<Z->item<<endl;
                display1(Z->next);
        }
}

void display2(Node *Y){
        if(Y!=NULL){
                display2(Y->next);
                cout<<Y->item<<endl;
        }
}

void insertNewBack(Node *W){
        int i;
        cout<<"Enter the number you want: ";
        cin>>i;

        while(W->next!=NULL)
                W=W->next;
        W->next=new Node;
        W = W->next;
        W->item = i;
        W->next = NULL;
}

void deleteBack(Node *X){
        Node *pre=NULL;
        while(X->next!=NULL){
                pre=X;
                X=X->next;
        }
        pre->next=NULL;
        delete X;
        X=NULL;
}

void saveFile(Node *V,char *F){
       
        ofstream outFile(F);
        for (V; V != NULL; V = V->next)
                outFile << V->item << endl;

        outFile.close();
}

void openFile(char *F){
        Node *head=NULL;
        Node *tail=NULL;
        ifstream inFile(F);
        int nextItem;

        if (inFile >> nextItem){  
                head = new Node;
                head->item = nextItem;
                head->next = NULL;
                tail = head;
   
                while (inFile >> nextItem){
                        tail->next = new Node;
                        tail = tail->next;
                        tail->item = nextItem;
                        tail->next = NULL;
                }
        }
        inFile.close();
}

void MenuA(Node *cur,char *fileName){
        int A;
        cout<<"1: List the content of the linked list";
        cout<<"\n2: List the content of the linked list in reverse order";
        cout<<"\n3: Insert a new item at the end of the list";
        cout<<"\n4: Delete the item at the end of the list";
        cout<<"\n5: Save the contents of the list to a text file";
        cout<<"\n6: Creates a linked list from the data in a text file"<<endl;
        cout<<"\nEnter the choice: ";
        cin>>A;
        if(A==1)
                display1(cur);
        if(A==2)
                display2(cur);
        if(A==3){
                insertNewBack(cur);
                display1(cur);
        }
        if(A==4){
                deleteBack(cur);
                display1(cur);
        }
        if(A==5){
                saveFile(cur,fileName);
                display1(cur);
        }
        if(A==6){
                openFile(fileName);
                display1(cur);
        }
}

void main()
{
        char *fileName = "numbers.txt";       
       
        Node *cur = inputA();
        MenuA(cur,fileName);
}
回复

使用道具 举报

发表于 19-3-2006 02:37 AM | 显示全部楼层
原帖由 asimo 于 18-3-2006 05:10 发表
你们如果学着Link list, queue, stack, binary tree 等OOP

link list 是最重要的, 如果不明白link list就不能明白其他的OOP method

binary tree 最难...

Sample Link list

//Copy and paste the belo ...

为什么不用CLASS而选用STRUCT?
还有,你的IF ELSE应该可以用SWITCH代替。
回复

使用道具 举报

发表于 19-3-2006 03:14 AM | 显示全部楼层
CLASS或STRUCT在没用到private或protected时应该是同样功能的呱...
但是搞OOP的话个人觉得用class较适合

IF ELSE和SWITCH功能都一样的吧,个人觉得SWITCH看起来较整齐,不过没个case都要放break,很麻烦下...哈哈

对于做menu我不太建议用int,因为按到键盘的其它key会造成error的
回复

使用道具 举报

发表于 19-3-2006 03:23 AM | 显示全部楼层
我不大喜欢用switch. struct 和 class 是一样,但是我本人比较喜欢用struct only for link list, stack, queue, binary tree problem problem.
回复

使用道具 举报

发表于 19-3-2006 03:25 AM | 显示全部楼层
原帖由 AdventChildren 于 19-3-2006 03:14 AM 发表
CLASS或STRUCT在没用到private或protected时应该是同样功能的呱...
但是搞OOP的话个人觉得用class较适合

IF ELSE和SWITCH功能都一样的吧,个人觉得SWITCH看起来较整齐,不过没个case都要放break,很 ...


如果我没记错案C++ 有 io throw exception的.
回复

使用道具 举报

发表于 19-3-2006 03:10 PM | 显示全部楼层
原帖由 asimo 于 19-3-2006 03:23 发表
我不大喜欢用switch. struct 和 class 是一样,但是我本人比较喜欢用struct only for link list, stack, queue, binary tree problem problem.

原来是这样。

原帖由 AdventChildren 于 19-3-2006 03:14 发表
CLASS或STRUCT在没用到private或protected时应该是同样功能的呱...
但是搞OOP的话个人觉得用class较适合

IF ELSE和SWITCH功能都一样的吧,个人觉得SWITCH看起来较整齐,不过没个case都要放break,很 ...

通常使用CHAR。
很少用其他。
但是使用CHAR,如果一个人INPUTL:ABCDE,或A BCD也应该算成ERROR。

SWITCH在部分CASE不太好用。
回复

使用道具 举报


ADVERTISEMENT

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

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-9-2024 06:39 AM , Processed in 0.111971 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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