單鏈表的基本操作

2021-08-08 09:38:49 字數 2078 閱讀 4798

今天我們來看看單鏈表的基本操作:初始化,插入(頭插、尾插),查詢,刪除,判空,求長,摧毀,逆置。

.cpp:

#include #include #include #include "list.h"

void initlist(list plist)

//頭插

bool insert_head(list plist,int val)

//尾插

bool insert_tail(list plist,int val)

node *search(list plist,int key)

} return null;

}//查詢key的前驅

static node *searchpri(list plist,int key)

} return null;

}bool delete(list plist,int key)

node *q = p->next;

p->next = q->next;//將q從鍊錶中剔除

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

free(q);

return true;

}bool isempty(list plist)

int getlength(list plist)

return count;

}void show(list plist)

printf("\n");

}void clear(list plist)

void destroy(list plist)

/* node *p = plist->next;//第二種方法

//node *q = p->next;//bug

node *q;

while(p != null)

plist->next = null;

*/}void revers(list plist)//逆置

node *p = plist->next;

node *q;

plist->next = null;//

while(p != null)

//第二種方法

#if 0

/*if(plist->next->next==null ||

plist==null || plist->next==null

)*///error

if(plist==null || plist->next==null ||

plist->next->next==null)

node*p = plist->next;

node *q = p->next;

node *s;

p->next = null;

while(q != null)

plist->next = p;

#endif

}

.h:

#pragma once

//帶頭節點的單鏈表

typedef struct node

node,*list;//list == node*

void initlist(list plist);//node *plist

//頭插

bool insert_head(list plist,int val);

//尾插

bool insert_tail(list plist,int val);

node *search(list plist,int key);//list

node *search_before(list plist,int key);

bool delete(list plist,int key);

bool isempty(list plist);

int getlength(list plist);

void show(list plist);

void clear(list plist);

void destroy(list plist);

void revers(list plist);

單鏈表基本操作

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