单向链表的建立,添加与删除



/*-------------------------包含头文件------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
int count=0;
/*-------------------------结构体定义部分------------------------------*/
typedef struct Node
{
    char name[10];
    int score;
    struct Node *next;
} ListNode;


/*----------------------------函数声明部分------------------------------*/




/*---------------------------函数实现部分-------------------------------*/
/*-----------------------------创建链表---------------------------------*/
/*在链表的末端插入新的节点,建立链表*/
ListNode *CreateList()
{
    ListNode *head;//指向头结点指针
    ListNode *p,*pre;
    head=(ListNode *)malloc(sizeof(ListNode));//为头节点分配内存空间
    head->next=NULL;//将头结点的指针域清空
    pre=head;//先将头结点首地址赋给中间变量pre
    while(1)
    {
        if((p=(ListNode *)malloc(sizeof(ListNode)))==NULL)
        {
            printf("内存空间不足!!!\n");
            break;
        }
        count++;
        printf("input name of the %d student(input \"q\" to quit):",count);//打印出第几个人的名字
        //内存空间p指向新插入结点的首地址
        scanf("%s",&p->name);//输入姓名
        if(strcmp(p->name,"q")==0)
            break;
        printf("input score of the %d student:",count);
        scanf("%d",&p->score);//输入分数
        pre->next=p;//将p指向新结点插入链表也就是头结点指针域指向
        //下个结点
        //第一个结点就是p指向的,因为头结点内容为空
        pre=p;//这个起着指向下一个结点的作用
        pre->next=NULL;
    }
    return head;//返回这个链表的首地址
}
/*-------------------------输出链表-----------------------------------*/
void PrintList(ListNode *h)
{
    ListNode *p;
    p=h->next;
    while(p)
    {
        printf("%s,%d",p->name,p->score);
        p=p->next;
        printf("\n");
    }
}
/*----------------------插入链表结点--------------------------*/
/*--------------------------------------------------------------------
函数名称:InsertList(ListNode *h,int i,char name[],int e,int n)
函数功能:插入链表结点
入口参数: h: 头结点地址 i:插入到第几个结点 name:插入结点的姓名 e:插入结点的分数 n:链表中结点的个数除下头结点外的个数
出口参数:
--------------------------------------------------------------------*/
void InsertList(ListNode *h,int i,char name[],int e,int n)
{
    ListNode *p,*q;  //先定义2个指向一个结点的指针
    if(i<1||i>n+1)
        printf("Error!!!\n");
    else
    {
        int j=1;
        p=h;//将指针p指向要链表的头结点
        while(j<i)
        {
            p=p->next;
            j++;
        }
        if((q=(ListNode *)malloc(sizeof(ListNode)))==NULL)/*为要插入的
   结点分配内存空间*/
            printf("内存空间不足!!!\n");
        else
        {
            count++;
            strcpy(q->name,name);//将名字拷到要插入的节点内
            q->score=e;//将要插入的节点中分数赋值
            q->next=p->next;/*这个是将新插入的结点指针域指向
   上一个结点指针域指向的结点地址即为p->next*/
            p->next=q;/*将要插入结点位置前面的结点指针域
   指向现在插入的结点首地址*/
        }
    }
}


    /*--------------------------------------------------------------------
    函数名称:DeleteList(ListNode *h, int i, int n)
    函数功能:删除链表结点
    入口参数: h: 头结点地址 i:要删除的结点所在位置  n:链表中结点的个数除下头结点外的个数
    出口参数:
    --------------------------------------------------------------------*/
    void DeleteList(ListNode *h, int i, int n)
    {
        ListNode *p,*q;//首先定义2个指向结点型结构体的指针
        char name[10];
        int score;
        if(i<1||i>count+1)
            printf("Error!!!\n");
        else
        {
            int j=1;
            p=h;//将指针指向链表的头结点首地址
            while(j<i)
            {
                p=p->next;
                j++;
            }
            q=p->next;/*q指向要删除的位置之前的那个结点指针域指向的
   地址q指向的结点就是要删除的结点*/
            p->next=q->next;/*这个就是将要删除的结点的前面那个结点
   的指针域指向要删除的结点指针域中存放的下个结点的
   首地址从而实现了删除第i个结点的作用*/
            strcpy(name,q->name);
            score=q->score;
            free(q);//释放q指向的结点
            printf("name:%s\tscore:%d has been deleted!!!\n",name,score);
        }
    }


    /*--------------------------主函数-------------------------------*/
    int main()
    {
        ListNode *h;//h指向结构体NODE
        int i = 1, n, score;
        char name [10];


        while ( i )
        {
            /*输入提示信息*/
            printf("1--建立新的链表\n");
            printf("2--添加元素\n");
            printf("3--删除元素\n");
            printf("4--输出当前表中的元素\n");
            printf("0--退出\n");


            scanf("%d",&i);
            switch(i)
            {
            case 1:
                h=CreateList();/*创建链表*/
                printf("list elements is : \n");
                PrintList(h);
                break;


            case 2:
                printf("input the position. of insert element:");
                scanf("%d",&i);
                printf("input name of the student:");
                scanf("%s",name);
                printf("input score of the student:");
                scanf("%d",&score);
                InsertList(h,i,name,score,count);
                printf("list elements is:\n");
                PrintList(h);
                break;


            case 3:
                printf("input the position of delete element:");
                scanf("%d",&i);
                DeleteList(h,i,count);
                printf("list elements in : \n");
                PrintList(h);
                break;


            case 4:
                printf("list element is : \n");
                PrintList(h);
                break;
            case 0:
                return;
                break;
            default:
                printf("ERROR!Try again!\n");
            }
        }
        return 0;
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值