2 1單鏈表的基本操作

2021-07-27 10:39:59 字數 4526 閱讀 2215

/***********************

date:2017-2-24

author:sedate

description:單鏈表的基本操作

***********************/

#include

#include

#include

#include

using namespace std;

#define int_min 0x80000000

#define ok 1

#define error 0

#define

true

1#define

false

0typedef int elementtype;//資料元素的型別為整型

typedef int status; //表示函式返回的狀態值

typedef struct node

node,*listnode;

typedef struct linklist

*linklist,head;

/** summary: 頭插法建立單鏈表,鍊錶儲存1-100之間的隨機數

* parameters:

list: the linklist

n: the length of linklist

* return: the status

*/status createlisthead(linklist &

list,int n)

list

->size =

0; list

->head->

data

= int_min;

list

->head->next =

null;

srand(time(0));

for (int i =

0; i < n; i++)

p->

data

= rand() %

100+

1; p->next =

list

->head->next;

list

->head->next = p;

++list

->size;

}return ok;}/*

* summary: 尾插法建立單鏈表,鍊錶儲存隨機數

* parameters:

list: the linklist

n: the length of linklist

* return: the status

*/status createlisttail(linklist &

list, int n)

list

->size =

0; q =

list

->head;

q->

data

= int_min;

q->next =

null;

srand(time(0));

for (int i =

0; i < n; i++)

p->

data

= rand() %

100+

1; p->next =

null;

q->next = p;

q = p;

++list

->size;

}return ok;}/*

* summary: 按位置查詢元素,從1開始

* parameters:

list: the linklist

pos: the position where the elem to be found

e: the elem found

* return: the status

*/status find_by_pos(linklist &

list, int pos,elementtype *e)

listnode p =

list

->head;

int i =

0; while (i*e = p->

data;

return ok;}/*

* summary: 按值查詢元素,從1開始

* parameters:

list: the linklist

pos: the postion found

e: the elem to get the position

* return: the status

*/status find_by_value(linklist &

list, int pos, elementtype *e)

else

}return error;}/*

* summary: 按位置插入元素,pos從1開始計數

* parameters:

list: the linklist

pos: the position where the elem to be found

e: the elem

* return: the status

*/status insert(linklist &

list, int pos, elementtype *e)

if (!(q = (listnode)malloc(sizeof(node))))

q->

data

=*e;

q->next = p->next;

p->next = q;

++list

->size;

return ok;}/*

* summary: 在頭部插入指定元素

* parameters:

list: the linklist

e: the elem

* return: the status

*/status insertfirst(linklist &

list,elementtype *e)

p->

data

=*e;

p->next =

list

->head->next;

list

->head->next = p;

++list

->size;

return ok;}/*

* summary: 在尾部插入指定元素

* parameters:

list: the linklist

e: the elem

* return: the status

*/status insertfirst(linklist &

list, elementtype *e)

p->

data

=*e;

p->next =

null;

q =list

->head;

while (q->next !=

null)

q->next = p;

++list

->size;

return ok;}/*

* summary: 刪除指定位置的元素

* parameters:

list: the linklist

pos: the position where an elem to be deleted

* return: the status

*/status delete_by_pos(linklist &

list, int pos)

q = p->next;

p->next = p->next->next;

free(q);

--list

->size;

return ok;}/*

* summary: 刪除頭部元素

* parameters:

list: the linklist

* return: the status

*/status deletefirst(linklist &

list)

/** summary: 刪除尾部元素

* parameters:

list: the linklist

* return: the status

*/status insertfirst(linklist &

list)

status insertfirst(linklist &

list)

free(p->next);

p->next =

null;

--list

->size;

return ok;

}

單鏈表基本操作

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...