存儲結構
typedef struct Node{
E element;
struct Node *next;
}*node;
創建(初始化)
void initList(node nd){
nd->next=NULL;
}
插入
void insertNode(node head,E e){
//有這個循環就是尾插,否則就是頭插
while(head->next!=NULL)
head=head->next;
// new一個node存我們要插入結點的數據
node nd=(node)malloc(sizeof(struct Node));
nd->element =e;
//令我們的nd指向head指向的地方 ,其實就是NULL
nd->next=head->next;
//再把head的指向改為nd
head->next=nd;
}
刪除
void delteNode(node n){
//要刪除第幾個,就讓n后面跟幾個->next,賦值就多指向一個
n->next->next=n->next->next->next;
}
刪除重復元素
void deleteDup(node nd) {
if (nd == NULL) return;
node current = nd;
while (current != NULL) {
node runner = current;
while (runner->next != NULL) {
if (current->element == runner->next->element) {
node tmp = runner->next;
runner->next = runner->next->next;
free(tmp);
} else {
runner = runner->next;
}
}
current = current->next;
}
}