#include
#include
typedef struct list
{
int data;
struct list *prior, *next;
} List;
List *creat_list(void)
{
List *head = (List *)malloc(sizeof(List));
if (head == NULL)
{
printf("Create fail \n");
return NULL;
}
head->prior = head->next = head;
return head;
}
//-----
int InsertList(List *head, int value)
{
List *newnode, *p;
if (head == NULL)
{
printf("No head Node !\n");
return -1;
}
newnode = (List *)malloc(sizeof(List));
if (newnode == NULL)
{
printf("Create fail \n");
return -1;
}
newnode->data = value;
p = head;
while (p->next!=head)
{
p = p->next;
}
// insert node
newnode->next = p->next;
p->next = newnode;
newnode->prior = p;
head->next = newnode; // 自己加的
return 0;
}
void Find(List *head, int find)
{
List *p;
int i = 0;
int flag = 0;
p = head;
//while(p!=head)
while (p->next != head)
{
p=p->next;
i++;
if(p->data == find)
{
printf("Data: %d is No.%d\n", find, i);
flag = 1;
}
}
// if(p->data!=find && p->next==head)
if (flag == 0)
printf("No data found\n");
}
//---
void print(List *head)
{
int i = 0;
List *p = head->next;
while(p!=head)
{
printf("%d \n", p->data);
p=p->next;
i++;
}
printf("Length is %d \n", i);
}
List *Delete(List *head, int x)
{
List *p;
int i=0;
p=head->next;
p=p->next;
while(p->next!=head)
{
p= p->next;
i++;
if(p->data==x)
{
p->prior->next = p->next;
p->next->prior = p->prior;
p=p->next;
// free(p);
printf("Data %d is No.%d been delete! \n",x,i);
}
}
return head;
}
int main()
{
List *head = NULL;
head = creat_list();
InsertList(head,10);
print(head);
Delete(head,10);
print(head);
Find(head,10);
return 0;
}
/*-- E --*/
谢谢欣赏!
阅读(949) | 评论(0) | 转发(0) |