| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 416 人关注过本帖
标题:大家帮我看看这个链表有什么缺陷?PS:删除还没写,继续更新中
只看楼主 加入收藏
fz19910125
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:52
专家分:11
注 册:2010-10-12
结帖率:84.62%
收藏
 问题点数:0 回复次数:3 
大家帮我看看这个链表有什么缺陷?PS:删除还没写,继续更新中
程序代码:
#include "stdafx.h"
#include <iostream>
using namespace std;
#define ZERO 0

class Node
{
public:
    int num;
    Node* next;
    Node* head;
    void creat()
    {
        int count=0;
        cout<<"please input Node Num";
        cin>>count;
        if (count <= ZERO)
        {
            head = NULL;
            return ;
        }
        Node* p1 = new Node;
        Node* p2;
        head = p1;
        for (int i = 0 ; i < count ;++i)
        {
            cout<<i+1<<":";
            cin>>p1->num;
            p2 = p1;
            p1= new Node;
            p2->next = p1;
        }
        delete p1;
        p2->next = NULL;
        return ;

    }
   

    void show()
    {
        Node* tmp = head;
        while(tmp)
        {
            cout<<tmp->num<<endl;
            tmp = tmp->next;
        }
        delete tmp;
    }

    void insert_font()
    {
        Node* tmp_Node = new Node;
        cout<<"input insert of font num:"<<endl;
        cin>>tmp_Node->num;
        tmp_Node->next = head;
        head = tmp_Node;
    }
   

    void insert_real()
    {
        Node* tmp_Node = new Node;
        cout<<"input insert of real num:"<<endl;
        cin>>tmp_Node->num;
        Node*tmp = head;
        while(tmp)
        {
            if (tmp->next == NULL)
            {
                tmp->next = tmp_Node;
                tmp_Node->next = NULL;
                //break;
            }
            tmp = tmp->next;
        }
        delete tmp;
    }

    void insert_center()
    {
        //插入的数据
        Node* tmp_Node = new Node;
        //插入的位置
        int postion;
        cout<<"input insert of center num:"<<endl;
        //输入插入数据
        cin>>tmp_Node->num;
        cout<<"insert postion"<<endl;
        //输入插入位置
        cin>>postion;
        //判断异常
        if (postion <= ZERO)
        {
            return ;
        }
        //创建head头结点副本
        Node* tmp = head;
        //循环跳转节点
        for (int i = 0 ; i < postion-1 ; ++i)
        {
            tmp = tmp->next;
        }
        //插入数据,前一个节点
        Node* tmpePos = tmp;
        Node* tmpeNext = tmp->next;

        //将该节点的next指向新节点
        tmpePos->next = tmp_Node;
       

        //新节点的next指向原始节点的next
        tmp_Node->next = tmpeNext;
    }

};


int _tmain(int argc, _TCHAR* argv[])
{
    Node a;
    a.creat();
    a.insert_font();
    system("cls");
    a.show();
    a.insert_real();
    system("cls");
    a.show();
    a.insert_center();
    system("cls");
    a.show();
    system("pause");
    return 0;
}
大家帮我看看这个链表有什么缺陷?PS:删除还没写,继续更新中


[ 本帖最后由 fz19910125 于 2012-12-6 17:44 编辑 ]
搜索更多相关主题的帖子: color 
2012-12-06 16:59
fz19910125
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:52
专家分:11
注 册:2010-10-12
收藏
得分:0 
程序代码:
    //删除链表
    void delete_node()
    {
        Node* tmpHead = head;
        Node* temp;
        int del_num;
        cout<<"input delete num : ";
        cin>>del_num;
        if (tmpHead->num == del_num)
        {
            temp = tmpHead;
            head = tmpHead->next;
            delete temp;
            cout<<"delete front num "<<endl;
            return ;
        }
        while(tmpHead)
        {
            if (tmpHead->next->num == del_num)
            {
                temp = tmpHead->next;
                tmpHead->next = temp->next;
                delete temp;
                cout<<"delete center num "<<endl;
                return;
            }
            tmpHead = tmpHead->next;
        }
        cout<<"Delete fail "<<endl;
    }
2012-12-06 17:24
fz19910125
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:52
专家分:11
注 册:2010-10-12
收藏
得分:0 
程序代码:
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
using namespace std;
#define ZERO 0

