#include <stdio.h>
#include <stdlib.h>
typedef struct CLinkList{
int data;
struct CLinkList *next;
}node;
void InitLinkList(node **pNode); //初始化链表
void Insert(node **pNode, int i); //插入结点
void Delete(node **pNode, int i); //删除结点
int Search(node *pNode, int elem); //搜索结点
void Traverse(node *pNode); //遍历链表
int main(){
node *pHead = NULL;
char opp;
int find;
printf("1.LinkList Initalized\n2.Node insert\n3.Node delete\n4.Return node position\n5.Traverse linklist\n0.EXIT\n\nPlease start your operation!\n");
while(opp != '0'){
scanf("%c", &opp);
switch(opp){
case '1':
InitLinkList(&pHead);
printf("\n");
Traverse(pHead);
break;
case '2':
printf("Input the place to insert node?");
scanf("%d", &find);
Insert(&pHead, find);
printf("After insert the %d value: \n", find);
Traverse(pHead);
printf("\n");
break;
case '3':
printf("Input which node to delete?");
scanf("%d", &find);
Delete(&pHead, find);
printf("After delete the No.%d node:\n", find);
Traverse(pHead);
printf("\n");
break;
case '4':
printf("The node is where you want to search?\n");
scanf("%d", &find);
printf("The position of elem %d: No.%d\n", find, Search(pHead, find));
printf("\n");
break;
case '5':
Traverse(pHead);
printf("\n");
break;
case '0':
exit(0);
}
}
return 0;
}
void InitLinkList(node **pNode){
int item;
node *temp;
node *target;
printf("Please input the number of node then input 0 can complete initalize\n");
while(1){
scanf("%d", &item);
fflush(stdin);
if(item == 0)
return;
if((*pNode) == NULL){ //循环链表有且只有一个结点
*pNode = (node*)malloc(sizeof(struct CLinkList));
if(!(*pNode))
exit(0);
(*pNode)->data = item;
(*pNode)->next = *pNode;
}
else{ //找next指向第一个结点的结点
for(target = (*pNode); target->next != (*pNode); target = target->next)
;
//做新的结点
temp = (node *)malloc(sizeof(struct CLinkList));
if(!temp)
exit(0);
temp->data = item;
temp->next = *pNode;
target->next = temp;
}
}
}
void Insert(node **pNode, int i){
node *temp;
node *target;
node *p;
int item;
int j = 1;
printf("Input the node value\n");
scanf("%d", &item);
if(i == 1){ //判断新插入的结点是否为第一个结点
temp = (node *)malloc(sizeof(struct CLinkList));
if(!temp)
exit(0);
//寻找最后一个结点
temp->data = item;
for(target = (*pNode); target->next != (*pNode); target = target->next)
;
//target做前一个元素,需要next指向下一个元素
temp->next = (*pNode);
target->next = (*pNode);
target->next = temp;
*pNode = temp;
}
else{
target = *pNode;
for(; j <(i - 1); ++j)
target = target->next;
temp = (node *)malloc(sizeof(struct CLinkList));
if(!temp)
exit(0);
temp->data = item;
p = target->next;
target->next = temp;
temp->next = p;
}
}
void Delete(node **pNode, int i){
node *target;
node *temp;
int j = 1;
if(i == 1){ //如果删除的第一个结点寻找最后一个结点
for(target = *pNode; target->next != *pNode; target = target->next)
;
temp = *pNode;
*pNode = (*pNode)->next;
target->next = *pNode;
free(temp);
}
else{
target = *pNode;
for(; j < i - 1; ++j)
target = target->next;
temp = target->next;
target->next = temp->next;
free(temp);
}
}
int Search(node *pNode, int elem){
node *target;
int i = 1;
for(target = pNode; target->data != elem && target->next != pNode; ++i)
target = target->next;
if(target->next == pNode) //判断该元素是否存在
return 0;
else
return i;
}
void Traverse(node *pNode){
node *temp;
temp = pNode;
printf("**********Elem in linklist**********\n");
do{
printf("%d ", temp->data);
}while((temp = temp->next) != pNode); //遍历链表输出
printf("\n");
}