带有头结点的循环单链表的相关操作

来源:互联网 发布:mac的输入法快捷键 编辑:程序博客网 时间:2024/06/06 03:07

link.h

//带有头结点的循环链表的建立#include<iostream>using namespace std;struct listnode;typedef struct listnode *link;struct listnode{int element;link next;};link Init_Link();//初始化链表int IsEmpty_Link(link );//判断链表是否为空void MakeEmpty_Link(link);//链表置空,释放内存void InsertNode_link(link ,int );//插入结点void DeleteNode_link(link,int );//删除结点int FindNode_Link(link,int );//查找结点void Print_link(link);//打印链表

link.cpp

#include "link.h"#include<stdlib.h>link Init_Link()//初始化带有头结点的链表{link head=(link)malloc(sizeof(struct listnode));if(head==NULL){cout<<"insufficient memory!"<<endl;exit(0);}head->next=head;return head;}int IsEmpty_Link(link head ){return head->next==head;}void MakeEmpty_Link(link head){    cout<<"链表置空中..."<<endl;link p=head->next;while(p!=head)//在结点P的指针域的指针指向头结点时,循环结束{link tmp;tmp=p;p=p->next;//必须先指定后继结点,在对该结点进行释放内存的工作free(tmp);}head->next=head;}void InsertNode_link(link head ,int x )//保证按序插入{link newnode=(link)malloc(sizeof(struct listnode));newnode->element =x;   if(head->next==head)//若开始时,链表中只有一个头结点{head->next=newnode;newnode->next=head;}else{link p;//pre指向的结点为p所指向的结点的前驱结点link pre=head;//为了避免插入的第二个实结点比第一个实结点小,导致for循环只运行一次便break,从而使pre没有被赋值的情况,在这里先为pre赋值    for(p=head->next;p!=head;pre=p,p=p->next)    {   if(p->element > newnode->element)  break;    }    pre->next=newnode;    //if(p==head)  //这里实际上不用用条件判断,直接newnode->next=p即可   //newnode->next=head;    //else   //newnode->next=p;newnode->next=p;}}void DeleteNode_link(link head,int x){link p,pre;link tmp;if(IsEmpty_Link(head)){cout<<"链表已为空"<<endl;exit(0);}if(!FindNode_Link(head,x)){cout<<"元素为x的节点没有找到,不能进行删除操作!"<<endl;exit(0);}for(p=head->next;p!=head;pre=p ,p=p->next){if(p->element==x)break;}tmp=p;pre->next=p->next;free(tmp);}int FindNode_Link(link head,int x ){link p;for(p=head->next;p!=head;p=p->next){if(p->element==x)return 1;}return 0;}void Print_link(link head){link p;for(p=head->next;p!=head;p=p->next){cout<<p->element<<" ";}cout<<endl;}

main.cpp

/******************************************************************************************* *name:jae chia                                                                            *                                       *date:2014.6.19                                                                           * *purpose:带有头结点的循环单链表的建立,插入,删除等操作                                    * *version:1.0                                                                              * *******************************************************************************************/#include"link.h"int main(void){link head;head=Init_Link();if(IsEmpty_Link(head)){cout<<"now,the link is empty!"<<endl;}cout<<"then insert the element into the link..."<<endl;InsertNode_link(head,3);InsertNode_link(head,2);InsertNode_link(head,1);//Print_link(head);//MakeEmpty_Link( head);InsertNode_link(head,5);InsertNode_link(head,4);cout<<"print the link:"<<endl;Print_link(head);cout<<endl<<"now ,delete the node which element is 3"<<endl;DeleteNode_link(head,3);cout<<endl<<"print the link "<<endl;Print_link(head);return 0;}



0 0