循环单链表学习

来源:互联网 发布:mac unable to launch 编辑:程序博客网 时间:2024/05/21 07:16
#include<stdio.h>#include<stdlib.h>typedef int ElemType;typedef struct CLNode{struct CLNode *next;ElemType data;}CLNode,*LinkList;//初始化循环单链表int  Init_List(LinkList &L){L=(LinkList)malloc(sizeof(CLNode));L->next=L;return 0;}//插入元素int Insert_List(LinkList &L,int pos,ElemType e){int j=0;LinkList p=L->next,s;while(j<pos-1){p=p->next;j++;} s=(LinkList)malloc(sizeof(CLNode));     s->data=e; s->next=p->next; p->next=s; if(p==L)       L=s;return 0;}//删除元素int Delete_List(LinkList &L,int pos,ElemType &e){int j=0;LinkList p=L->next,q;while(j<pos-1){  p=p->next;  j++;}q=p->next;e=q->data;p->next=q->next;if(q->next==L->next)L=p;free(q);return 0;}//得到元素int Get_List(LinkList L,int pos ,ElemType &e){int j=0;LinkList p=L->next;while(j<pos-1){p=p->next;j++;}e=p->next->data; return e;}//清空循环链表int Clear_List(LinkList &L){LinkList p,q;L=L->next;p=L->next;while(p!=L){  q=p->next;  free(p);  p=q;}L->next=L;return 0;}//判空循环链表int Empty_List(LinkList L){if(L->next==L)return 1;elsereturn 0;}
//主函数int main(){LinkList L,k;int j;int e;Init_List(L);for(j=1;j<=5;j++){Insert_List(L,1,j);}for(k=L->next->next;k!=L->next;k=k->next){   printf("%d ",k->data);}printf("\n");Delete_List(L,5,e);printf("%d ",e);printf("\n");for(k=L->next->next;k!=L->next;k=k->next){   printf("%d ",k->data);}printf("\n");Get_List(L,3,e);printf("%d",e);printf("\n");Clear_List(L);printf("%d\n",Empty_List(L));  return 0;}

0 0
原创粉丝点击