链表的特点
便于插入删除,这些操作只要改变指针的指向 遍历查找O(n) 排序复杂度也极大 所以适合插入删除频繁的程序。
实现思想
自建基本的节点数据结构 明确基本操作:初始化、插入、删除、按位置查找、按值查找等等
实现代码
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
typedef void * ElemType;
typedef struct __ListNode{
ElemType data;
__ListNode* next;
} * LinkNode;
class LinkList{
public:
LinkList ( ) ;
public:
void Init ( ) ;
bool InsertAtLoc ( ElemType, int ) ;
ElemType FindAtLoc ( int ) ;
int FindLocByValue ( ElemType) ;
int GetLength ( ) ;
bool IsEmpty ( ) ;
bool DeleteAtLoc ( int ) ;
void DestroyList ( ) ;
private:
LinkNode head;
LinkNode tail;
int ListCount;
} ;
LinkList: : LinkList ( ) {
Init ( ) ;
}
void LinkList: : Init ( ) {
head = ( LinkNode) malloc ( sizeof ( struct __ListNode) ) ;
head-> next = NULL ;
head-> data = NULL ;
tail = head;
ListCount = 0 ;
}
bool LinkList: : InsertAtLoc ( ElemType value, int loc = - 1 ) {
if ( loc> ListCount|| loc== 0 || loc< - 1 )
{
return false;
}
LinkNode new_node = ( LinkNode) malloc ( sizeof ( struct __ListNode) ) ;
if ( new_node == NULL )
{
return false;
}
new_node-> data = value;
if ( loc == - 1 )
{
tail-> next = new_node;
new_node-> next = NULL ;
tail = new_node;
ListCount++ ;
return true;
}
LinkNode p = head;
int cnt = 0 ;
while ( p-> next!= NULL )
{
if ( cnt == loc)
{
new_node-> next = p-> next;
p-> next = new_node;
}
p = p-> next;
cnt++ ;
}
if ( cnt == loc)
{
new_node-> next = p-> next;
p-> next = new_node;
tail = new_node;
}
ListCount++ ;
return true;
}
ElemType LinkList: : FindAtLoc ( int loc) {
if ( loc< 0 || loc> ListCount)
{
return NULL ;
}
LinkNode p = head;
int cnt = 0 ;
while ( p-> next!= NULL )
{
if ( loc == cnt)
{
return p-> data;
}
cnt++ ;
p = p-> next;
}
if ( cnt == loc)
{
return p-> data;
}
return NULL ;
}
int LinkList: : FindLocByValue ( ElemType elem)
{
LinkNode p= head;
int cnt= 0 ;
while ( p-> next!= NULL )
{
if ( p-> data == elem)
{
return cnt;
}
cnt++ ;
p= p-> next;
}
if ( p-> data == elem)
{
return cnt;
}
return - 1 ;
}
bool LinkList: : DeleteAtLoc ( int loc) {
if ( loc> ListCount|| loc< 0 )
{
return false;
}
LinkNode p = head;
int cnt = 0 ;
LinkNode pre = NULL ;
while ( p-> next!= NULL )
{
if ( loc == cnt)
{
pre-> next = p-> next;
free ( p) ;
ListCount-- ;
return true;
}
pre = p;
p = p-> next;
cnt++ ;
}
if ( loc == cnt)
{
pre-> next = NULL ;
tail = pre;
free ( p) ;
ListCount-- ;
return true;
}
return false;
}
void LinkList: : DestroyList ( ) {
for ( int i= 1 ; i<= ListCount; ++ i)
{
DeleteAtLoc ( 1 ) ;
}
}
int LinkList: : GetLength ( )
{
return ListCount;
}
bool LinkList: : IsEmpty ( )
{
return GetLength ( ) == 0 ? true: false;
}
int _tmain ( int argc, _TCHAR* argv[ ] )
{
LinkList l;
int t1, t2;
t1 = 16 ;
t2 = 8 ;
ElemType data1 = ( void * ) ( & t1) ;
l. InsertAtLoc ( data1) ;
data1 = ( void * ) ( & t2) ;
l. InsertAtLoc ( data1) ;
void * p = l. FindAtLoc ( 1 ) ;
std: : cout<< * ( int * ) p<< std: : endl;
l. DeleteAtLoc ( 2 ) ;
p = l. FindAtLoc ( 1 ) ;
std: : cout<< * ( int * ) p<< std: : endl;
int cur = l. FindLocByValue ( ( void * ) ( & t2) ) ;
l. DestroyList ( ) ;
int ret = l. GetLength ( ) ;
bool b = l. IsEmpty ( ) ;
system ( "pause" ) ;
return 0 ;
}