C++实现:单链表的反转(序)操作
来源:互联网 发布:上海数控编程招聘信息 编辑:程序博客网 时间:2024/05/16 19:28
//main.cpp #include <iostream>#include <ctime>#include <cstdlib>#include <string>#include <vector>#include "linklist.cpp"#define MAXINTLEN 10using namespace std;int main(int argc, char *argv[]) { int tmp[MAXINTLEN]; double runtime = 0; clock_t s,e; linklist sl; for (int i = 0; i < MAXINTLEN; i++) { //srand((unsigned)time(i)); tmp[i] = rand()%100; } s = clock(); sl.construct_link(tmp,MAXINTLEN); cout<<"this linklist..."<<endl; sl.print_link(); sl.reverse(); cout<<"after reverse..."<<endl; sl.print_link(); e = clock(); runtime = (double)(e - s);//CLOCKS_PER_SEC; cout<<"runtime is:"<<runtime<<endl; return 0;}
// linklist.hxx#ifndef LINKLIST_H_#define LINKLIST_H_#include<iostream>using namespace std;struct node{ int data; struct node *next;};class linklist{private: struct node *head; int length;public: linklist(); ~linklist(); struct node* get_link(void); void construct_link(int *arr, int num); int get_length(void); void reverse(void); void print_link(void);};#endif
// linklist.cpp#include "linklist.hxx"linklist::linklist(){ this->head = NULL; this->length = 0;}struct node* linklist::get_link(void){ return this->head;}void linklist::construct_link(int *arr, int num){ // use int array to construct linklist struct node *tmp, *cur; for (int i = 0; i < num; ++i) { tmp = new (struct node); tmp->data = arr[i]; tmp->next = NULL; if (0 == i){ this->head = tmp; }else{ cur->next = tmp; } cur = tmp; this->length ++; } return;}int linklist::get_length(void){ if (NULL == this->head) return 0; struct node *cur = this->head; int len = 0; while (cur){ len++; cur = cur->next; } return len;}void linklist::reverse(void){ // reverse the linklist if (!this->head) return; if (!this->head->next) return; // only one element struct node *cur = this->head->next; struct node *pre = this->head; struct node *tmp = NULL; pre->next = NULL; while(cur){ tmp = cur->next; cur->next = pre; pre = cur; cur = tmp; } this->head = pre; return;}void linklist::print_link(void){ // print the linklist content if (!this->head){ cout<<"this link is null."<<endl; return; } struct node* head = this->head; struct node *cur = head; cout<<"link ("<<this->length<<"): "; while(cur){ cout<<cur->data<<" "; cur = cur->next; } cout<<endl; return;}linklist::~linklist(){}
运行结果:
- C++实现:单链表的反转(序)操作
- 单链表的反转实现(C++)
- 单链表反转 c实现
- c语言实现单链表的操作:创建,删除,插入,反转, 排序等
- c/c++单链表的创建和删除,插入,反转操作
- 单链表反转C语言实现
- 单链表反转C语言实现
- 单链表反转C语言实现
- 单链表的反转实现
- 单链表反转的实现
- 实现单链表的反转
- C语言字符串反转的实现
- C语言指针实现字符串的反转
- C语言实现字符的反转输出
- C语言:单链表的反转
- C语言来实现字符串反转(只有单词反转,单词里的字母不反转)
- C语言来实现字符串反转 只有单词顺序反转,单词里的字母不反转
- 单链表的反转java实现
- 算法导论寻找最大子数组
- IOS简单Touch操作类UIGestureRecognizer
- java ant builder.xml详解
- maven tomcat内存溢出解决办法
- Hibernate get和load区别
- C++实现:单链表的反转(序)操作
- 给JVM配置jmx agent
- 格式占位符
- fabric配置——部署在apache下强制使用root用户
- IOS控件UITextField详解
- 3.3 标准库vector类型
- 12亿次月访问流量网站服务器架构探秘
- VC中连接数据库编程 详解2
- IOS简单调试NSLog