匿名用户
1级
2014-03-28 回答
1、查找成绩
#include
#include
#define MAX 101
struct aa/*定义结构体aa存储姓名和电话号码*/
{
char name[15];
char tel[15];
};
int readin(struct aa *a)/*自定义函数readin,用来存储姓名及电话号码*/
{
int i=0,n=0;
while(1)
{
scanf("%s",a[i].name);/*输入姓名*/
if(!strcmp(a[i].name,"#"))
break;
scanf("%s",a[i].tel);/*输入电话号码*/
i++;
n++;/*记录的条数*/
}
return n;/*返回条数*/
}
search(struct aa *b,char *x,int n)/*自定义函数search,查找姓名所对应的电话号码*/
{
int i;
i=0;
while(1)
{
if(!strcmp(b[i].name,x))/*查找与输入姓名相匹配的记录*/
{
printf("name:%s tel:%s\n",b[i].name,b[i].tel);/*输出查找到的姓名所对应的电话号码*/
break;
}
else
i++;
n--;
if(n==0)
{
printf("No found!");/*若没查找到记录输出提示信息*/
break;
}
}
}
main()
{
struct aa s[MAX];/*定义结构体数组s*/
int num;
char name[15];
num=readin(s);/*调用函数readin*/
printf("input the name:");
scanf("%s",name);/*输入要查找的姓名*/
search(s,name,num);/*调用函数search*/
}
2、删除成绩
#include "stdio.h"
struct student
{
int num;
struct student *next;
};
struct student *create(int n)
{
int i;
struct student *head, *p1, *p2;
int a;
head = NULL;
printf("The record:\n");
for (i = n; i > 0; --i)
{
p1 = (struct student*)malloc(sizeof(struct student)); /*分配空间*/
scanf("%d", &a);
p1->num = a; /*数据域赋值*/
if (head == NULL)
{
head = p1;
p2 = p1;
}
else
{
p2->next = p1; /*指定后继指针*/
p2 = p1;
}
}
p2->next = NULL;
return head;
}
struct student *delnode(struct student *head, int i)
{
int j = 0;
struct student *p, *r;
p = head;
while (p && j < i - 1)
{
p = p->next;
++j;
}
if (!p->next || j > i - 1)
exit(1);
r = p->next;
p->next = r->next;
free(r);
}
void main()
{
int n, i;
int x;
struct student *q;
printf("Input the count of the nodes you want to creat:");
scanf("%d", &n);
q = create(n);
i = 2;
delnode(q, i);
printf("The third record is deleted!\nThe result is:\n");
while (q)
{
printf("%d ", q->num);
q = q->next;
} getch();
}
3、插入成绩
#include "stdio.h"
struct student
{
int num;
struct student *next;
};
struct student *create(int n)
{
int i;
struct student *head, *p1, *p2;
int a;
char b;
head = NULL;
printf("The record:\n");
for (i = n; i > 0; --i)
{
p1 = (struct student*)malloc(sizeof(struct student)); /*分配空间*/
scanf("%d", &a);
p1->num = a; /*数据域赋值*/
if (head == NULL)
{
head = p1;
p2 = p1;
}
else
{
p2->next = p1; /*指定后继指针*/
p2 = p1;
}
}
p2->next = NULL;
return head;
}
struct student *insertnode(struct student *head, char x, int i)
{
int j = 0;
struct student *p, *s;
p = head;
while (p && j < i - 1)
{
p = p->next;
++j;
}
if (!p || j > i - 1)
exit(1);
s = (struct student*)malloc(sizeof(struct student));
s->num = x;
s->next = p->next;
p->next = s;
}
void main()
{
int n, i;
int x;
struct student *q;
printf("Input the count of the nodes you want to creat:");
scanf("%d", &n);
q = create(n);
i = 2;
x = 123;
insertnode(q, x, i);
printf("The result is:\n");
while (q)
{
printf("%d ", q->num);
q = q->next;
}
}