您的位置:首页 > 其它

单向链表操作(产生,删除,插入以及查询)

2012-12-12 23:29 585 查看
//
//  main.m
//  leap
//
//  Created by Mamong on 12-11-26.
//  Copyright (c) 2012年 Mamong. All rights reserved.
//

#include<stdio.h>
//#include<malloc.h>
//#define NULL 0
#define LEN sizeof(DAT)
typedef struct student
{
long num;
char name[100];
int age;
char sex;
int score;
struct student *next;
}DAT;

int static n=0;

DAT* create()
{
DAT *head,*e;
DAT *u,*R;
head=(DAT *)malloc(LEN);
e=(DAT *)malloc(LEN);
R=head;
printf("type in the first student's info:");
scanf("%ld %s %d %c %d",&e->num,e->name,&e->age,&e->sex,&e->score);
if (0!=e->num) n=n+1;
while(0!=e->num)
{
u=(DAT *)malloc(LEN);
u->num=e->num;strcpy(u->name,e->name);u->age=e->age;u->sex=e->sex;u->score=e->score;
R->next=u;
R=u;
if(getchar()=='\n') printf("type in the %d(st/nd/rd/th) student's info:",++n);
scanf("%ld %s %d %c %d",&e->num,e->name,&e->age,&e->sex,&e->score);
}
R->next=NULL;
return head;
}

void print(DAT *head)
{
DAT * p;
if(NULL==head->next) printf("error:null\n");
else {p=head->next;
do
{
printf("%ld,%s,%d,%c,%d\n",p->num,p->name,p->age,p->sex,p->score);
p=p->next;
}while(NULL!=p);
}
}

DAT *del(DAT *head,long num)
{
DAT *p1,*p2;
if(NULL==head->next) {printf("table is NULL\n");goto end;}
p1=head;
while(num!=p1->num&&NULL!=p1->next)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(head==p1) head=p1->next;
else p2->next=p1->next;
printf("the number to delete is %ld\n",num);
n--;
}
else printf("error:no such student\n");
end:
return(head);
}

DAT *insert(DAT *head,DAT *stud)
{
DAT *p0,*p1,*p2;
p1=head->next;
p0=stud;
if(NULL==head->next) {head->next=p0;p0->next=NULL;}
else
{
while((p0->num>p1->num)&&(NULL!=p1->next))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(p1==head->next) head->next=p0;
else p2->next=p0;
p0->next=p1;
}
else {p1->next=p0;p0->next=NULL;}

}
n++;
return(head);
}
void search(DAT *head,long num)
{
DAT *p1;
long p0;
p1=head->next;
if(p1==NULL)
{
printf("error:table is NULL\n");
}
while(p1!=NULL)
{
p0=p1->num;
if(p0==num){
printf("number %ld student's info as follow:%ld,%s,%d,%c,%d",num,num,p1->name,p1->age,p1->sex,p1->score);break;
}
else
p1=p1->next;
}
if(p1==NULL) printf("reach the tail,no such student");

}

int main(int argc,char * argv[])
{
long search_num;
DAT *head,*stud;
head=(DAT *)malloc(sizeof(DAT));
long num;
head=create();
print(head);

printf("type in the number to delete:");
scanf("%ld",&num);
while(0!=num)
{
head=del(head,num);
print(head);
printf("type in the next number to delete:");
scanf("%ld",&num);

}
printf("new student info:");
stud=(DAT *)malloc(LEN);
scanf("%ld %s %d %c %d",&stud->num,stud->name,&stud->age,&stud->sex,&stud->score);
if(NULL!=stud)
while(0!=stud->num)
{
head=insert(head,stud);
print(head);
printf("next new student's info:");
stud=(DAT *)malloc(LEN);
scanf("%ld %s %d %c %d",&stud->num,stud->name,&stud->age,&stud->sex,&stud->score);
}
printf("type in the number you want to search:");
scanf("%ld",&search_num);
search(head,search_num);
return 1;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