基于c++的单链表,双向链表操作以及环
2015-10-07 11:08
357 查看
1:单向链表:生成,头插入,尾插入,某个元素后面插入,删除某元素
:2:双向链表操作::生成,正序/逆序遍历,头插入,尾插入,某个元素后面插入,删除某元素
3:判断链表是否有环
4:判断链表环人长度
#include<stdio.h> using namespace std; struct Node{ int data; Node* next; }; Node* create(){ Node *h=NULL,*p=NULL,*tmp=NULL; int a; scanf("%d",&a); while(a!=0){ tmp=new Node(); tmp->data=a; tmp->next=NULL; if(h==NULL){ h=tmp; p=h; } else{ p->next=tmp; p=tmp; } scanf("%d",&a); } return h; } void addAfter(Node* head,int data,int afdata){ Node *p=head; while(p!=NULL){ if(p->data==afdata){ Node *tmp=new Node(); tmp->data=data; tmp->next=p->next; p->next=tmp; break; } else{ p=p->next; } } } Node* deleteData(Node* head,int data){ Node *p=head; if(p!=NULL && p->data==data){ if(p->next!=NULL){ head=p->next; return head; } else{ return NULL; } } while(p->next!=NULL){ if(p->next->data==data){ if(p->next->next){ p->next=p->next->next; } else{ p->next=NULL; } break; } else{ p=p->next; } } return head; } void addTail(Node* head,int data){ Node* p=head; while(p->next!=NULL){ p=p->next; } Node *tmp=new Node(); tmp->data=data; tmp->next=NULL; p->next=tmp; } Node* addHead(Node* head,int data){ Node *tmp=new Node(); tmp->data=data; tmp->next=head; head=tmp; return head; } void show(Node* head){ Node* p=head; while(p){ printf("%d,",p->data); p=p->next; } printf("\n"); } int main(){ Node *head=create(); show(head); addTail(head,9); show(head); head=addHead(head,1); show(head); addAfter(head,2,9); show(head); head=deleteData(head,2); show(head); return 1; }
:2:双向链表操作::生成,正序/逆序遍历,头插入,尾插入,某个元素后面插入,删除某元素
#include<stdio.h> using namespace std; struct Node{ int data; Node* next; Node* pre; }; Node* create(){ Node *h=NULL,*p=NULL,*tmp=NULL; int a; scanf("%d",&a); while(a!=0){ tmp=new Node(); tmp->data=a; tmp->next=NULL; tmp->pre=NULL; if(h==NULL){ h=tmp; p=h; } else{ p->next=tmp; tmp->pre=p; p=tmp; } scanf("%d",&a); } return h; } void addAfter(Node* head,int data,int afdata){ Node *p=head; while(p!=NULL){ if(p->data==afdata){ Node *tmp=new Node(); tmp->data=data; tmp->next=p->next; tmp->pre=p; p->next=tmp; break; } else{ p=p->next; } } } Node* deleteData(Node* head,int data){ Node *p=head; if(p!=NULL && p->data==data){ if(p->next!=NULL){ head=p->next; p->pre=NULL; return head; } else{ return NULL; } } while(p->next!=NULL){ if(p->next->data==data){ if(p->next->next){ p->next->next->pre=p; p->next=p->next->next; } else{ p->next=NULL; } break; } else{ p=p->next; } } return head; } void addTail(Node* head,int data){ Node* p=head; while(p->next!=NULL){ p=p->next; } Node *tmp=new Node(); tmp->data=data; tmp->next=NULL; tmp->pre=p; p->next=tmp; } Node* addHead(Node* head,int data){ Node *tmp=new Node(); tmp->data=data; tmp->next=head; head->pre=tmp; head=tmp; return head; } void show(Node* head){ Node* p=head; while(p){ printf("%d,",p->data); p=p->next; } printf("\n"); } void upshow(Node* head){ Node* p=head; while(p->next){ p=p->next; } while(p){ printf("%d,",p->data); p=p->pre; } printf("\n"); } int main(){ Node *head=create(); show(head); upshow(head); addTail(head,9); show(head); head=addHead(head,1); show(head); addAfter(head,2,9); show(head); head=deleteData(head,2); show(head); return 1;
3:判断链表是否有环
Node* IsLoop(Node *head) { Node*pslow=head,*pfast=head; while(pslow!=NULL && pfast!=NULL) { pslow=pslow->next; pfast=pfast->next->next; if(pslow==pfast) return pslow; } return NULL; }
4:判断链表环人长度
int GetRingLen(Node *meeting) { Node *pslow=meeting,*pslow=meeting; int len=0; while(pslow!=NULL && pfast!=NULL) { pslow=pslow->next; pfast=pfast->next->next; len++; if(pslow==pfast) return len; } return 0; }
相关文章推荐
- 黑马程序员——OC语言加强---NSArray 的介绍和基本使用
- 最小生成树prim算法实现
- 黑马程序员——OC语言加强---NSRange使用
- C++中的类型转换之 const_cast static_cast dynamic_cast reinterpret_cast
- 黑马程序员——OC语言加强---Foundation框架介绍
- 一起talk C栗子吧(第五十三回:C语言实例--图的最短路径一)
- 黑马程序员——OC语言加强---block的typedef
- 黑马程序员——OC语言加强---ARC使用特点及注意事项
- C++ map的基本操作和使用
- 【C++】递归求数组最短路径
- C++ operator关键字(重载操作符)
- 常用排序算法分析实现(C语言)
- char *a 和char a[] 的区别
- C++排序:冒泡排序,简单选择排序,直接插入排序,希尔排序,堆排序,归并排序,快速排序
- 面试复习(C++)之归并排序
- C++实现链队列
- C++实现循环队列
- c/c++区别
- C++实现顺序栈
- C++实现链栈