C++创建链表
来源:互联网 发布:mysql中的日期函数 编辑:程序博客网 时间:2024/06/14 17:32
首先是Node.h#pragma once#include<iostream>using namespace std;class Node{public: Node() { data = 0; next = NULL; }; Node * next; int data; };然后是LinkList.h#pragma once#include<iostream>#include"Node.h"using namespace std;class LinkList{public: LinkList(); ~LinkList(); void CreatLinkList(int l); void Print();private: int length=0; Node * head,*tail;};LinkList::LinkList(){ head = new Node; tail = head; length++;}LinkList::~LinkList(){ Node* temp; int i = 0; while (head!=NULL) { temp = head; head = head->next; delete temp; i++; }}void LinkList::CreatLinkList(int l){ length = l; if (length == 0) { cout << "Failed to Creat!"<<endl; } else { head->next = new Node; tail = head->next; cout << "Input the data of this node: "; cin >> tail->data; for (int i = 1; i < length; i++) { tail->next = new Node; tail = tail->next; cout<< "Input the data of this node: "; cin >> tail->data; tail->next = NULL; } }}void LinkList::Print(){ if (length == 0) { cout << "Empty list;"<<endl; } else { Node * p=head->next; while (p != NULL) { cout << p->data << endl; p = p->next; } }}最后在 LinkList.cpp中测试#pragma once#include<iostream>#include"Node.h"#include"LinkList.h"using namespace std;int main(){ LinkList L; L.CreatLinkList(5); L.Print();}
阅读全文
0 0
- C/C++ 创建链表
- c语言创建链表
- c语言 创建链表
- c语言实现创建链表
- c语言版创建单循环链表
- C创建链表(二)
- C语言链表的创建
- c语言创建链表总结
- C语言—链表的创建
- C语言 单向链表创建
- C语言创建链表malloc的使用(C的创建对象)
- C链表之创建简单静态链表
- C语言创建单向链表之正向建链
- C语言创建单向链表之逆向建链
- C语言中,链表的创建、输入、输出、链接。
- 链表的简单创建——C
- 链表的简单创建——C程序设计
- 一个链表创建、反转、打印的C语言代码
- python3模拟登录知乎
- ORACLE批量导入
- 《DBA 修炼之道》总结一之前言
- SaveDC与RestoreDC
- Spring MVC学习指南(11-12章总结)
- C++创建链表
- dfs实现单源点最短路径求解
- Java后台与js前端之间的通信问题
- 选择音视频SDK解决方案的标准技术
- Graphics2渲染图像导出的3种方式
- 机器学习实战(Machine Learning in Action)参考答案以及原始数据
- 【每周论文】Xen and the Art of Virtualization
- linux 链接器 库打桩
- python爬虫备忘(6)