線性表之鍊錶的實現(一) 單鏈表帶頭結點實現

2021-07-24 16:47:58 字數 4408 閱讀 7930

本文針對單鏈表,總結其相應的常用操作。主要是給出**實現。

common.h

#ifndef common_h

#define common_h

/* 函式結果狀態碼 */

#define true 1

#define false 0

#define ok 1

#define error 0

#define infeasible -1

//#define overflow -2

/* 型別定義 */

typedef int status; // status是函式的型別,其值是函式結果狀態碼

typedef int elemtype; // elemtype是資料型別

#endif

linkedlist.h

注:以下常用操作介面均為帶頭結點的實現

#ifndef linkedlist_h

#define linkedlist_h

#include "common.h"

#include

/* 鍊錶節點宣告 */

struct listnode

listnode( elemtype x ) : data(x), next(null) {}

};typedef listnode* linkedlist; // linkedlist是指向型別listnode的指標

linkedlist create_by_head( const elemtype* arr, int n ); // 頭插法

linkedlist create_by_tail( const elemtype* arr, int n ); // 尾插法

status destroy( linkedlist head ); // 銷毀

status destroy1( linkedlist head ); // 銷毀 - 遞迴實現

status print( linkedlist head ); // 列舉

status print1( linkedlist head ); // 列舉 - 遞迴實現 - 呼叫函式時引數為head->next,避免處理頭結點

status insert_x( linkedlist head, elemtype x, int i ); // 在第i個位置進行插入

elemtype get( linkedlist head, int i ); // 查詢位於第i位的元素

status delete_x( linkedlist head, elemtype x ); // 刪除值為x的元素

status delete_x_i( linkedlist head, int i ); // 刪除位於第i個位置的元素

int get_length( linkedlist head ); // 求單鏈表長度

bool increase( linkedlist head ); // 判斷單鏈表是否非遞減

status move_min( linkedlist head ); // 移動最小值節點, 空間複雜度o(1)

status min_out( linkedlist head ); // 按非遞減順序輸出單鏈表中元素,並釋放節點所佔的儲存空間,空間複雜度o(1)

status exchange( linkedlist head, linkedlist p ); // 交換節點p和p的後繼

#endif

linkedlist create_by_head( const elemtype* arr, int n )

linkedlist head = new listnode();

if( !head )

for( int i = 0; i < n; ++i )

cur->next = head->next;

head->next = cur;

}return head;

}

linkedlist create_by_tail( const elemtype* arr, int n )

linkedlist head = new listnode();

if( !head )

linkedlist tail = head;

for( int i = 0; i < n; ++i )

tail->next = cur;

tail = cur;

}return head;

}

status destroy( linkedlist head )

std::stack

stk;

linkedlist p = head->next;

while( p )

while( !stk.empty() )

return ok;

}

status destroy1( linkedlist head )

return ok;

}

status print( linkedlist head )

linkedlist p = head->next;

while( p )

std::cout

<< std::endl;

return ok;

}

status print1( linkedlist head )

return ok;

}

status insert_x( linkedlist head, elemtype x, int i )

linkedlist p = head->next;

int j =

1; while( p && j < i -

1 )

if( p )

cur->next = p->next;

p->next = cur;

return ok;

}else

}

elemtype get( linkedlist head, int i )

linkedlist p = head->next;

int j =

1; while( p && j < i )

if(p)

else

}

status delete_x_i( linkedlist head, int i )

linkedlist pre = head;

linkedlist p = head->next;

int j =

1; while( p && j < i )

if( p )

else

}

status delete_x( linkedlist head, elemtype x )

linkedlist pre = head;

linkedlist p = head->next;

while( p && p->

data

!= x )

if( p )

else

}

int get_length( linkedlist head )

intlength = 0;

linkedlist p = head->next;

while( p )

return

length;

}

bool increase( linkedlist head )

linkedlist pre = head->next; // pre指向第乙個節點

if(!pre)

return

false;

linkedlist p = pre->next;

while( p )

return

true; // 不存在逆序

}

線性表 帶頭結點單鏈表的實現

帶頭結點的單鏈表 vs2010 除錯 include include include typedef struct linknode 獲得鍊錶長度 int get length struct linknode l while trace next null return length 插入節點 向p...

c資料結構線性表之單鏈表 帶頭結點 基本操作

include include define maxsize 10 define elemtype char define ok 1 define error 0 typedef struct node 結點型別定義 node,linklist linklist為結構體指標型別 換行函式 void ...

線性表之單鏈表

cpp view plain copy linkedlist linc 2013.2.26 include include include define ok 1 define error 1 define ture 1 define false 0 struct node typedef stru...