双向循环链表,最易读懂的插入,删除,查找,并且显示长度

loading 分享 2026-8-13 下载文档

#include #include

typedef struct list {

int data;

struct list *left,*next; }List;

////*************** 建立空链表 *******************///// List * CreatList( void ) {

List * head=(List *)malloc(sizeof(List)); if(head==NULL) { printf(\分配失败!\\n\ return 0; } head->left=head->next=head; //双向链表结束标志为,尾到了头! return head; }

int InsertList(List * head,int value) //头结点是确定的,不需要每次都返回头结点了!!! { List *newnode,*p; if(head==NULL) {

//你建立的是个带头结点的表,如果表是头是空,那么应该报错了! printf(\!\\n\ return -1; } newnode=(List *)malloc(sizeof(List)); if(newnode==NULL) { printf(\分配失败!\\n\ return -1; } newnode->data=value; p=head ; while(p->next!=head) //这里是为了找到表尾

{ p=p->next; }

/**//**//**//**//**//**//**//**//**//**//**//**//**//**/

/***************将数据插入表尾位置*********************/ /**/ newnode->next=p->next ; /**/ /**/ p->next=newnode; /**/ /**/ newnode->left=p; /**/ /**//**//**//**//**//**//**//**//**//**//**//**//**//**/

////free(newnode); //这个结点(指针)你还要用,不能释放。 return 0; }

void Find(List *head,int find)///查找并显示位置 { List *p; int i=0; p=head; while(p!=head) { p=p->next; i+=1; if(p->data==find) { printf(\要查找的数 %d 是第 %d 个\\n\ } } if(p->data!=find && p->next==head) { printf(\连表里没有要查找的数%d!\\n\ } }

void print(List *head)//打印链表,并显示长度 {

//head=(List *)malloc(sizeof(List)); 遍历表不需要新分配空间!这样做会丢失掉原有的地址!! 改成如下: int i=0; List *p=head->next ; while(p!=head) //head一直要保证不变,这样,你才能正常转一圈 { printf(\ p=p->next; i+=1; } printf(\链表长度为 %d \\n\ printf(\ }

List * Delete(List *head ,int x)///删除数据操作 { List *p; int i=0; p=head->next; p=p->next; while(p->next!=head) { p=p->next; i+=1; if(p->data!=x && p->next==head) { printf(\没有要删除的数%d!\\n\ } else if(p->data==x) {

/**//**//**//**//**//**//**//**//**//**//**//**//**//**/

/*************** 将数据删除操作 *********************/ /**/ p->left->next=p->next; /**/ /**/ p->next->left=p->left; /**/ /**/ p=p->next; /**/ /**//**//**//**//**//**//**//**//**//**//**//**//**//**/ printf(\要删除的数 %d 是第 %d 个\\n\ }

} return head; }

int main() { List *head=NULL;

//CreatList(head); 这句带不回来指针值的!!! head= CreatList(); InsertList(head,10); print(head); // Delete(head,10); print(head); Find(head,10);

return 0; }


双向循环链表,最易读懂的插入,删除,查找,并且显示长度.doc 将本文的Word文档下载到电脑
搜索更多关于: 双向循环链表,最易读懂的插入,删除,查找,并且显示长度 的文档
相关推荐
相关阅读