head代表第乙個指標,前面加了乙個dummy節點,方面用來處理頭指標。
#include using namespace std;struct linknode
};//建立只有空鍊錶,返回乙個dummy節點
linknode * createlink()
linknode * initlink(linknode *dummy)
; linknode *tmp,*head;
tmp = dummy;
//使用的是尾插入法
for(int i=0;inext=p;
tmp = tmp->next;
} head = dummy->next;
delete dummy;
return head;
}//顯示鍊錶
void showlist(linknode *head)
printf("\n");
return;
}//找值為x的前驅
linknode * findprevious(linknode *head,int x)
//在某個節點後,增加乙個新的節點
void addnewnodeafter(linknode *insertpos,int x)
//在某個節點insertpos前插入乙個新的節點
linknode* addnewnodebefore(linknode *head,int insertpos,int x)
//在第k個元素之前,增加乙個新的節點
linknode * insertkthnode(linknode *head,int k,int x)
//刪除值為x的節點,用dummy節點
linknode * deletenode(linknode *head,int x)
//刪除值為x的節點,不用dummy節點
linknode* deletenode2(linknode *head,int x)
//找到x的前驅
linknode *pre = findprevious(head,x);
if(null == pre) return head;
linknode *p = pre->next;
pre->next = p->next;
delete p;
return head;
}//reverse 乙個鍊錶
linknode *reverse(linknode *head)
return pre;
}//摧毀鍊錶
void desroylist(linknode *head)
return;
}void main()
修改前的。
#include using namespace std;
struct node
;//建立只有頭結點的空鍊錶
struct node * createlist()
head->next = null;
head->data = -1;
return head;
}//頭插法建立鍊錶
bool insertfromhead(struct node * head,int x)
p->data = x;
p->next = head->next;
head->next = p;
return true;
}//尾插法建立鍊錶
bool insertfromtail(struct node * head,int x)
//找到目前的最後乙個結點,將新元素插到後面
while(tmp->next)
tmp = tmp->next;
p->data = x;
p->next = null;
tmp->next = p;
return true;
}//增加乙個結點,在insertpos後面新增乙個結點
bool addnewnodeafter(struct node * head,struct node * insertpos,int x)
p->next = insertpos->next;
p->data = x;
insertpos->next = p;
return true;
}//查詢含有值為x的前驅p.如果存在多個x的值,我們返回第乙個。
struct node * findprevious(struct node * head,int x)
else
return p;
}//在對應元素insertpos的前面,增加乙個結點
bool insertnewnodebefore(struct node * head,int insertpos,int x)
struct node *p = (struct node *)malloc(sizeof(struct node));
if (null == p)
p->next = pre->next;
p->data = x;
pre->next = p;
return true;
}//在第k個元素之前,增加乙個結點
void insertnodekth(struct node * head,int k,int x)
p->next = pre->next;
p->data = x;
pre->next = p;
return;
}//在第k個元素後面,插入乙個值
void insertkthafter(struct node *head,int k,int x)
p->next = cur->next;
p->data = x;
cur->next = p;
}//刪除值為x的結點
void deletenode(struct node * head,int x)
//reversse.遍歷原來的鍊錶節點,用頭插法重新生成乙個新的鍊錶
struct node * generatererverse(struct node *head)
head_reverse->next = null;
head_reverse->data = 0;
tmp = head->next;
while(tmp!=null)
p->data = tmp->data;
p->next = head_reverse->next;
head_reverse->next = p;
tmp = tmp->next;
} return head_reverse;
}//將這個鍊錶翻轉,不產生新的鍊錶
struct node * reversecurlinklist(struct node *head)
last = cur;
first->next = null;//原來第乙個節點,現在在末尾
head->next = last;//原來最後乙個節點,現在位於第乙個
return head;
}//顯示鍊錶
bool showlist(struct node * head)
printf("\n");
return true;
}//摧毀鍊錶
void deletelist(struct node * head)
}void main()
//insertfromhead(head,1);
//insertfromhead(head,2);
//insertfromhead(head,3);
insertfromtail(head,1);
insertfromtail(head,2);
insertfromtail(head,3);
insertfromtail(head,4);
insertfromtail(head,5);
showlist(head);
//查詢前驅
//pre = findprevious(head,3);
//if (pre!=null)
//printf("the pre value is: %d\n",pre->data);
//在元素i前面,增加乙個元素
//insertnewnodebefore(head,5,10);
//showlist(head);
//在第k個元素前,增加乙個元素
//insertnodekth(head,1,100);
//showlist(head);
//在第k個元素後面,增加乙個元素
//insertkthafter(head,1,99);
//showlist(head);
//產生乙個全新的翻轉鍊錶
//struct node *head_reverse = generatererverse(head);
//showlist(head_reverse);
//將當前的鍊錶翻轉,不產生新的鍊錶
head = reversecurlinklist(head);
showlist(head);
//刪除值為x的結點
//deletenode(head,100);
//showlist(head);
//銷毀list
//deletelist(head);
//showlist(head);
return;
}
鍊錶的基本操作
include include include include using namespace std struct listnode void initnode listnode node bool isempty listnode head void pushfront listnode hea...
鍊錶的基本操作
鍊錶操作是最基本的 必須掌握的知識點,最好滾瓜爛熟,透徹理解。工作時間短用的也不夠頻繁,還是總結一下比較好,以加強鞏固。1.單鏈表 結點形式 區分幾個概念 首節點 第乙個元素所在節點。頭指標 指向首節點的指標。頭結點 為了操作方便,在第乙個節點之前附設的乙個結點,此時指向頭結點的為頭指標。基本操作 ...
鍊錶的基本操作。。。
include node.h 列印鍊錶 void print node head printf n 從尾部插入 void insert tail node head,const int d while t next null t next p p next null 從頭部插入 void inser...