定義鏈表結(jié)構(gòu)體(節(jié)點(diǎn))
typedef struct Node{
int data;
struct Node * pNext;
}NODE,*PNODE;
NODE 等價(jià)于
typedef struct Node
PNODE 等價(jià)于
typedef struct * Node
1.創(chuàng)建一個(gè)鏈表
/**
* 創(chuàng)建一個(gè)集合,返回頭節(jié)點(diǎn)
* @return [description]
*/
PNODE createList(){
PNODE pHead=(PNODE)malloc(sizeof(NODE));
pHead->pNext=NULL;
return pHead;
}
2.向鏈表中添加一個(gè)節(jié)點(diǎn)
/**
* 添加一個(gè)節(jié)點(diǎn)
* @param pHead [description]
* @param node [description]
*/
void append(PNODE pHead,PNODE node){
while(pHead->pNext!=NULL){
pHead=pHead->pNext;
}
pHead->pNext=node;
}
3.向指定的位置添加一個(gè)節(jié)點(diǎn)
/**
* 向指定位置添加一個(gè)節(jié)點(diǎn) 當(dāng)index超過索引會(huì)將元素掛到鏈表的結(jié)尾
* @param pHead [description]
* @param node [description]
* @param index [description]
*/
void insert(PNODE pHead,PNODE node,int index){
PNODE temp=pHead;
while(temp->pNext!=NULL){
if (index==0&&node)
{
/* code */
node->pNext=temp->pNext;
temp->pNext=node;
return;
}
index--;
temp=temp->pNext;
}
}
4.刪除指定位置的結(jié)點(diǎn)
/**
* 刪除指定位置的數(shù)據(jù)
* @param pHead [description]
* @param index [description]
*/
PNODE delete(PNODE pHead, int index){
PNODE r;
PNODE temp=pHead;
if (index<=0){
//如果是刪除第一個(gè)元素
r=pHead;
pHead=pHead->pNext;
free(r);
return pHead;
}
while(temp->pNext!=NULL){
if (index==0){
//刪除
//首先保存temp所指向的結(jié)點(diǎn)的指針
r=temp->pNext;
//將temp->pNext指向下下個(gè)結(jié)點(diǎn)
temp->pNext=temp->pNext->pNext;
//釋放內(nèi)存
free(r);
return pHead;
}
index--;
temp=temp->pNext;
}
printf("%s\n", "\n下標(biāo)越界,指定索引超過鏈表長(zhǎng)度,刪除失敗");
return pHead;
}
5.獲取鏈表的長(zhǎng)度
/**
* 獲取鏈表的長(zhǎng)度
* @param pHead [description]
* @return [description]
*/
int size(PNODE pHead){
int len=0;
while((pHead=pHead->pNext)!=NULL){
len++;
}
return len;
}
6.對(duì)鏈表排序(冒泡排序算法)
/**
* 對(duì)鏈表排序(冒泡排序算法)
* @param pHead [description]
*/
void sortList(PNODE pHead){
int i,j,max;
int length=size(pHead);
PNODE r,q;
r=pHead;
for (int i = 0; i < length-1; ++i)
{
//向下移動(dòng)
r=r->pNext;
//假定在外層的本次循環(huán)中的第一個(gè)節(jié)點(diǎn)的數(shù)據(jù)是最大值
max=r->data;
//把本次循環(huán)的r當(dāng)前節(jié)點(diǎn)賦值給q
q=r;
//從i的下一個(gè)結(jié)點(diǎn)開始,往下找,是否有比max打的結(jié)點(diǎn)
for (int j = i+1; j < length; ++j)
{
//向下移動(dòng)一次
q=q->pNext;
if (q->data>max)
{
//找到比max大的數(shù),交換
max=q->data;
q->data=r->data;
r->data=max;
}
}
}
}
7.遍歷
/**
* 遍歷鏈表
* @param pHead [description]
*/
void traverse(PNODE pHead){
while((pHead=pHead->pNext)!=NULL){
printf("%d\t", pHead->data);
}
}
測(cè)試
int main(){
int i;
PNODE pHead=NULL;
//創(chuàng)建
pHead=createList();
//添加
for(i=0;i<10;i++){
//創(chuàng)建結(jié)點(diǎn)
PNODE pNew=(PNODE)malloc(sizeof(NODE));
pNew->data=i+1;
pNew->pNext=NULL;
//追加結(jié)點(diǎn)
append(pHead,pNew);
}
printf("鏈表長(zhǎng)度是length=%d\n",size(pHead));
traverse(pHead);
pHead=delete(pHead, 0);
printf("\n");
printf("鏈表長(zhǎng)度是length=%d\n",size(pHead));
traverse(pHead);
pHead=delete(pHead, 1);
printf("\n");
traverse(pHead);
PNODE pNew=(PNODE)malloc(sizeof(NODE));
pNew->data=100;
printf("鏈表長(zhǎng)度是length=%d\n",size(pHead));
pNew->pNext=NULL;
insert(pHead,pNew,3);
printf("\n");
printf("鏈表長(zhǎng)度是length=%d\n",size(pHead));
traverse(pHead);
sortList(pHead);
printf("\n");
traverse(pHead);
return 0;
}