c++实现链表的抽象数据结构
2015-08-04 20:13
821 查看
#include<iostream>
#include<stdio.h>
using namespace std;
static const int arr[]={11,2,31,4};
static const int length=sizeof(arr)/sizeof(int);
typedef struct listnode{
int data;
struct listnode * next;
}node,*pnode;
class LIST{
public:
LIST(){
head=NULL;
}
~LIST(){
while(head){
pnode tmp=head->next;
delete head;
head=NULL;
head=tmp;
}
}
pnode createList(); //1.新建单链表
int sizeList(pnode head); //2.单链表长度
void print(pnode head) const; //3.打印
int deleteList(pnode head, int value); //4.删除某个节点
int insertList(pnode head,int value); //5.插入某个节点
pnode sort(pnode head); //7.单链表排序
pnode reverseList(pnode head); //8.单链表逆转
pnode researchMid(pnode head); //9.单链表中间节点
private:
pnode head;
};
pnode LIST::createList() {
int i=0;
pnode head = new node;
head->data=-1;
pnode tmp=head;
for(;i<length;++i) {
pnode p1=new node;
p1->data=arr[i];
tmp->next=p1;
tmp=p1;
}
tmp->next=NULL;
return head;
}
void LIST::print(pnode head) const {
pnode tmp=head->next;
while(tmp){
cout<<tmp->data<<" ";
tmp=tmp->next;
}
cout<<""<<endl;
}
int LIST::sizeList(pnode head) {
pnode tmp=head->next;
int size=0;
while(tmp){
size++;
tmp=tmp->next;
}
return size;
}
int LIST::deleteList(pnode head,int value) {
pnode p1=head;
pnode p2=p1->next;
while(p2&&p2->data!=value) {
p1=p2;
p2=p2->next;
}
if(p2){
p1->next=p2->next;
delete p2;
p2=NULL;
return 1;
}
else{
return -1;
}
}
int LIST::insertList(pnode head, int value) {
pnode p0=new node;
p0->data=value;
pnode p1=head;
pnode p2=head->next;
while(p2 && value>p2->data ) { //关键代码
p1=p2;
p2=p2->next;
}
p1->next=p0;
p0->next=p2;
return 1;
}
pnode LIST::sort(pnode head){
if(NULL==head->next){
return NULL;
}
int size=sizeList(head);
int i,j;
for(i=1;i<=size;++i){ //冒泡
pnode p=head;
for(j=0;j<=size-i;++j) {
if(p->data > p->next->data){
int tmp=p->data;
p->data=p->next->data;
p->next->data=tmp;
}
p=p->next;
}
}
return head;
}
pnode LIST::reverseList(pnode head) {
pnode p0=head->next;//原先链表模型: head->p0->p1
pnode p1=NULL;
head->next=NULL; //新建一个链表
while(p0){ //从链表的第一个结点开始遍历
p1=p0->next;
p0->next=head->next; //头插法继续新建链表
head->next=p0;
p0=p1;
}
return head;
}
pnode LIST::researchMid(pnode head) {
if(NULL==head || NULL==head->next) {
return NULL;
}
pnode p0=head->next; //快指针
pnode p1=head->next; //慢指针
while(p0->next->next){
p1=p1->next;
p0=p0->next->next;
}
return p1;
}
#include"list.h"
int main(){
LIST *list=new LIST;
cout<<"create list: ";
pnode head = list->createList();
list->print(head);
if(list->deleteList(head,6)==1) {
list->print(head);
}
cout<<"after insert two number: ";
if(list->insertList(head,6)==1) {
list->insertList(head,5);
list->print(head);
}
else{
cout<<"connot success insert number~"<<endl;
}
cout<<"list's size: ";
cout<<list->sizeList(head)<<endl;
cout<<"after sort: ";
list->sort(head);
list->print(head);
cout<<"reverse list:"<<endl;
list->reverseList(head);
list->print(head);
cout<<"mid value is: ";
pnode midNode=list->researchMid(head);
cout<<midNode->data<<endl;
system("pause");
return 1;
}
参考:
http://www.cnblogs.com/EricYang/archive/2009/09/06/1561353.html http://blog.csdn.net/huzhen919/article/details/4296220
#include<stdio.h>
using namespace std;
static const int arr[]={11,2,31,4};
static const int length=sizeof(arr)/sizeof(int);
typedef struct listnode{
int data;
struct listnode * next;
}node,*pnode;
class LIST{
public:
LIST(){
head=NULL;
}
~LIST(){
while(head){
pnode tmp=head->next;
delete head;
head=NULL;
head=tmp;
}
}
pnode createList(); //1.新建单链表
int sizeList(pnode head); //2.单链表长度
void print(pnode head) const; //3.打印
int deleteList(pnode head, int value); //4.删除某个节点
int insertList(pnode head,int value); //5.插入某个节点
pnode sort(pnode head); //7.单链表排序
pnode reverseList(pnode head); //8.单链表逆转
pnode researchMid(pnode head); //9.单链表中间节点
private:
pnode head;
};
pnode LIST::createList() {
int i=0;
pnode head = new node;
head->data=-1;
pnode tmp=head;
for(;i<length;++i) {
pnode p1=new node;
p1->data=arr[i];
tmp->next=p1;
tmp=p1;
}
tmp->next=NULL;
return head;
}
void LIST::print(pnode head) const {
pnode tmp=head->next;
while(tmp){
cout<<tmp->data<<" ";
tmp=tmp->next;
}
cout<<""<<endl;
}
int LIST::sizeList(pnode head) {
pnode tmp=head->next;
int size=0;
while(tmp){
size++;
tmp=tmp->next;
}
return size;
}
int LIST::deleteList(pnode head,int value) {
pnode p1=head;
pnode p2=p1->next;
while(p2&&p2->data!=value) {
p1=p2;
p2=p2->next;
}
if(p2){
p1->next=p2->next;
delete p2;
p2=NULL;
return 1;
}
else{
return -1;
}
}
int LIST::insertList(pnode head, int value) {
pnode p0=new node;
p0->data=value;
pnode p1=head;
pnode p2=head->next;
while(p2 && value>p2->data ) { //关键代码
p1=p2;
p2=p2->next;
}
p1->next=p0;
p0->next=p2;
return 1;
}
pnode LIST::sort(pnode head){
if(NULL==head->next){
return NULL;
}
int size=sizeList(head);
int i,j;
for(i=1;i<=size;++i){ //冒泡
pnode p=head;
for(j=0;j<=size-i;++j) {
if(p->data > p->next->data){
int tmp=p->data;
p->data=p->next->data;
p->next->data=tmp;
}
p=p->next;
}
}
return head;
}
pnode LIST::reverseList(pnode head) {
pnode p0=head->next;//原先链表模型: head->p0->p1
pnode p1=NULL;
head->next=NULL; //新建一个链表
while(p0){ //从链表的第一个结点开始遍历
p1=p0->next;
p0->next=head->next; //头插法继续新建链表
head->next=p0;
p0=p1;
}
return head;
}
pnode LIST::researchMid(pnode head) {
if(NULL==head || NULL==head->next) {
return NULL;
}
pnode p0=head->next; //快指针
pnode p1=head->next; //慢指针
while(p0->next->next){
p1=p1->next;
p0=p0->next->next;
}
return p1;
}
#include"list.h"
int main(){
LIST *list=new LIST;
cout<<"create list: ";
pnode head = list->createList();
list->print(head);
if(list->deleteList(head,6)==1) {
list->print(head);
}
cout<<"after insert two number: ";
if(list->insertList(head,6)==1) {
list->insertList(head,5);
list->print(head);
}
else{
cout<<"connot success insert number~"<<endl;
}
cout<<"list's size: ";
cout<<list->sizeList(head)<<endl;
cout<<"after sort: ";
list->sort(head);
list->print(head);
cout<<"reverse list:"<<endl;
list->reverseList(head);
list->print(head);
cout<<"mid value is: ";
pnode midNode=list->researchMid(head);
cout<<midNode->data<<endl;
system("pause");
return 1;
}
参考:
http://www.cnblogs.com/EricYang/archive/2009/09/06/1561353.html http://blog.csdn.net/huzhen919/article/details/4296220
相关文章推荐
- SDUToj2122数据结构实验之链表七:单链表中重复元素的删除
- SDUToj1130数据结构上机测试1:顺序表的应用
- SDUToj2121数据结构实验之链表六:有序链表的建立
- 【暑假】[基本数据结构]基本的数据结构知识点总结梳理
- 数据结构笔记之线性存储
- 【暑假】[基本数据结构]根据in_order与post_order构树
- 树状数组---数据结构
- 【暑假】[基本数据结构]根据BFS与DFS确定树
- ios 底层数据结构
- 红黑树
- 数据结构及算法综述
- 数据结构及算法综述
- 【细说PHP学习】第九章 PHP中的数组与数据结构
- 数据结构学习笔记1-链表反转(递归与非递归)
- 读书笔记:数据结构与算法分析(java语言描述)——引论
- 数据结构03_栈、队列和数组
- 位图数据结构的实现与应用
- 数据结构 树状数组
- hdu 3791 二叉搜索树(数据结构)
- <C/C++数据结构>常见的树结构