typedef struct Node ListNode; ListNode *CreateList(int n);

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct Node
{
char name[10];
int score;
struct Node *next;
};
typedef struct Node ListNode;
ListNode *CreateList(int n);
void InsertList(ListNode*h,int i,char name[],int score,int n);
void DeleteList(ListNode*h, int i,int n);
void PrintList(ListNode*h);
ListNode*CreateList(int n)
{
ListNode*head;
ListNode*p,*pre;
int i;
head=(ListNode*)malloc(sizeof(ListNode));
head->next=NULL;
pre=head;
for(i=0;i<=n;i++)
{
printf("input name of the %d student:",i);
p=(ListNode*)malloc(sizeof(ListNode));
scanf("%s",&p->name);
printf("intput score of the %d student:",i);
scanf("%d",&p->score);
pre->next=p;
pre=p;
}
pre->next=NULL;
return head;
}
void PrintList(ListNode*h)
{
ListNode *p;
p=h->next;
while(p)
{
printf("%s,%d",p->name,p->score);
p=p->next;
printf("\n");
}
}
int main()
{
ListNode*h;
int i=1,n,score;
char name[10];
while(i)
{
printf("1---------建立新的链表\n");
printf("2---------添加元素\n");
printf("3--------删掉元素\n");
printf("4---------输出当前中的元素\n");
printf("0--------退出\n");
scanf("%d",&i);
switch(i)
{
case 1:
printf("n=");
scanf(" %d",&n);
h=CreateList(n);
printf("list element s is :\n");
PrintList(h);
break;
case 2:
printf("input the postion of insert element:");
scanf("%d",&i);
printf("intput name of the student:");
scanf("%s",name);
printf("input score of the student:");
scanf("%d",&score);
InsertList(h,i,name,score,n);
printf("list element is :");
PrintList(h);
break;
case 3:
printf("input the postion of insert element:");
scanf("%d",&i);
DeleteList(h,i,n);
printf("list element s is :\n");
PrintList(h);
break;
case 4:
printf("list element s is :\n");
PrintList(h);
break;
case 0:
return ;
break;
default:
printf("ERROR!TRY again!\n");
}
}
}
void InsertList(ListNode*h,int i,char name[],int e,int n)
{
ListNode*q,*p;
int j;
if(i<1||i>n+1)
printf("ERROR!TRY again!\n");
else
{
j=0;p=h;
while(j<i-1)
{
p=p->next;
j++;
}
q=(ListNode*)malloc(sizeof(ListNode));
strcpy(q->name,name);
q->score=e,
q->next=p->next;
p->next=q;
}
}
void DeleteList(ListNode*h,int i,int n)
{
ListNode*q,*p;
int j;
char name[10];
int score;
if(i<1||i>n)
printf("ERROR!TRY again!\n");
else
{
j=0;p=h;
while(j<i-1)
{
p=p->next;
j++;
}
q=p->next;
p->next=q->next;
strcpy(name,q->name);
score=q->score;
free(q);
printf("name=%s,score=&d\n",name,score);
}
求修

第1个回答  2013-01-02
全队了,加油吧!!!

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网