#include
#include
#include
using namespace std;
typedef int elemtype;
//定義節點型別
typedef struct node //typedef使定義乙個物件名變為定義乙個型別名
這裡的node,*linkedlist本來是struct node的物件,加了typedef就變成了型別名
node, *linkedlist;
//單鏈表的初始化
linkedlist linkedlistinit()
//單鏈表的建立,頭插法建立單鏈表
linkedlist linkedlistcreatehead()
return l;
}//單鏈表的建立,尾插法建立單鏈表
linkedlist linkedlistcreatebehind()
r->next = null;
return l;
}//單鏈表的插入,在鍊錶的第i個位置插入x的元素
linkedlist linkedlistinsert(linkedlist l, int i, elemtype x)
node *p; //插入的結點為p
p = (node*)malloc(sizeof(node)); // 為p申請乙個結點空間
p->data = x; //將x給p結點中data
p->next = pre->next; //p取代pre指向目標位置
pre->next = p;
return l; //返回鍊錶
}//單鏈表的刪除,在鍊錶中刪除值為x的元素
linkedlist linkedlistdelete(linkedlist l, elemtype x)
pre->next = p->next;
// 這行是關鍵。找到了p,就將p的下乙個結點給pre,這樣就跳過了x
free(p); //將x的空間釋放
return l;
}int main()
printf("\n");
int i;
elemtype x;
printf("請輸入插入資料的位置: ");
scanf_s("%d", &i);
printf("請輸入插入資料的值: ");
scanf_s("%d", &x);
linkedlistinsert(list, i, x); //在鍊錶list的第i個位置插入資料為x的結點
for (start = list->next; start != null; start = start->next)
printf("\n");
printf("請輸入要刪除元素的值: ");
scanf_s("%d", &x);
linkedlistdelete(list, x); //刪除資料為x的結點
for (start = list->next; start != null; start = start->next)
printf("\n");
system("pause");
return 0;
}
單鏈表基本操作
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...