level 1
阿拉阿巴奇
楼主
#include <stdio.h>
#include <stdlib.h>
struct stu
{
int number;
char name[10];
char sex;
int age;
double score;
struct stu *next;
};
typedef struct stu NODE;
NODE *Create ( );
void Delete (NODE *head, int i);
void Print (NODE *head);
void Free(NODE *head);
int main ( )
{
int k;
NODE *head;
head=Create ( );
printf ("After create:\n ");
Print (head); //输出链表中的值
printf("Please input the age that you want to delete!");
scanf("%d",&k);
Delete (head, k); //删除链表
printf ("After delete: ");
Print (head); //输出链表中的值
Free (head); //销毁链表
return 0;
NODE *Create( ) //创建链表
{
NODE *head, *tail, *pnew;
int i;
head = (NODE *)malloc (sizeof(NODE)); //创建头节点
if (head == NULL) //创建失败,则返回
{
printf ("no enough memory!\n");
return (NULL);
}
head->next = NULL; //头节点的指针域置NULL
tail = head; //开始时尾指针指向头节点
printf ("input the score of students:\n");
for(i=0;i<5;i++) //创建学生成绩线性链表
{
pnew = (NODE *)malloc (sizeof(NODE));
if (pnew == NULL) //创建新节点失败,则返回
{
printf ("no enough memory!\n");
return (NULL);
}
pnew->next = NULL;
tail->next = pnew;
tail=pnew;
scanf("%d",pnew->number);
gets(pnew->name);
scanf("%c",pnew->sex);
scanf("%d",pnew->age);
scanf("%d",pnew->score); //输入成绩
}
return (head);
}
void Delete(NODE *head, int m)
{
NODE *p,*q;
int i;
for (i=0;i<5;i++)
{
if(p->next!=NULL&& p->age!=m)
p=p->next;
else if(p->age==m)
{
q=p->next;
p->next=q->next ;
}
else if (p->next == NULL)
{
printf ("the age of %d not found!\n",m);
return;
}
}
}
int Print (NODE *head)
{
NODE *p;
for (p = head->next; p != NULL; p = p->next)
printf ("%6d ",p->number);
puts(p->name);
printf ("%5c%3d%6.2lf\n",p->sex,p->age,p->score);
}
void Free(NODE *head)
{
NODE *p, *q;
for (p=head;p->next != NULL;)
{
q=p->next;
p->next=q->next;
free (q);
}
free (head);
}
2014年12月09日 10点12分
1
#include <stdlib.h>
struct stu
{
int number;
char name[10];
char sex;
int age;
double score;
struct stu *next;
};
typedef struct stu NODE;
NODE *Create ( );
void Delete (NODE *head, int i);
void Print (NODE *head);
void Free(NODE *head);
int main ( )
{
int k;
NODE *head;
head=Create ( );
printf ("After create:\n ");
Print (head); //输出链表中的值
printf("Please input the age that you want to delete!");
scanf("%d",&k);
Delete (head, k); //删除链表
printf ("After delete: ");
Print (head); //输出链表中的值
Free (head); //销毁链表
return 0;
NODE *Create( ) //创建链表
{
NODE *head, *tail, *pnew;
int i;
head = (NODE *)malloc (sizeof(NODE)); //创建头节点
if (head == NULL) //创建失败,则返回
{
printf ("no enough memory!\n");
return (NULL);
}
head->next = NULL; //头节点的指针域置NULL
tail = head; //开始时尾指针指向头节点
printf ("input the score of students:\n");
for(i=0;i<5;i++) //创建学生成绩线性链表
{
pnew = (NODE *)malloc (sizeof(NODE));
if (pnew == NULL) //创建新节点失败,则返回
{
printf ("no enough memory!\n");
return (NULL);
}
pnew->next = NULL;
tail->next = pnew;
tail=pnew;
scanf("%d",pnew->number);
gets(pnew->name);
scanf("%c",pnew->sex);
scanf("%d",pnew->age);
scanf("%d",pnew->score); //输入成绩
}
return (head);
}
void Delete(NODE *head, int m)
{
NODE *p,*q;
int i;
for (i=0;i<5;i++)
{
if(p->next!=NULL&& p->age!=m)
p=p->next;
else if(p->age==m)
{
q=p->next;
p->next=q->next ;
}
else if (p->next == NULL)
{
printf ("the age of %d not found!\n",m);
return;
}
}
}
int Print (NODE *head)
{
NODE *p;
for (p = head->next; p != NULL; p = p->next)
printf ("%6d ",p->number);
puts(p->name);
printf ("%5c%3d%6.2lf\n",p->sex,p->age,p->score);
}
void Free(NODE *head)
{
NODE *p, *q;
for (p=head;p->next != NULL;)
{
q=p->next;
p->next=q->next;
free (q);
}
free (head);
}