关于链表的基本操作





/**
*  编写:中医熊猫
*  时间:20150801
*  初次编写,还望指教
*/


#include <malloc.h>
#include <stdio.h>
#define LEN sizeof(struct STU)
int n = 0;
struct STU{
    int num;
    struct STU *next;
};


/**************创建链表********************/
struct STU *creatList()
{
    n=0;
    struct STU *head;
    struct STU *p1,*p2;
    head = p1 = p2 = (struct STU *)malloc(LEN);
    printf("Please input %d number:",++n);
    scanf("%d",&p1->num);
    while(p1->num)
    {
        p1 = (struct STU *)malloc(LEN);
        printf("Please input %d number:",++n);
        scanf("%d",&p1->num);
        p2->next = p1;
        p2 = p1;
    }
    p1->next = NULL;
    return head;
}


/********遍历链表**************/
void printList(struct STU *head)
{
    printf("\n ergodic chained list:\n");
    while(head->num)
    {
        printf("  %d",head->num);
        head = head->next;
    }
    printf("\n");
}


//插入元素
//在第几个位置上插入一个数。
/*
    1.第一个,最后一个。
    2.保留前驱。
    3.确保后驱。
*/
/***************插入元素***************/
void insertList(struct STU *head)
{
    struct STU *p1,*p2;
    int i=0;
    int number,place;
    printf("Please input the number you want to insert:");
    scanf("%d",&number);
    p1 = (struct STU *)malloc(LEN);
    p1->num = number;
    printf("Please input the number's lace(0-%d):",n-1);
    scanf("%d",&place);


    while(head)
    {
        if(i==place-1)
        {
            p2->next = p1;
            p1->next = head;
            printf("insert successfully!");
            break;
        }
        p2 = head;
        head = head->next;
        i++;
    }
}


//删除元素
   /********1.删除已知数**************/
void deleteList1(struct STU *head)
{
    if(head==NULL)
    {
        printf("\n it's NULL!!\n");
        exit(0);
    }
    int number;
    struct STU *p1,*p2;
    printf("\n Please input the number you want to delete:");
    scanf("%d",&number);
    while(head->num)
    {
        if(head->num == number)
        {
            p1->next = head->next;
        }
        p1 = head;
        head = head->next;
    }
}
/****** 2.删除指定位置上的数****/
void deleteList2(struct STU *head)
{
    if(head==NULL)
    {
        printf("\n it's NULL!!\n");
        exit(0);
    }
    int i=0,place;
    struct STU *p1;
    printf("\n Please input the number's place that you want to delete:");
    scanf("%d",&place);
    while(head->num)
    {
        if(i++ == place)
        {
            p1->next = head->next;
            printf("\n Luickly...Have delelted\n");
        }
        p1 = head;
        head = head->next;
    }
}


/****************改变元素*************************/
        /**1.根据值,改变数值*****/
void changeList1(struct STU *head)
{
    int number,afternumber;
    if(head==NULL)
    {
        printf("\n it's NULL!!\n");
        exit(0);
    }
    printf("Please input the number you want to change:");
    scanf("%d",&number);
    printf("After changing ,the number is changed for:");
    scanf("%d",&afternumber);
    while(head->num)
    {
        if(head->num == number)
        {
            printf("after seek ..");
            head->num = afternumber;
            printf("\nHave changed!!\n");
        }
        head = head->next;
    }
}
    /*****2.改变指定位置上的数*****/
void changeList2(struct STU *head)
{
    int number,i,j;
    if(head==NULL)
    {
        printf("\n it's NULL!!\n");
        exit(0);
    }
    printf("\n Plaes input the place that number you want to change:");
    scanf("%d",&i);
    printf("\n After changed,the number is changed for:");
    scanf("%d",&number);
    j=0;
    while(head->num)
    {
        j++;
        if(j==i)
        {
            head->num = number;
        }
        head = head->next;
    }
}




/*********************查找元素************************/
int seekList(struct STU *head)
{
    int number,i=1;
    if(head==NULL)
    {
        printf("\n it's NULL!!\n");
        exit(0);
    }
    printf("\n Please input the number you want to seek:");
    scanf("%d",&number);
    while(head->num)
    {
        if(head->num == number)
        {
            printf("\n seek successfully! the place is %d\n",i);
        }
        head = head->next;
        i++;
    }
}


/*****************************Menu***************************/
void printMenu()
{
    printf("\n\t***********************************************************\n");
    printf("\t*                     THE MENU FOLLOW                      *\n");
    printf("\t*  0.print the Menu                                        *\n");
    printf("\t*  1.creat Linked List                                     *\n");
    printf("\t*  2.insert the Element to List                            *\n");
    printf("\t*  3.delete the Element to List according to number        *\n");
    printf("\t*  4.delete the Element to List according to place         *\n");
    printf("\t*  5.change the Elemnet to List according to number        *\n");
    printf("\t*  6.change the Element to List according to place         *\n");
    printf("\t*  7.seek the list                                         *\n");
    printf("\t*  8.ergodic chained list                                  *\n");
    printf("\t*  9.exit                                                  *\n");
    printf("\t************************************************************\n");
}


/****************主函数******************/
int main(void)
{
    int num;
    struct STU *head;
    printMenu();
    printf("\norder:");
    while(scanf("%d",&num)!=EOF)
    {
        switch(num)
        {
            case 0:printMenu();break;
            case 1:head=creatList();break;
            case 2:insertList(head);break;
            case 3:deleteList1(head);break;
            case 4:deleteList2(head);break;
            case 5:changeList1(head);break;
            case 6:changeList1(head);break;
            case 7:seekList(head);break;
            case 8:printList(head);break;
            case 9:exit(0);
        }
        printf("order:");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

方_小_白

谢谢金主子,记得关注方家小白哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值