vcf_DataStructure_v1.1

来源:互联网 发布:养老保险算法 编辑:程序博客网 时间:2024/06/16 22:29
#include<stdio.h>
#include<string.h>


typedef struct linknode
{
char name[20];
char tel[12];
struct linknode *next;
}node;


node *init()
{
node *head;
head = (node *)malloc(sizeof(node));
head->next = NULL;
return head;
}


node *find_last(node *head)
{
node *pre = init(&pre), *p = head;
while (p)
{
pre = p;
p = p->next;
}
return pre;
}
//importance
void build(node *head)
{
char a[20];
char b[12];
int i;
node *p,*last;
printf("name:");
scanf_s("%s", a, 20);
printf("tel:");
scanf_s("%s", b, 12);
p = (node *)malloc(sizeof(node));
for(i=0;a[i]!='\0';i++)
p->name[i] = a[i];
p->name[i] = '\0';
for (i = 0; b[i] != '\0'; i++)
p->tel[i] = b[i];
p->tel[i] = '\0';
p->next = NULL;
last = find_last(head);
last->next = p;
}
node *find_num(node *head, int i)
{
node *p = head->next;
int j = 2;;
if (i == 0)
return head;
if (i == 1)
return head->next;
else
{
p = p->next;
while (p&&i != j)
{
p = p->next;
++j;
}
if (!p&&i != j)
{
printf("没有找到第%d个结点\n", i);
return NULL;
}
else
if (i == j)
return p;
}
return NULL;
}
node *find_key(node *head, char x[])
{
node *p = head;
p = p->next;
while (p->next != NULL && !(strcmp(p->name, x) == 0))
p = p->next;
if (p->next == NULL && !(strcmp(p->name, x) == 0))
printf("没有找到所输入的项!\n");
else
{
printf("按“name”关键字找到的信息:name:%20s:", p->name);
printf("tel:%12s\n", p->tel);
return p;
}
return NULL;
}


node *insert(node *head, char x[], char t[], int j)
{
node *p;
int i;
node *q = (node *)malloc(sizeof(node));
for (i = 0; x[i] != '\0'; i++)
q->name[i] = x[i];
q->name[i] = '\0';
for (i = 0; t[i] != '\0'; i++)
q->tel[i] = t[i];
q->tel[i] = '\0';
q->next = NULL;
printf("所插入的内容:%s,%s\n", q->name, q->tel);
q->next = NULL;
p = find_num(head, j-1);
if (p)
{
q->next = p->next;
p->next = q;
}
return head;
}
node *dele(node *head, char x[])
{
node *p = head, *pre = init(&pre);
pre = head;
if (!p->next)
{
printf("链表为空,无法进行删除!\n");
return NULL;
}
p = p->next;
//printf("test:%s,%s\n", p->name, x);
//printf("test_0:%d,%d\n", p != NULL, !(strcmp(p->name, x)==0));
while (p->next!= NULL&& !(strcmp(p->name, x) == 0))
{
//printf("test:%s,%s\n", p->name, x);
pre = p;
p = p->next;
//printf("test_0:%d,%d\n", p != NULL, !(strcmp(p->name, x) == 0));
}
//printf("test_1:%d,%d\n", p->next == NULL, (strcmp(p->name, x) == 0));
if (p->next == NULL&& !(strcmp(p->name, x) == 0))
printf("没有找到要删除的值!\n");
else
pre->next = p->next;
return head;
}
void display(node *head)
{
node *p = head;
p = p->next;
while (p)
{
printf("name:%20s:", p->name);
printf("tel:%12s\n", p->tel);
p = p->next;
}
printf("-----------------------------------------\n");
}
void main()
{
node *head, *p;
head = init();
int i, t;
char temp1[20],temp2[12];
//通讯录的初始化
printf("请输入有几条数据:");
scanf_s("%d", &t);
for (i = 0; i < t; i++)
build(head);
display(head);
//查找第?条数据
printf("请输入要查找第几条数据:");
scanf_s("%d", &t, 1);
p = find_num(head, t);
printf("find_num:%d\n", t);
printf("name:%20s:", p->name);
printf("tel:%12s\n", p->tel);
printf("***********************************************\n");
//插入
printf("插入-请输入姓名:");
scanf_s("%s", temp1, 20);
printf("插入-请输入%s的电话:",temp1);
scanf_s("%s", temp2, 12);
printf("插入-请输入要插入的位置:");
scanf_s("%d", &t, 1);
p = insert(head, temp1, temp2, t);
display(head);
//删除
printf("删除-请输入姓名:");
scanf_s("%s", temp1, 20);
head = dele(head, temp1);
display(head);
//按姓名查找
printf("find_key:\n");
printf("查找-请输入姓名:");
scanf_s("%s", temp1, 20);
p = find_key(head, temp1);
}
原创粉丝点击