單鏈表的增刪查改等操作實現

2021-08-04 14:24:07 字數 3075 閱讀 8920

//.h

#ifndef __link_list__

#define __linklist__

#include

#include

#include

typedef int datetype;

typedef struct node

node,*pnode,list;

//初始化鍊錶和銷毀鍊錶

void initlinklist(list

** pplist);

void destroylist(list

**pplist);

//尾部插入和刪除元素

void pushback(list

**pplist,datetype x);

void popback(list

**pplist);

//頭部插入和刪除元素

void pushfront(list

**pplist, datetype x);

void popfront(list

**pplist);

//查詢

pnode find(list

*plist, datetype x);

//指定位置插入和刪除

void insert(list

**pplist,int pos, datetype x);

void erase(list

**pplist, int pos);

//刪除指定元素

void remove(list

**pplist, datetype x);

//刪除指定所有元素

void removeall(list

**pplist, datetype x);

//遍歷鍊錶

void display(list

* plist);

#endif

//.c

#include

"linklis.h"

void display(list

* plist)

printf("\n");

}void initlinklist(list

** pplist)

void destroylist(list

**pplist)

}//尾插

void pushback(list

**pplist,datetype x)

temp->

data

= x;

temp->next =

null;

if (head ==

null)//空煉表處理

while (head->next !=

null)//非空煉表處理

head->next = temp;

}//尾刪

void popback(list

**pplist)

while (head->next !=

null)//多節點處理

free(ptemp->next);

ptemp->next =

null;

}//頭插

void pushfront(list

**pplist,datetype x)

node->

data

= x;

node->next =

null;

node->next =

*pplist;

*pplist = node;

}//頭刪

void popfront(list

**pplist)

//查詢

pnode find(list

*plist, datetype x)

return

null;

}//指定位置插入,頭節點位置為1

void insert(list

**pplist, int pos, datetype x)

temp->

data

= x;

temp->next =

null;

if (head ==

null)//空煉表處理

if (pos <=

1)//插入位置為1或小於1處理

while( (i < pos-

1)&&(head->next !=

null))//插入位置大於1處理

temp->next = head->next;

head->next = temp;

}//刪除指定位置,頭節點位置為1

void erase(list

**pplist, int pos)

//刪除位置小於等於1時處理

if (pos <=

1)

//刪除位置大於1時處理

while (i < pos && head->next!=

null)

head = temp->next->next;

free(temp->next);

temp->next = head;

}//刪除指定元素

void remove(list

**pplist, datetype x)

//當指定元素為非頭節點時處理

while (head!=

null)

temp = head;

head = head->next;

}}//刪除指定所有元素

void removeall(list

**pplist, datetype x)

if (head->

data

== x)

//其他情況時

//使temp儲存當前節點的位置

temp = head;

//使head指向下乙個節點

head = head->next;

}}//.c

#include

"linklis.h"

int main()

單鏈表的增刪查改

include includetypedef struct nodenode,linklist int num 查詢給定值的結點,返回結點指標 node findvalue char c,node head return head 查詢給定位置的結點的值 char findindex value n...

單鏈表的增刪查改

本篇部落格主要介紹c資料結構中的單鏈表有關的增刪查改操作,並且介紹列表的快慢指標,鍊錶的逆置和合併等用法,廢話不說直接上 pragma once include include include typedef int datetype typedef struct plistnode plistno...

單鏈表的增刪查改

鍊錶是一種物理儲存單元上非連續 非順序的儲存結構,資料元素的邏輯順序是通過鍊錶中的指標鏈結次序實現的。鍊錶由一系列結點 鍊錶中每乙個元素稱為結點 組成,結點可以在執行時動態生成。每個結點包括兩個部分 資料域和指標域 特點 1 可以方便的進行擴充。2 可以方便的刪除和插入。例子如下 include i...