您的位置:首页 > 理论基础 > 数据结构算法

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: