日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

循环链表的插入和删除

發布時間:2025/4/16 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 循环链表的插入和删除 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

循環鏈表可以用來使計算機處理內存工作區或輸出至數據緩沖區。

循環鏈表的插入和刪除

#include"iostream"
#include
"stdlib.h"
using namespace std;

struct clist
{
int data;
struct clist *next;
};
typedef
struct clist cnode;
typedef cnode
*clink;

/*-----循環鏈表的輸出------*/

void printclist( clink head)
{
clink ptr;
head
=head->next;
ptr
=head;
do
{
printf(
"[%d]",ptr->data);
ptr
=ptr->next;
}
while(head!=ptr && head !=head->next);
printf(
"\n");

}
/*-----循環鏈表的結點插入----*/

clink insertnode(clink head,clink ptr,
int value)
{
clink new_node;
new_node
=(clink) malloc(sizeof(cnode));
if(!new_node)
return NULL;
new_node
->data=value;
new_node
->next=NULL;

if(head==NULL)
{
new_node
->next=new_node;
return new_node;
}
if(ptr==NULL)
{
/*----情況1:插在第一結點之前---*/
new_node
->next=head->next;
head
->next->next=new_node;
}
else
{
/*-----情況2:插在結點之后-------*/
new_node
->next=ptr->next;
ptr
->next=new_node;
}
if(ptr==head)
head
=new_node;
return head;
}
/*---循環鏈表結點刪除---*/
clink deletenode(clink head,clink ptr)
{
clink previous;
if(head==NULL)
{
/*----情況1:刪除第一個結點----*/
head
->next=ptr->next;
}
else
{
/*--情況2:刪除中間結點---*/
previous
=head;
if(head!=head->next)
while(previous->next!=ptr)
previous
=previous->next;
previous
->next=ptr->next;
}
if(ptr==head)
head
=previous;
free(ptr);
return head;
}

/*使用插入結點的方式來創建鏈表,完成后將鏈表內容輸出,然后刪除前后兩結點*/

int main()
{
clink head
=NULL;
int list[6]={9,7,3,4,5,6};
int i;

head
=insertnode(head,head,list[0]);
printf(
"創建第一個結點: ");
printclist(head);
/*---情況1:插在第一結點前----*/
head
=insertnode(head,NULL,list[1]);
printf(
"插入第一結點之前: ");
printclist(head);
for(i=2;i<6;i++)
{
/*---情況2:插在結點之后-----*/
head
=insertnode(head,head->next,list[i]);
printf(
"插入結點之后: ");
printclist(head);
}
/*---情況1:刪除第一個結點---*/
head
=deletenode(head,head->next);
printf(
"刪除第一個結點: ");
printclist(head);
/*--刪除最后一個結點--*/
printf(
"刪除最后一個結點: ");
head
=deletenode(head,head);
printclist(head);
}

?

轉載于:https://www.cnblogs.com/FCWORLD/archive/2010/11/20/1882463.html

總結

以上是生活随笔為你收集整理的循环链表的插入和删除的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。