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




//
//  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;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值