单向循环链表【数据结构-朱战立】

  1 /*这个程序是循环链表*/
  2 #include <stdio.h>
  3 #include <stdlib.h>
  4 #include <malloc.h>
  5 typedef int bool;
  6 #define true 1
  7 #define false 0
  8 
  9 typedef int DataType;
 10 typedef struct node{
 11     DataType data;
 12     struct node *next;
 13 }CurrNode;
 14 
 15 //链表初始化
 16 void CurrInit(CurrNode **head)
 17 {
 18     if ((*head = (CurrNode*)malloc(sizeof(CurrNode))) == NULL)
 19     {
 20         exit(1);
 21     }
 22     (*head)->next = *head;//构造循环链表
 23 }
 24 
 25 //链表长度返回
 26 int Currlength(CurrNode *head)
 27 {
 28     CurrNode *per = head->next;
 29     int num = 0;
 30 
 31     while (per!= head)//出现中断错误?    //2018年8月15日 18:40:31 返回长度per!=head
 32     {
 33         per = per->next;
 34         num++;
 35     }
 36     return num;
 37 }
 38 
 39 //链表插入
 40 int CurrInsert(CurrNode *head, int i, DataType x)
 41 {
 42     CurrNode *per, *fin;
 43     int j = 0;
 44 
 45     if ((fin=(CurrNode*)malloc(sizeof(CurrNode)))==NULL)
 46     {
 47         exit(1);
 48     }
 49     per = head;                                       //2018年8月15日 18:48:36  如果per=head->next 那么链表将倒序
 50     while(per->next!=head&&/*per->next!=head&&*/j<i)  //2018年8月15日 18:41:38  判断条件不需要那么多
 51 
 52     {
 53         per = per->next;
 54         j++;
 55     }
 56     if (j!=i)
 57     {
 58         printf("插入位置错误!\n");
 59         return 0;
 60     }
 61     fin->data = x;
 62 
 63     fin->next = per->next;
 64     per->next = fin;
 65     return 1;
 66 }
 67 
 68 //链表删除
 69 int CurrDelete(CurrNode *head, int i, DataType *x)
 70 {
 71     CurrNode *per, *tmp;
 72     int j = 0;
 73 
 74     if ((tmp = (CurrNode*)malloc(sizeof(CurrNode))) == NULL)
 75     {
 76         exit(1);
 77     }
 78     per = head;
 79     while (per->next != head&&j<i)//删除不需要判断下一个节点是否存在
 80     {
 81         per = per->next;
 82         j++;
 83     }
 84     if (j != i)
 85     {
 86         printf("删除位置错误!\n");
 87         return 0;
 88     }
 89     *x = per->data;
 90 
 91     tmp = per->next;
 92     per->next = tmp->next;
 93     free(tmp);
 94     return 1;
 95 }
 96 
 97 //链表取出
 98 int CurrGet(CurrNode *head, int i, DataType *x)
 99 {
100     CurrNode *per=head->next;
101     int j = 0;
102 
103     while (per!=head&&j<i)
104     {
105         per = per->next;
106         j++;//失误
107     }
108     if (j!=i)
109     {
110         printf("取出位置错误!\n");
111         return 0;
112     }
113     *x = per->data;
114     return 1;
115 }
116 //链表销毁
117 void CurrDestroy(CurrNode **head)
118 {
119     CurrNode *per = *head, *tmp;
120     if ((tmp = (CurrNode*)malloc(sizeof(CurrNode))) == NULL)
121     {
122         exit(1);
123     }
124     while (per->next!= *head)
125     {
126         tmp = per;
127         per = per->next;
128         free(tmp);
129     }
130     *head = NULL;
131 }
132 
133 //判断是否构成循环链表
134 bool Currcheck(CurrNode* head)
135 {
136     int nNumber = Currlength(head);
137 
138     CurrNode* pTemp = head;
139     int i = 0;
140     while (i < nNumber)
141     {
142         pTemp = pTemp->next;
143         i++;
144     }
145 
146     return (pTemp->next == head) ? true : false;
147 }
148 
149 //主函数
150 int main()
151 {
152     CurrNode *mylist;
153     int i, x;
154 
155     CurrInit(&mylist);
156     for ( i = 0; i < 10; i++)
157     {
158         if ((CurrInsert(mylist, i, i + 1)) == 0)
159         {
160             printf("插入位置错误!\n");
161             return ;
162         }
163     }
164     if ((CurrDelete(mylist, 5, &x)) == 0)
165     {
166         printf("删除位置错误!\n");
167         return;
168     }
169     else
170     {
171         printf("删除数字:%d\n", x);
172     }
173     for ( i = 0; i < Currlength(mylist); i++)
174     {
175         if ((CurrGet(mylist, i, &x)) == 0)
176         {
177             printf("取出位置错误!\n");
178             return;
179         }
180         else
181         {
182             printf("%d    ",x);
183         }
184     }
185     printf("\n%s", (Currcheck(mylist) == 1 )? "是循环链表": "不是循环链表");
186     CurrDestroy(&mylist);
187     getchar();
188     return 0;
189 }

 

posted @ 2018-08-15 18:50  蓝莓派Alex  阅读(551)  评论(0编辑  收藏  举报