文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>对单链表进行操作【C/数据结构】

对单链表进行操作【C/数据结构】

时间:2010-05-31  来源:advancing

//《数据结构》课程实习(程序实现采用C语言) //编译运行通过 //题 目:编写建立一个由单链表组织存储的整数序列的程序,链表中每个结点存储一个整型数值,以此为基础完成将整数b插入到该链表中第一个数值为a的结点之前的程序。 // 代  码:  

#include<stdio.h>
#define LEN sizeof(struct jiedian)


struct jiedian
{
    int data;
    struct jiedian * next;
};

struct jiedian * home;

struct jiedian * creat()
{
    int n=0;
    struct jiedian * head,* p1,* p2;
    p1=p2=(struct jiedian*)malloc(LEN);
    printf("\nPlease enter the data: ");
    scanf("%d",&p1->data);
    head=NULL;
    while(p1->data!=0)
    {
        n=n+1;
        if(n==1)head=p1;
        else
            p2->next=p1;
            p2=p1;
        p1=(struct jiedian*)malloc(LEN);
        printf("\nPlease enter the data: ");
        scanf("%d",&p1->data);
        
    }
    p2->next=NULL;
    return(head);
}

void insert(struct jiedian * p,int m,int n)
{
    int i=0,j=0;
    struct jiedian *p1,*p2,*p3;
    p1=p;
    p2=p;
    if(p==NULL)
        {
            printf("The lianbiao is not exit!");
            exit(0);
        }
        else

          { while((p1!=NULL)&&(j!=1))
              {
                if(p1->data==n)
                {
                        if(i==0)
                        {
                                    p3=(struct jiedian *)malloc(LEN);
                                    p3->data=m;
                                    p3->next=p2;
                                    p=p3;
                                    j=1;

                        }
                        else
                        {
                            p3=(struct jiedian *)malloc(LEN);
                            p3->data=m;
                            p2->next=p3;
                            p3->next=p1;
                            j=1;

                        }

                 }
                else
                 {

                        p2=p1;
                        p1=p1->next;

                 }
               i++;
              }
   if(p1==NULL)
       printf("\nthe data %d is not exit!",n);
   }
   home=p;

}

void print(struct jiedian * m)
{
    struct jiedian * n;
    n=m;
    printf("\nThe current data is: ");
    while(n!=NULL)
    {
        printf("%4d ",n->data);
        n=n->next;
    }
}

void main()
{
    struct jiedian * head;
    int a,b;
    home=creat();
    print(home);
    printf("\nPlease enter the data you want to insert: ");
    scanf("%d",&a);
    printf("\nPlease enter the data you want to find: ");
    scanf("%d",&b);
    insert(home,a,b);
    print(home);
    printf("\nBYE!");
    getch();


}




//欢迎提出宝贵建议!谢谢!

 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载