博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个简单的链表结构 分类: C/C++ 数据结构与算...
阅读量:4613 次
发布时间:2019-06-09

本文共 4605 字,大约阅读时间需要 15 分钟。

         仅仅实现了基本的链表操作,如创建、查找、删除、排序等。

//头文件/*there is no head node exist * */#include 
using namespace std;typedef struct Node{ int value; struct Node* next;}Node,*ListNode;bool isEmpty(ListNode );//judge the list statusbool createList(ListNode* ,const int=10);//create new listbool printList(ListNode ,ostream& output=cout);//print the listint size(ListNode);bool insert(ListNode* ,int ,int);//insert in the lastbool insertTail(ListNode* ,int );bool find(ListNode ,int ,int& );//find a node in listbool deleteNode(ListNode* ,int ,int& );bool deleteNode(ListNode* ,int ,bool flag=true);bool getData(ListNode ,int ,int& );bool deleteList(ListNode* );//delete the list entireListNode sort(ListNode );//sort,and return a new listint fmin(ListNode );int fmax(ListNode );
//源文件#include 
#include "List.h"#include
bool isEmpty(ListNode pHead){ if(!pHead) return true; return false;}bool createList(ListNode* pHead,const int N){ if(N<0) return false; if(N==0) return true; static size_t randSeed=100; ListNode p1=new Node(); if(!p1)//apply for memory failed return false; srand(time(0)+randSeed); p1->value=rand()%100+1; *pHead=p1; for(int i=0;i
next=NULL; return false; } pNew->value=rand()%100+1; p1->next=pNew; p1=pNew; randSeed+=rand(); } p1->next=NULL;//the last node pointer to NULL return true;}bool printList(ListNode pHead,ostream& output){ if(!pHead) return false; ListNode p=pHead; while(p) { output<
value<<" "; p=p->next; } return true; }int size(ListNode pHead){ ListNode p=pHead; int length(0); while(p) { length++; p=p->next; } return length;}bool insert(ListNode* pHead,int pos,int data){//insert before the pos node if(pHead==NULL) return false; ListNode p,k; k=p=*pHead; int j=1; //pos should more than 0 while(p && j
next; ++j; } //if the list is empty,insert will be refused if(!p || j>pos)//if pos<1,return; return false; ListNode q; q=new Node(); q->value=data; q->next=p; if(k==p)//在第一个节点前插入 { *pHead=q; } else k->next=q; return true;}bool insertTail(ListNode* pHead,int data){ if(pHead==NULL) return false; ListNode p=*pHead; if(!p) { ListNode q=new Node(); q->value=data; q->next=NULL; *pHead=q; return true; } while(p->next) { p=p->next; } ListNode q=new Node(); q->value=data; q->next=NULL; p->next=q; return true;}bool deleteNode(ListNode* pHead,int pos,int& value){ if(pHead==NULL || *pHead==NULL) return false; ListNode p,q; p=q=*pHead; int j=1; while(p && j
next; j++; } if(!p || j>pos) return false; if(p==q) { value=p->value; *pHead=p->next; delete p; p=NULL; return true; } else { value=p->value; q->next=p->next; delete p; p=NULL; return true; }}//delete the node where value=data//if flag=true,delete all node where value=data,or delete the first onebool deleteNode(ListNode* pHead,int data,bool flag){ if(pHead==NULL || *pHead==NULL) return false; ListNode p,q; p=q=*pHead; if(flag) { while(p) { if(p->value==data) { if(p==q) { *pHead=p->next; delete p; p=q=*pHead; } else { q->next=p->next; delete p; p=q->next; } } q=p; p=p->next; } } else { while(p && p->value!=data) { q=p; p=p->next; } if(!p) return false; if(p==q) { *pHead=p->next; delete p; p=NULL; } else { q->next=p->next; delete p; p=NULL; } } return true;}//find data in list,pos is the position in listbool find(ListNode pHead,int data,int& pos){ ListNode p=pHead; pos=0; while(p) { pos++; if(p->value==data) { return true; } p=p->next; } return false;}//read the value in k nodebool getData(ListNode pHead,int k,int& data){ if(pHead==NULL) return false; if(k<=0 || k>size(pHead)) return false; int i=1; ListNode p=pHead; while(p && i
next; } if(!p) return false; data=p->value; return true;}bool deleteList(ListNode* pHead){ if(pHead==NULL) return false; ListNode p=*pHead; while(p) { ListNode q=p->next; delete p; p=q; } *pHead=NULL; return true;}//select sortListNode sort(ListNode pHead){ ListNode pHeadNew; pHeadNew=NULL; if(!pHead) return pHeadNew; while(pHead) { int min=fmin(pHead); deleteNode(&pHead,min,false); insertTail(&pHeadNew,min); } return pHeadNew;}int fmax(ListNode pHead){ int max=0x80000000; ListNode p=pHead; while(p) { if(p->value > max) { max=p->value; } p=p->next; } return max; }int fmin(ListNode pHead){ int min=0x7FFFFFFF; ListNode p=pHead; while(p) { if(p->value < min) { min=p->value; } p=p->next; } return min; }
//测试程序#include 
#include "List.h"using namespace std;const int N=10;int main(){ ListNode pHead; pHead=NULL; if(!createList(&pHead,N)) cout<<"create list failed!"<

转载于:https://www.cnblogs.com/zclzqbx/p/4687119.html

你可能感兴趣的文章
acwing 651. 逛画展
查看>>
Vijos P1243 生产产品 (单调队列优化DP)
查看>>
iOS常用第三方库 -转
查看>>
Android布局学习
查看>>
jQuery中事件绑定与解绑
查看>>
js原生Ajax的封装与使用
查看>>
周总结6
查看>>
PostgreSQL 务实应用(二/5)插入冲突
查看>>
一种公众号回复关键词机制
查看>>
java多线程入门学习(一)
查看>>
基于 Web 的 Go 语言 IDE - Wide 1.1.0 公布!
查看>>
nyist oj 138 找球号(二)(hash 表+位运算)
查看>>
Movidius软件手册阅读 2017-09-04
查看>>
ytu 1910:字符统计(水题)
查看>>
201671030110 姜佳宇 实验三作业互评与改进
查看>>
mysql-5.6.15 开启二进制文件
查看>>
python的沙盒环境--virtualenv
查看>>
软件自动化测试——入门、进阶与实战
查看>>
BZOJ1878 [SDOI2009]HH的项链 树状数组 或 莫队
查看>>
BZOJ3675 [Apio2014]序列分割 动态规划 斜率优化
查看>>