不懂*&和*区别的可以进来看看

     为了更好的理解*&和*的区别,决定自己动手写个程序理解理解。首先就想到了链表。现在把代码贴出来和大家一起理解。大家特别应该注意当中*&的用法和*。有些地方用了*&,有的地方只用了*。大家可以去理解理解,如果有困难,可以给我留言,我的博客,我每天基本上都会来

看看的。和大家一起提高C++技能。/*************************************************************************** *FileName:InsertSortList.cpp * *Author:FlySky Young * *Version:1.0 * *Date:Nov/14/2010 * *Description:Linked list. * ****************************************************************************/ #include <iostream> #include <stdlib.h> using namespace std; //结点类 class Node { friend class List; //友员类 public: Node() { this->next = NULL; } private: int value; Node *next; }; //链表类 class List { public: List(); void createToHead(Node *&head); //头插法建立链表 void InsertNode(Node *head, int location, int value); //在location位置插入value void deleteNodeLocation(Node *head, int location); //删除链表中location位置的结点 void showList(Node *head); //遍历链表 private: int length; }; //构造函数 List::List() { length = 0; } //头插法建立链表 void List::createToHead(Node *&head) { Node *tail = NULL; Node *temp = NULL; //设立头结点,没有实际意义的值,是为了以后的操作 head = new Node(); length = length + 1; head->value = 0; tail = head; while (tail->value != -1) { temp = tail; tail = new Node(); //新建一个结点 length++; temp->next = tail; cin>>tail->value; } //判断输出是否为1,如果为1,去掉最后一个结点 if (tail->value == -1) { //是否为头结点 if (tail == head) { delete head; cout<<"No List!"<<endl; length = 0; exit(0); } else { //在链表尾部加入结点 temp->next = NULL; delete tail; tail = temp; length = length - 1; } } return; } //插入结点 void List::InsertNode(Node *head, int location, int value) { int tempValue = 0; Node *ToInsertNode = NULL; //判断location的正确性 if (location > length || location < 0) { cout<<"Wrong location!"<<endl; exit(0); } while (tempValue != location) { head = head->next; tempValue++; } if (location != length) { ToInsertNode = new Node(); ToInsertNode->next = head->next; ToInsertNode->value = value; head->next = ToInsertNode; } else { ToInsertNode = new Node(); ToInsertNode->next = NULL; ToInsertNode->value = value; head->next = ToInsertNode; } return; } //删除链表中location的结点 void List::deleteNodeLocation(Node *head, int location) { int temp = 0; Node *pre = NULL; //一个临时变量 //判断location的正确性 if (location > length || location < 0) { cout<<"Wrong location!"<<endl; exit(0); } while (temp != location) { pre = head; //记录head当前的地址 head = head->next; temp++; } if (location != length) { pre->next = head->next; delete head; length = length - 1; } else { delete head; pre->next = NULL; length = length - 1; } return; } //遍历链表 void List::showList(Node *head) { head = head->next; while (head != NULL) { cout<<head->value<<" "; head = head->next; } cout<<"链表长度:"<<length<<endl; } int main() { List list; int location; Node *head = NULL; list.createToHead(head); cout<<"请输入插入位置的值:"; cin>>location; //输入插入位置 list.InsertNode(head, location, 10); list.showList(head); cout<<endl; cout<<"请输入要删除的位置:"; cin>>location; list.deleteNodeLocation(head, location); list.showList(head); } 

转载于:https://www.cnblogs.com/JPAORM/archive/2010/11/16/2509915.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值