链表 c++
来源:互联网 发布:2016淘宝开店步骤 编辑:程序博客网 时间:2024/06/13 01:41
linearLinkedList.h
#include <iostream>#include <string>using namespace std;struct Node{ int data; Node *next; Node(int a){data=a; next=NULL;}};class LinkList{public: LinkList(); void Insert(int data, int pos); // void Remove(int data); // int Length(); // void Reverse(); int Find(int data); void Print(); ~LinkList();private: Node *head; int length;};
linearLinkedList.cpp
#include <string>#include <iostream>#include "linearLinkedList.h"using namespace std;LinkList::LinkList(){ head = NULL; length = 0;}void LinkList::Insert(int data, int pos){ if(pos < 0 || pos > length) { cout << "ValueError: pos not right." << endl; return; } Node *tmp = head; Node *node = new Node(data); if(pos == 0) { node->next = tmp; head = node; length++; return; } int idx = 1; while(tmp != NULL && idx < pos) { tmp = tmp->next; idx++; } node->next = tmp->next; tmp->next = node; length++; return;}LinkList::~LinkList(){ Node *tmp; for(int i=0; i < length; i++) { tmp = head; head = head->next; delete tmp; }}int LinkList::Find(int data){ // Node *tmp = head; int idx = 1; while(tmp != NULL) { if(tmp->data == data) return idx; tmp = tmp->next; idx++; } return -1;}void LinkList::Print(){ if(head == NULL) { cout << "Empty LinkList." << endl; return; } Node *tmp = head; while(tmp != NULL) { cout << tmp->data << " "; tmp = tmp->next; } cout << endl;}int main(){ LinkList h; h.Insert(1, 0); h.Insert(2, 1); h.Insert(3, 2); h.Print();}
result
junr$ ./a.out1 2 3
阅读全文
0 0
- C链表
- c 链表
- 链表(C#)
- C链表
- C 链表
- [C]链表
- C 链表
- 【C++】链表
- C 链表
- c链表
- C-链表
- c 链表
- C链表
- c链表
- 链表 c
- C++:链表
- C 链表
- C语言泛型初探----C链表
- LeetCode #7 Reverse Integer
- Codeforces 854D Jury Meeting【思维+前后缀和+二分】
- sql with as 用法
- bzoj 1637: [Usaco2007 Mar]Balanced Lineup
- sass+compass异常:error screen.scss (Line 3: File to import not found or unreadable: blueprint/reset.)
- 链表 c++
- linux启动XAMPP命令
- kafka--简介、组件构成、安装、基础操作
- poj1062 [ZJOI2002]昂贵的聘礼(spfa+枚举)
- Linux
- ubuntu 无法解析主机的解决方法
- 易语言静态变量和全局变量的区别
- HDU 1420Prepared for New Acmer(快速幂裸题)
- java中序列化与反序列化