#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Name_Max 20
#define Tele_Max 100
#define Company_Max 100
typedef struct node {
char Name[Name_Max];
char Tele[Tele_Max];
char Company[Company_Max];
struct node* next;
}Linked_list_Typedef;
void Linked_list_Insert(Linked_list_Typedef* head);
void Linked_list_Read(Linked_list_Typedef* head);
Linked_list_Typedef* Linked_list_Creat(int n);
//void Linked_list_Change(Linked_list_Typedef* head, int x);
void Linked_list_Cut(Linked_list_Typedef* head);
//Linked_list_Typedef* Linked_list_Invert(Linked_list_Typedef* head);
void menu();
void Linked_list_Find(Linked_list_Typedef* head);
int main()
{
int N;
int KeyNum;
Linked_list_Typedef* HEAD;
printf("创造链表,请输入链表长度:");
scanf_s("%d", &N);
getchar();
HEAD = Linked_list_Creat(N);
while (1)
{
menu();
printf("选择:");
scanf_s("%d", &KeyNum);
getchar();
switch (KeyNum)
{
case 1:
Linked_list_Insert(HEAD);
break;
case 2:
Linked_list_Find(HEAD);
break;
case 3:
Linked_list_Cut(HEAD);
break;
case 4:
Linked_list_Read(HEAD);
break;
case 5:
exit(0);
break;
}
}
return 0;
}
Linked_list_Typedef* Linked_list_Creat(int n)//创建一个链表
{
Linked_list_Typedef* head = NULL, * node, * end;
head = (Linked_list_Typedef*)malloc(sizeof(Linked_list_Typedef));
end = head;
for (int i = 0; i < n; i++)
{
node = (Linked_list_Typedef*)malloc(sizeof(Linked_list_Typedef));
printf("输入姓名:");
gets(node->Name);
printf("输入电话号码:");
gets(node->Tele);
printf("输入单位名称:");
gets(node->Company);
printf("-------------\n");
end->next = node;
end = node;
}
end->next = head;
return head;
}
void Linked_list_Read(Linked_list_Typedef* head)//遍历
{
Linked_list_Typedef* q;
q = head->next;
if (q == head)
{
printf("通讯录为空!");
return;
}
else
{
printf("通讯录成员:\n");
while (q != head)
{
printf("-------------\n");
puts(q->Name);
puts(q->Tele);
puts(q->Company);
printf("-------------\n");
q = q->next;
}
printf("\n");
}
}
void Linked_list_Insert(Linked_list_Typedef* head)//末尾插入
{
Linked_list_Typedef* q, * p;
p = (Linked_list_Typedef*)malloc(sizeof(Linked_list_Typedef));
printf("输入姓名:");
gets(p->Name);
printf("输入电话号码:");
gets(p->Tele);
printf("输入单位名称:");
gets(p->Company);
printf("-------------\n");
q = head;
while (q->next != head)
q = q->next;
p->next = head;
q->next = p;
}
void Linked_list_Cut(Linked_list_Typedef* head)
{
Linked_list_Typedef* q, * p = NULL, * temp;
if (head->next == head)
{
printf("通讯录为空!");
return;
}
temp = (Linked_list_Typedef*)malloc(sizeof(Linked_list_Typedef));
printf("-------------\n");
printf("输入想删除的姓名:");
gets(temp->Name);
printf("输入想删除的电话号码:");
gets(temp->Tele);
printf("输入想删除的单位名称:");
gets(temp->Company);
printf("-------------\n");
q = head;
while (strcmp((q->next)->Name, temp->Name) != 0 && strcmp((q->next)->Tele, temp->Tele) != 0 && strcmp((q->next)->Company, temp->Company) != 0 && q->next != head)
{
q = q->next;
}
if (q->next == head)
{
printf("查无此人\n");
return;
}
else
{
p = q->next;
q->next = p->next;
free(p);
}
}
void menu()
{
printf("___________________\n");
printf("_____按 1 插入_____\n");
printf("_____按 2 查找_____\n");
printf("_____按 3 删除_____\n");
printf("_____按 4 遍历_____\n");
printf("_____按 5 退出_____\n");
printf("___________________\n");
}
void Linked_list_Find(Linked_list_Typedef* head)
{
Linked_list_Typedef* point, * temp;
temp = (Linked_list_Typedef*)malloc(sizeof(Linked_list_Typedef));
printf("输入想寻找的姓名:");
gets(temp->Name);
printf("-------------\n");
point = head->next;
while (strcmp((point->Name), temp->Name) != 0 && point!= head)
{
point = point->next;
}
if (point== head)
{
printf("查无此人");
}
else
{
puts(point->Name);
puts(point->Tele);
puts(point->Company);
}
}
作业分享!!!!!!!!(造福后人,啊哈哈哈哈哈哈哈哈哈哈)