教程学院
图像设计 多媒体类 机械制图 办公软件 操作系统 系统编程 网站编程 网页制作 数据库类 网络路由 网络工程 网络安全 考试认证
酷网学院
CAD
AutoCad Cam350 ProEngineer GCcam MATLAB Unigraphics SolidWorks CAXA Solid3000 Cimatron EdgeCAM
系统
安全 防火墙 病毒 WinXP Win2003 Vista
数据库
编程
网络
  网站导航: 库库中文网 · 系统编程教程 · C语言 · C语言编程技巧  

c语言最经典地链表

教程推荐
『c语言最经典地链表』如果文章有大量图片,显示会较慢,请等待图片下载完成
 
点击数: 更新时间:2006-12-14 21:11:58 
在turbo c 2.0 下调试通过.(把汉语注释去掉)


#include"stdio.h"

#include"malloc.h"

#define NULL 0

#define L sizeof(struct integer)

struct integer                           /*定义结构体*/

{

int num;

int zhengshu;

    struct integer *next;

};

int n;   //纪录链表的长度

struct integer *creat(void)             /*创建链表*/

{

    struct integer *head;

    struct integer *p1,*p2;

    n=0;

    p1=p2=(struct integer *)malloc(L);

    scanf("%d,%d",&p1->num,&p1->zhengshu);

    head=NULL;

    while(p1->num!=0)

{

n=n+1;

        if(n==1) head=p1;

        else p2->next=p1;

        p2=p1;

        p1=(struct integer *)malloc(L);

        scanf("%d,%d",&p1->num,&p1->zhengshu);

}

    p2->next=NULL;

    return(head);

}



void print(struct integer *head)    /*打印链表中的数据*/

{

    struct integer *p;

    printf("Now %d biaohao and zhengshu are :n",n);

    p=head;

    if(head!=NULL)

    do

     {printf("%d,%5.1dn",p->num,p->zhengshu);

      p=p->next;

     }while(p!=NULL);

}



int count(struct integer *head)   /*返回链表的长度*/

{

int i=0;

    struct integer *p;

p=head;

while(p!=NULL)

{

p=p->next;i++;

}

return i;

}



void *findnode(struct integer *head,int num)  /*查找链表中的第num个数据*/

{

int j=1;

struct integer *p;

/*if(head==NULL)

{

printf("n空链表,请先创建!n");

return;

}*/

p=head;

if(num>count(head)||num<=0)

{

printf("Input error! Please input againn");

}

else

{

while(p!=NULL && jnext;

}

printf("%d bianhao reprensts %dn",p->num,p->zhengshu);

printf("n");

}

return(head);

}





struct integer *del(struct integer *head,long num)  /*删除链表中的第num个数据*/

{

struct integer *p1,*p2;

    if(head==NULL)

{

printf("nList Null!n");

        return;

}

    p1=head;

    while(num!=p1->num && p1->next!=NULL)

{

        p2=p1;

        p1=p1->next;

}

    if(num==p1->num)

    {

        if(p1==head) head=p1->next;

        else p2->next=p1->next;

        printf("Delete: %dn",num);

        n=n-1;

    }

    else printf("%d not been fonnd!n",num);

    return(head);

}



struct integer *insert(struct integer *head,struct integer *stud)  /*插入数据*/

{

    struct integer *p0,*p1,*p2;

    p1=head;

    p0=stud;

    if(head==NULL)

{

head=p0;

        p0->next=NULL;

}

    else

{

while((p0->num>p1->num)&&(p1->next!=NULL))

{

p2=p1;

            p1=p1->next;

}

if(p0->num<=p1->num)

{

            if(head==p1)head=p0;

            else p2->next=p0;

            p0->next=p1;

}

        else

{

            p1->next=p0;

            p0->next=NULL;

}

}

    n=n+1;

    return(head);

}



main()  /*主功能函数*/

{

int a,b;

    struct integer *head,stu;

    int del_num,fin_num;

printf("1 to go on / 0 to exit:n");

scanf("%d",&a);

while(a!=0)

{

/*struct integer *head,stu;

        int del_num;*/

printf("1 creat 2 print 3 delete 4 insert 5 findonde 0 exitn");

/*菜单的实现*/

scanf("%d",&b);

switch(b)

{

case 1:

{

/*clrscr();*/

printf("Please Input bianhao and data:n");

printf("for example 1,90  0,0 to exit:n");

head=creat();

}break;

case 2:

{

/*clrscr();*/

print(head);

}break;

case 3:

{

/*clrscr();*/

    printf("nInput the deleted biaohao:");

    scanf("%d",&del_num);

    head=del(head,del_num);

}break;

        case 4:

{

/*clrscr();*/

                printf("nInput the inserted biaohao and zhengshu:");

                scanf("%d,%d",&stu.num,&stu.zhengshu);

                head=insert(head,&stu);

}break;

        case 5:

{

/*clrscr();*/

printf("Please Input the bianhao you want to find:");

scanf("%d",&fin_num);

head=findnode(head,fin_num);

}break;

case 0:

{

     return;

}break;

default:

printf("Input error! Please input againn");

}

}

 

}



】【关闭窗口
  上一页:
  下一页:后面的文章暂时没有更新了,请多关注QQgb.com
C语言编程技巧:相关文章
C语言编程技巧点击榜
普通教程c语言最经典地链表
普通教程运用C语言小程序来处理大疑难
普通教程用C语言完成普遍地三种中文内码
推荐教程运用C语言小程序来处理大疑难
推荐教程用C语言完成普遍地三种中文内码
推荐教程怎么样在C语言里面灵活运用正则
普通教程几行代码,可画所有函数地图像
普通教程普遍错误
普通教程XP环境下地tc鼠标调用
普通教程文本方式下菜单
PHOTOSHOP - 基础教程 抠图专题 蒙版专题 3DsMax 基础 设计实例 Maya设计实例
3D设计教程
advertisement
关于站点 - 广告服务 - 联系我们 - 版权隐私 - 免责声明 - 合作伙伴 - 程序支持 - 网站地图 - 返回顶部
网站文本地图
版权所有:库库中文 2005-2007 欢迎各种媒体转载我们的原创作品[转载请注明出处]
copyright ? 2005-2008 www.QQGB.com online services. all rights reserved. 蜀ICP备05015578
Optimized for 1024x768 to Firefox,Opera and MS-IE6. Site powered by EQL. 电脑硬件 电脑知识 教程学习
红盾
热爱电脑,热爱生活
拥有电脑,拥有生命
让我们享受拥有电脑的时光