單鏈表的基本操作

2021-08-18 18:04:31 字數 3028 閱讀 7265

主要是鍊錶的增刪改查的基本功能,做了乙個簡單的集錦。

大一學的鍊錶,大二的我回來總結一哈,彌補過去不努力的自己。即使是現在寫,自己也忘記了不少,**除錯也花了很多時間,所以建議各位程式猿多多練習,不要學著後面,忘了前面。

#include#includeusing namespace std;

class node;

class list

int length();

bool isempty()

bool get_data(int i, int &x);//獲得鍊錶指定位置i的值

bool get_succ(int i, int &x);定義取後繼元素函式

bool getprior(int i, int &x);

bool replace_data(int data, int i);

bool insert_data(int data, int i);//插入資料

bool delete_data(int i);

bool find_data(int x, int &result);

void print_list();

~list()

}};int list::length()

return counter;

}bool list::get_data(int i, int &x)

current = head->next;

while (current != null&&j < i)

x = current->data;

return true;

}bool list::get_succ(int i, int&x)

current = head->next;

while (current->next != null&&jnext;

} if (current->next != null)

else

}bool list::getprior(int i, int &x)

previous = head;//previous作為指向第i-1個節點的指標

current = head->next;

while (current != null && jnext;

} if (previous != head)

else

}bool list::replace_data(int data, int i)

current = head->next;

while (current != null&&jnext;

} current->data = data;

return true;

}bool list::insert_data(int data, int i)

newnode = new node;

if (newnode == null)

int j = 1;

newnode->data = data;

newnode->next = null;

previous = head;

current = head->next;

while (current != null&&jnext;

} newnode->next = current;

previous->next = newnode;

return true;

}bool list::delete_data(int i)

if (i<1 || i>length())

int j = 1;

previous = current = null;

current = head->next;

while (current != null&&j < i)

if (current->next != null)else

}bool list::find_data(int x, int &result)

} cout << "鍊錶中沒有" << x << "這個值!" << endl;

return false;

}void list::print_list()

cout << endl;

}int main()

non_seqlist1.print_list();

non_seqlist1.insert_data(404, 4);

non_seqlist1.print_list();

non_seqlist1.insert_data(809, -4);

non_seqlist1.print_list();

non_seqlist1.delete_data(8);

non_seqlist1.print_list();

non_seqlist1.delete_data(3);

non_seqlist1.print_list();

non_seqlist1.delete_data(5);

non_seqlist1.print_list();

int i_data = -1;

non_seqlist1.replace_data(0, i_data);

non_seqlist1.replace_data(5, i_data);

non_seqlist1.print_list();

non_seqlist1.replace_data(1, i_data);

non_seqlist1.replace_data(4, i_data);

non_seqlist1.print_list();

list non_seqlist2;

for (int i = 1; i < 5; i++)

non_seqlist2.print_list();

int f_data = -1;

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

f_data = -1;

for (i = 0; i < +6; i++)

} return 0;

}

單鏈表基本操作

include include include include includeusing namespace std typedef struct node node,plinklist plinklist createfromhead node pstnode node malloc sizeof...

單鏈表基本操作

單鏈表的初始化,建立,插入,查詢,刪除。author wang yong date 2010.8.19 include include typedef int elemtype 定義結點型別 typedef struct node node,linkedlist 單鏈表的初始化 linkedlist...

單鏈表基本操作

include using namespace std define namelenth 20 define ok 0 define error 1 typedef struct flagnode node 生成結點 inline node newnode 銷毀化煉表 void destroylin...