單鏈表的實現

2021-07-28 05:28:37 字數 3244 閱讀 6059

單鏈表的實現:

標頭檔案list.h

#ifndef __list_h

#define __list_h

#include

#include

#include

typedef int datatype;

typedef struct node

node, *pnode, *plist;

void initlist(plist* pplist);

void pushback(plist* pplist, datatype d);

void pushfront(plist* pplist, datatype d);

void popback(plist* pplist);

void popfront(plist* pplist);

int getlistlength(plist plist);

void printflist(plist);

pnode find(plist plist, datatype d);

void remove(plist* pplist, datatype d);

void removeall(plist* pplist, datatype d);

void insert(plist* pplist, pnode pos, datatype d);

void erase(plist* pplist, pnode pos);

void destroylist(plist* pplist);

#endif

test.c

#define _crt_secure_no_warnings 1

#include "list.h"

void menu()

int main()

else

}break;

case 9:

printf("請輸入要刪除的元素:");

scanf("%d", &data);

remove(&plist, data);

break;

case 10:

printf("請輸入要刪除的元素:");

scanf("%d", &data);

removeall(&plist, data);

break;

case 11:

printf("請輸入要插入的位置:");

scanf("%d", &data);

ret = find(plist, data);

if (ret == null)

else

break;

case 12:

printf("請輸入要刪除的位置:");

scanf("%d", &data);

ret = find(plist, data);

if (ret == null)

else

break;

case 13:

destroylist(&plist);

break;

case 0:

exit;

break;

default:

printf("選擇錯誤,請重新輸入\n");

break;

} } while (input);

return 0;

}list.c

#include "list.h"

void initlist(plist* pplist)

pnode buynode(datatype d)

pnode->data = d;

pnode->next = null;

return pnode;

}void pushback(plist* pplist, datatype d)

else

cur->next = pnewnode;

printf("插入元素成功\n"); }}

void pushfront(plist* pplist, datatype d)

else }

void popback(plist* pplist)

if (cur->next==null)

while (cur->next->next != null)

free(cur->next);

cur->next = null;

printf("刪除成功\n");

}void popfront(plist* pplist)

if (cur->next == null)

del = *pplist;

*pplist = cur->next;

free(del);

printf("刪除成功\n");

}int getlistlength(plist plist)

printf("該鍊錶的元素個數為:%d\n", count);

}pnode find(plist plist, datatype d)

cur = cur->next;

} return null;

}void remove(plist* pplist, datatype d)

else //非第乙個

return;

} prev = cur;

cur = cur->next;

} if (cur== null) }

void removeall(plist* pplist, datatype d)

while (cur)

else //非第乙個

prev = cur;

cur = cur->next;

} else

}printf("該資料刪除成功\n");

}void insert(plist* pplist, pnode pos, datatype d)

else }

void erase(plist* pplist, pnode pos)

else

else

}}void destroylist(plist* pplist)

*pplist = null;

printf("銷毀成功\n");

}void printflist(plist plist)

else

printf("\n");

}}

單鏈表的實現

include includetypedef struct node 定義鍊錶 snode snode creat 建立鍊錶的函式 q next null return head int length snode head 測鍊錶的結點數 return i void display snode he...

單鏈表的實現

單鏈表夜市線性表的一種表現形式,乙個表節點由乙個資料空間和乙個指標域組成。指標域記錄下乙個結點的位址。鍊錶在插入,刪除功能中效率高。但是讀取某個結點的時候需要順序讀取。效率不如順序儲存形式。下面是一些鍊錶實現的 鍊錶.cpp 定義控制台應用程式的入口點。include stdafx.h define...

單鏈表的實現

單鏈表是資料結構中重要並且基礎的一環,學習資料結構就需要知道單鏈表有的常用操作。1 單鏈表的頭插式建立 2 單鏈表的尾插式建立 3 單鏈表的長度計算 4 單鏈表的列印輸出 5 單鏈表的釋放操作 6 單鏈表是否為空判斷 7 單鏈表在指定index插入指定元素 8 單鏈表刪除指定index的節點 9 單...