单循环链表和单链表的唯一区别在于单循环链表的最后一个节点的指针域指向第一个节点,
使得整个链表形成一个环.
C实现代码如下:
1 #include<stdio.h> 2 3 typedef struct node 4 { 5 int data; 6 struct node *next; 7 }Node; 8 9 //链表的初始化 10 Node* InitList(int number) 11 { 12 int i; 13 Node *pHead=(Node *)malloc(sizeof(Node)); 14 Node *TempHead=pHead; 15 Node *Head=pHead; 16 int data; 17 for(i=0;i<number;i++) 18 { 19 pHead=(Node *)malloc(sizeof(Node)); 20 printf("Please input the %dst node data: ",i+1); 21 scanf("%d",&data); 22 pHead->data=data; 23 pHead->next=Head->next; 24 TempHead->next=pHead; 25 TempHead=TempHead->next; 26 } 27 return Head; 28 } 29 30 //显示链表 31 void ShowList(Node *Head) 32 { 33 Node *TempNode=Head; 34 TempNode=TempNode->next; 35 printf("Show List: "); 36 while(TempNode->next!=Head->next) 37 { 38 printf("%d ",TempNode->data); 39 TempNode=TempNode->next; 40 } 41 printf("%d",TempNode->data); 42 printf(" "); 43 } 44 45 //输出链表某个值的位置 46 int ListLocation(Node *Head,int data,int number) 47 { 48 Node *TempNode=Head; 49 int location=1; 50 TempNode=TempNode->next; 51 while(TempNode->next!=Head->next) 52 { 53 if(TempNode->data==data) 54 { 55 return location; 56 } 57 location++; 58 TempNode=TempNode->next; 59 } 60 if(location>=number) 61 printf("Not found!"); 62 } 63 64 //输出链表某个位置的值 65 int ListData(Node *Head,int location,int number) 66 { 67 if(location>number) 68 printf("Not found!"); 69 70 Node *TempNode=Head; 71 TempNode=TempNode->next; 72 int i; 73 for(i=1;i<=number;i++) 74 { 75 if(location==i) 76 return TempNode->data; 77 TempNode=TempNode->next; 78 } 79 } 80 81 //头入法插入元素 82 void HeadInsertData(Node *Head,int data) 83 { 84 Node *TempNode=Head; 85 Node *InsertNode=(Node *)malloc(sizeof(Node)); 86 InsertNode->data=data; 87 88 89 while(TempNode->next->next!=Head->next) 90 TempNode=TempNode->next; 91 92 TempNode=TempNode->next; 93 94 InsertNode->next=Head->next; 95 TempNode->next=InsertNode; 96 Head->next=InsertNode; 97 } 98 99 //尾入插入除元素 100 void TailInsertData(Node *Head,int data) 101 { 102 Node *TempNode=Head; 103 Node *InsertNode=(Node *)malloc(sizeof(Node)); 104 InsertNode->data=data; 105 106 while(TempNode->next->next!=Head->next) 107 TempNode=TempNode->next; 108 109 TempNode=TempNode->next; 110 111 TempNode->next=InsertNode; 112 InsertNode->next=Head->next; 113 } 114 115 116 117 //删除头结点 118 void HeadDeleteData(Node *Head) 119 { 120 Node *TempNode=Head; 121 while(TempNode->next->next!=Head->next) 122 TempNode=TempNode->next; 123 TempNode->next->next=Head->next->next; 124 Head->next=Head->next->next; 125 } 126 127 128 //删除尾结点 129 void TailDeleteData(Node *Head) 130 { 131 Node *TempNode=Head->next; 132 while(TempNode->next!=Head->next) 133 { 134 TempNode=TempNode->next; 135 if(TempNode->next->next==Head->next) 136 break; 137 } 138 139 TempNode->next=Head->next; 140 } 141 142 int main() 143 { 144 Node *Head; 145 int number; 146 printf("Please input the node number: "); 147 scanf("%d",&number); 148 Head=InitList(number); 149 printf("The initital list is: "); 150 ShowList(Head); 151 152 int flag; 153 printf(" "); 154 printf("**********************Your Choice******************** "); 155 printf("****************1-输出链表某个值的位置*************** "); 156 printf("****************2-输出链表某个位置的值*************** "); 157 printf("****************3-头入法插入元素********************* "); 158 printf("****************4-尾入法插入元素********************* "); 159 printf("****************5-删除头结点************************* "); 160 printf("****************6-删除尾结点************************* "); 161 printf("****************0-退出******************************* "); 162 printf(" "); 163 printf("Please input flag: "); 164 scanf("%d",&flag); 165 166 switch(flag) 167 { 168 case 1: 169 { 170 int data; 171 printf("Please input the data you want locate: "); 172 scanf("%d",&data); 173 int location; 174 location=ListLocation(Head,data,number); 175 printf("The data's location is: %d",location); 176 break; 177 } 178 case 2: 179 { 180 int location; 181 printf("Please input the location you want data: "); 182 scanf("%d",&location); 183 int data; 184 data=ListData(Head,location,number); 185 printf("The location's data is: %d ",data); 186 break; 187 } 188 case 3: 189 { 190 int data; 191 printf("Please input the data you want insert in head: "); 192 scanf("%d",&data); 193 HeadInsertData(Head,data); 194 ShowList(Head); 195 break; 196 } 197 case 4: 198 { 199 int data; 200 printf("Please input the data you want insert in tail: "); 201 scanf("%d",&data); 202 TailInsertData(Head,data); 203 ShowList(Head); 204 break; 205 } 206 case 5: 207 { 208 HeadDeleteData(Head); 209 ShowList(Head); 210 break; 211 } 212 case 6: 213 { 214 TailDeleteData(Head); 215 ShowList(Head); 216 break; 217 } 218 case 7: 219 { 220 printf("You choose to exit. "); 221 break; 222 } 223 } 224 return 0; 225 }
结果图: