c++实现链表的抽象数据结构

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值