#include
#include
#include
typedef struct node
{
char name[20];
struct node *next, *prior;
} NODE;
// creat...
NODE *creat(int n)
{
NODE *p, *q, *head;
int i;
if ((head = (NODE *)malloc(sizeof(NODE))) == NULL)
return NULL ;
head ->name[0] = '\0';
head ->next = NULL;
head ->prior = NULL;
q = head;
for (i=0; i
{
if ((p = (NODE *)malloc(sizeof(NODE))) == NULL)
return NULL ;
q->next = p;
scanf("%s", &p->name);
p->next = NULL;
p->prior = q;
q = p;
}
q->next = head;
head->prior = q;
return head;
}
// -- print
int print_chain(NODE *head)
{
NODE *p;
if (head == NULL)
return -1;
p = head->next;
while (p != head)
{ printf("Name is %s\n", p->name);
p=p->next;
}
return 0;
}
//--
NODE *search(NODE *head, char *name)
{
NODE *p;
p = head->next;
while(p != head)
{
if (strcmp(p->name, name) == 0)
return p;
else p = p->next;
}
printf("No name found \n");
return NULL;
}
// ---insert before p_name
int insert(NODE *head, char *p_name, char *in_name)
{
NODE *p, *newnode;
if(head == NULL)
return -1;
if ((p = search(head, p_name)) == NULL)
{
printf("No place found !\n");
return -1;
}
if ((newnode = (NODE *)malloc(sizeof(NODE))) == NULL)
{ printf("No mem place left!\n");
return -1;
}
strcpy(newnode->name, in_name);
newnode->next = p;
newnode->prior = p->prior;
(p->prior)->next = newnode;
p->prior = newnode;
return 0;
}
//--
int del(NODE *head, char *d_name)
{
NODE *p;
if (head == NULL)
return -1;
if ((p = search(head, d_name)) == NULL)
{ printf("Node was not found !\n");
return -1;
}
p->next->prior = p->prior;
p->prior->next = p->next;
return 0;
}
//--
int main()
{
NODE *head ;
char s_name[20];
char in_name[20];
char d_name[20];
int num;
printf("Please input the node No you want to create\n");
scanf("%d", &num);
head = creat(num);
print_chain(head);
printf("Please enter the name you want to search:\n");
scanf("%s", s_name);
printf("Please enter the name you want to insert:\n");
scanf("%s", in_name);
insert(head, s_name,in_name);
print_chain(head);
printf("Please enter the name you want to delete:\n");
scanf("%s", d_name);
del(head, d_name);
print_chain(head);
return 0;
}
/*-- E --*/
谢谢欣赏,欢迎指正!
阅读(768) | 评论(0) | 转发(0) |