單鏈表 建立 插入 刪除 查詢 反轉等操作

2021-06-09 05:39:05 字數 2589 閱讀 2907

#ifndef _list_h

#define _list_h

#include #include #define notfound null;

typedef struct list

node;

typedef struct list *pnode;

typedef pnode plist;

typedef pnode position;

plist creatlist(int *array, int len);

position findnode(plist head, int x);

plist lastinsertlist(plist head, int insertvalue);

plist insertlist(plist head, int val, int insertvalue);

plist deletenode(plist head, int delvalue);

plist reversallist(plist head);

plist freelist(plist head);

void printlist(plist head);

#endif /* _list_h */

#include "singlelist.h"

plist creatlist(int *array, int len)

head->next = null;

head->value = array[0];

tmp = head;

for(i = 1; i < len; i++)

ptr->value = array[i];

ptr->next = null;

tmp->next = ptr;

tmp = tmp->next;

} return head;

} position findnode(plist head, int x)

return notfound;

}//在末節點插入

plist lastinsertlist(plist head, int insertvalue)

//在值為val的節點前插入

plist insertlist(plist head, int val, int insertvalue)

while(temp)

temp = temp->next;

ptr = ptr->next;

} return notfound;

}plist deletenode(plist head, int delvalue)

while(temp)

temp = temp->next;

ptr = ptr->next;

} return notfound;

}plist reversallist(plist head)

temp->next = ptr;

head->next = null;

return temp;

}plist freelist(plist head)

return head;//注意記憶體分配問題

}void printlist(plist head)

printf("the list is : ");

while(head)

printf("\n");

}

#include "singlelist.h"

int main(void)

; int len = sizeof(array)/sizeof(*array);

int insertvalue = 100;

position p;

plist head = creatlist(array,len);

printlist(head);

head = reversallist(head);

printlist(head);

p = findnode(head,5);

printf("the position value is : %d\n",p->value);

//在元素為10前面插入100

head = insertlist(head,10,insertvalue);

printlist(head);

//在末節點後面插入10000

head = lastinsertlist(head,10000);

printlist(head);

//刪除10000的節點

head = deletenode(head,10000);

printlist(head);

head = deletenode(head,1);

printlist(head);

head = deletenode(head,5);

printlist(head);

//釋放鍊錶

head = freelist(head);

printlist(head);

}

單鏈表的建立 插入刪除等操作

utili.h ifndef utili h define utili h include using namespace std include define bool int define true 1 define false 0 endif list.h ifndef list h defi...

單鏈表的建立 插入刪除等操作

單鏈表的建立 插入刪除等操作 c語言的指標以及結構體沒有學好,導致老師在說鍊錶時就已經懵了一圈了。typedef struct 和僅僅struct 的區別是花了無數的耐心才在乙個偶然的機會發現的。2 剛開始做資料結構實驗的時候一臉懵逼,不知道要做什麼,比如說建立鍊錶,我在螢幕上看不見我建立的鍊錶,我...

單鏈表 建立插入刪除

建立乙個工程,在其中新增 list.h 標頭檔案,list.cpp原始檔和main.cpp原始檔。標頭檔案list.h中進行資料型別抽象 adt 宣告了乙個結構體和對應的操作,如下 ifndef list h define list h typedef struct list list 函式宣告 l...