一个单链表C++简单的实现版本-转自chinaunix
2012-12-11 15:07
639 查看
链表是一种线性的数据结构,其一个已有的链表中插入、删除一个链表结点的时间复杂度是O(n)。
以下为一个单链表C++简单的实现版本。
具体为:
头插法、尾插法,链表翻转的递归版和非递归版。
点击(此处)折叠或打开
#include <cstdio>
struct Node
{
int data ;
Node *next ;
} ;
void Insert_Head( Node *&head , const int &val )
// 头插法
{
Node *ptr = new Node() ;
ptr->next = head ;
ptr->data = val ;
head = ptr ;
}
void Insert_Back( Node *&head , Node *&back ,const int &val )
// 尾插法
{
Node *ptr = new Node() ;
ptr->data = val ;
ptr->next = NULL ;
if ( head == NULL )
head = back = ptr ;
else {
back->next = ptr ;
back = ptr ;
}
}
void Print( Node * const &head )
// 单链表的打印函数
{
for ( Node *it = head ; it != NULL ; it = it ->next )
printf("%d " , (*it).data );
puts("") ;
}
void Reverse_not_Dfs( Node *&head )
// 翻转(非递归)
{
if ( head == NULL )
return ;
Node *pre , *cur , *nxt ;
pre = head ;
cur = head->next ;
while ( cur )
{
nxt = cur->next ;
cur->next = pre ;
pre = cur ;
cur = nxt ;
}
head->next = NULL ;
head = pre ;
}
Node* Reverse_Dfs( Node *p , Node *&head )
// 翻转(递归)
{
if ( p->next == NULL )
{
head->next = NULL ;
head = p ;
return p ;
}
Node *tmp = Reverse_Dfs(p->next , head) ;
tmp->next = p ;
return p ;
}
int main()
{
int n;
while ( ~scanf("%d" , &n) )
{
Node *Fhead = NULL , *Bhead = NULL ,*back = NULL ;
int val , i ;
for ( i = 0; i < n; i++ )
{
scanf("%d" , &val ) ;
Insert_Head( Fhead , val ) ;
Insert_Back( Bhead, back , val ) ;
}
printf( "Print Insert_head list : " ) ;
Print( Fhead ) ;
printf( "Print Insert_back list : ") ;
Print(Bhead) ;
printf( "The Insert_head list after Reverse : " ) ;
Reverse_not_Dfs( Fhead ) ;
Print( Fhead ) ;
printf( "The Insert_back list after Reverse : " ) ;
Reverse_Dfs(Bhead , Bhead) ;
Print( Bhead ) ;
}
return 0 ;
}
简单的有序链表合并:
点击(此处)折叠或打开
// 合并两个有序的链表 eg: 1 4
6 , 3 5 9 -> 1 3 4
5 6 9
Node* Merge_not_Dfs( Node *head1 , Node *head2 )
// 非递归版
{
if ( head1 == NULL ) return
head2 ;
if ( head2 == NULL ) return
head1 ;
Node *head = NULL , *ptr1 = NULL , *ptr2 = NULL ;
if ( head1->data < head2->data )
{
head = head1 ;
ptr1 = head->next ;
ptr2 = head2 ;
}
else
{
head = head2 ;
ptr2 = head2->next ;
ptr1 = head1 ;
}
Node *cur = head ;
while ( ptr1 != NULL && ptr2 != NULL )
{
if ( ptr1->data <= ptr2->data )
{
cur->next = ptr1 ;
cur = ptr1 ;
ptr1 = ptr1->next ;
}
else
{
cur->next = ptr2 ;
cur = ptr2 ;
ptr2 = ptr2->next ;
}
}
if ( ptr1 != NULL )
cur->next = ptr1 ;
if ( ptr2 != NULL )
cur->next = ptr2 ;
return head ;
}
Node* Merge_Dfs( Node *head1 , Node *head2 )
// 递归版
{
if ( head1 == NULL ) return
head2 ;
if ( head2 == NULL ) return
head1 ;
Node *head =NULL ;
if ( head1->data < head2->data )
{
head = head1 ;
head->next = Merge_Dfs( head1->next , head2 ) ;
}
else
{
head = head2 ;
head->next = Merge_Dfs( head1 , head2->next) ;
}
return head ;
}
原文地址: http://blog.chinaunix.net/uid-26902496-id-3392760.html
以下为一个单链表C++简单的实现版本。
具体为:
头插法、尾插法,链表翻转的递归版和非递归版。
点击(此处)折叠或打开
#include <cstdio>
struct Node
{
int data ;
Node *next ;
} ;
void Insert_Head( Node *&head , const int &val )
// 头插法
{
Node *ptr = new Node() ;
ptr->next = head ;
ptr->data = val ;
head = ptr ;
}
void Insert_Back( Node *&head , Node *&back ,const int &val )
// 尾插法
{
Node *ptr = new Node() ;
ptr->data = val ;
ptr->next = NULL ;
if ( head == NULL )
head = back = ptr ;
else {
back->next = ptr ;
back = ptr ;
}
}
void Print( Node * const &head )
// 单链表的打印函数
{
for ( Node *it = head ; it != NULL ; it = it ->next )
printf("%d " , (*it).data );
puts("") ;
}
void Reverse_not_Dfs( Node *&head )
// 翻转(非递归)
{
if ( head == NULL )
return ;
Node *pre , *cur , *nxt ;
pre = head ;
cur = head->next ;
while ( cur )
{
nxt = cur->next ;
cur->next = pre ;
pre = cur ;
cur = nxt ;
}
head->next = NULL ;
head = pre ;
}
Node* Reverse_Dfs( Node *p , Node *&head )
// 翻转(递归)
{
if ( p->next == NULL )
{
head->next = NULL ;
head = p ;
return p ;
}
Node *tmp = Reverse_Dfs(p->next , head) ;
tmp->next = p ;
return p ;
}
int main()
{
int n;
while ( ~scanf("%d" , &n) )
{
Node *Fhead = NULL , *Bhead = NULL ,*back = NULL ;
int val , i ;
for ( i = 0; i < n; i++ )
{
scanf("%d" , &val ) ;
Insert_Head( Fhead , val ) ;
Insert_Back( Bhead, back , val ) ;
}
printf( "Print Insert_head list : " ) ;
Print( Fhead ) ;
printf( "Print Insert_back list : ") ;
Print(Bhead) ;
printf( "The Insert_head list after Reverse : " ) ;
Reverse_not_Dfs( Fhead ) ;
Print( Fhead ) ;
printf( "The Insert_back list after Reverse : " ) ;
Reverse_Dfs(Bhead , Bhead) ;
Print( Bhead ) ;
}
return 0 ;
}
简单的有序链表合并:
点击(此处)折叠或打开
// 合并两个有序的链表 eg: 1 4
6 , 3 5 9 -> 1 3 4
5 6 9
Node* Merge_not_Dfs( Node *head1 , Node *head2 )
// 非递归版
{
if ( head1 == NULL ) return
head2 ;
if ( head2 == NULL ) return
head1 ;
Node *head = NULL , *ptr1 = NULL , *ptr2 = NULL ;
if ( head1->data < head2->data )
{
head = head1 ;
ptr1 = head->next ;
ptr2 = head2 ;
}
else
{
head = head2 ;
ptr2 = head2->next ;
ptr1 = head1 ;
}
Node *cur = head ;
while ( ptr1 != NULL && ptr2 != NULL )
{
if ( ptr1->data <= ptr2->data )
{
cur->next = ptr1 ;
cur = ptr1 ;
ptr1 = ptr1->next ;
}
else
{
cur->next = ptr2 ;
cur = ptr2 ;
ptr2 = ptr2->next ;
}
}
if ( ptr1 != NULL )
cur->next = ptr1 ;
if ( ptr2 != NULL )
cur->next = ptr2 ;
return head ;
}
Node* Merge_Dfs( Node *head1 , Node *head2 )
// 递归版
{
if ( head1 == NULL ) return
head2 ;
if ( head2 == NULL ) return
head1 ;
Node *head =NULL ;
if ( head1->data < head2->data )
{
head = head1 ;
head->next = Merge_Dfs( head1->next , head2 ) ;
}
else
{
head = head2 ;
head->next = Merge_Dfs( head1 , head2->next) ;
}
return head ;
}
原文地址: http://blog.chinaunix.net/uid-26902496-id-3392760.html
相关文章推荐
- unix ls命令的一个简单实现
- 二路归并的一个C++最简单实现
- 一个多层感知机C++的简单实现
- C++的杂七杂八:如何实现一个简单的bind
- binder 一个简单的c++服务的实现,与callback实现
- c++实现一个简单的空间配置器allocator
- 从零开始学C++之重载 operator new 和 operator delete 实现一个简单内存泄漏跟踪器
- C++ 使用模版范式写一个单例模式的类--简单实现单例模式
- C++实现一个简单的异常日志记录类
- unix ls命令的一个简单实现
- c++ — 实现一个简单的string类
- 一个简单二叉树的C++实现(一)
- C++ 一个简单的log类的实现
- 深入UNIX编程:一个简单聊天室的两种实现 (fcntl 和 select)
- 一个简单链表的C++实现(二)
- 归并排序的简单实现(c++ 版本)
- 一个简单的剪刀石头布游戏(C++实现)
- 单链表的简单c++实现
- 归并排序的简单实现(c++ 版本)
- 简单的一个C++中traits功能的实现例子