class Node
{
public:
    int num;                    //value
    Node* next;                 //下一个节点
    Node* head;                 //头结点
    void creat();               //创建链表
    void show();                //显示链表
    void insert_front();       //头插入节点
    void insert_real();        //尾插入节点
    void insert_center();      //中间插入节点
    void delete_node();        //删除节点
};

void Node::creat()
{
    int count=0;
    cout<<"please input Node Num";
    cin>>count;
    if (count <= ZERO)
    {
        head = NULL;
        return ;
    }
    Node* p1 = new Node;
    Node* p2;
    head = p1;
    for (int i = 0 ; i < count ;++i)
    {
        cout<<i+1<<":";
        cin>>p1->num;
        p2 = p1;
        p1= new Node;
        p2->next = p1;
    }
    delete p1;
    p2->next = NULL;
    return ;

}

void Node::show()
{
    Node* tmp = head;
    while(tmp)
    {
        cout<<tmp->num<<endl;
        tmp = tmp->next;
    }
    delete tmp;
}

void Node::insert_front()
{
    Node* tmp_Node = new Node;
    cout<<"input insert of front num:"<<endl;
    cin>>tmp_Node->num;
    tmp_Node->next = head;
    head = tmp_Node;
}

void Node::insert_center()
{
    //插入的数据
    Node* tmp_Node = new Node;
    //插入的位置
    int postion;
    cout<<"input insert of center num:"<<endl;
    //输入插入数据
    cin>>tmp_Node->num;
    cout<<"insert postion"<<endl;
    //输入插入位置
    cin>>postion;
    //判断异常
    if (postion <= ZERO)
    {
        return ;
    }
    //创建head头结点副本
    Node* tmp = head;
    //循环跳转节点
    for (int i = 0 ; i < postion-1 ; ++i)
    {
        tmp = tmp->next;
    }
    //插入数据,前一个节点
    Node* tmpePos = tmp;
    Node* tmpeNext = tmp->next;
    //将该节点的next指向新节点
    tmpePos->next = tmp_Node;

    //新节点的next指向原始节点的next
    tmp_Node->next = tmpeNext;
}


void Node::insert_real()
{
    Node* tmp_Node = new Node;
    cout<<"input insert of real num:"<<endl;
    cin>>tmp_Node->num;
    Node*tmp = head;
    while(tmp)
    {
        if (tmp->next == NULL)
        {
            tmp->next = tmp_Node;
            tmp_Node->next = NULL;
            //break;
        }
        tmp = tmp->next;
    }
    delete tmp;
}



void Node::delete_node()
{
    Node* tmpHead = head;
    Node* temp;
    int del_num;
    cout<<"input delete num : ";
    cin>>del_num;
    if (tmpHead->num == del_num)
    {
        temp = tmpHead;
        head = tmpHead->next;
        delete temp;
        cout<<"delete front num "<<endl;
        return ;
    }
    while(tmpHead)
    {
        if (tmpHead->next->num == del_num)
        {
            temp = tmpHead->next;
            tmpHead->next = temp->next;
            delete temp;
            cout<<"delete center num "<<endl;
            return;
        }
        tmpHead = tmpHead->next;
    }
    cout<<"Delete fail "<<endl;
}
2012-12-06 17:35
fz19910125
Rank: 2
来 自:上海
等 级:论坛游民
帖 子:52
专家分:11
注 册:2010-10-12
收藏
得分:0 
更新一下删除代码
程序代码:
void Node::delete_node()
{
    Node* tmpHead = head;
    Node* temp;
    int del_num;
    cout<<"input delete num : ";
    cin>>del_num;
    if (tmpHead->num == del_num)
    {
        temp = tmpHead;
        head = tmpHead->next;
        delete temp;
        cout<<"delete front num "<<endl;
        return ;
    }
    while(tmpHead)
    {
        //判断下个节点是否为尾节点*********
        if(tmpHead->next == NULL)
        {
            break;
        }
        //**********************************
        if (tmpHead->next->num == del_num)
        {
            temp = tmpHead->next;
            tmpHead->next = temp->next;
            delete temp;
            cout<<"delete center num "<<endl;
            return;
        }
        tmpHead = tmpHead->next;
    }
    cout<<"Delete fail "<<endl;
}

2012-12-06 17:41
快速回复:大家帮我看看这个链表有什么缺陷?PS:删除还没写,继续更新中
数据加载中...
 
   



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

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