程序员面试宝典之数据结构基础-----③单链表的插入

单链表的插入,注意分三种情况,插入到头部前,插入到尾部后,其他。

仍然是细节决定成败。。。


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
//notice the define of struct, the node is not a variable,it is the name of the struct.so it can be used to define the struct variable.
//notice the key word of typedef. if there is not the key word ,the node will have different properties.
typedef struct student
{
    int data;
    struct student* next;
}node;
node* creat()
{
    node* head,*p,*s;
    int x=0,cycle = 1 ;
    head=(node*)malloc(sizeof(node));   //notice the malloc is contained in the lib of <stdlib.h> and must malloc the space for the node before you use it.
    p = head;
    while(cycle)
    {
        printf("\nPlease input the data:");
        scanf("%d",&x);
        if(x!= 0)   // only for test ,so the '0' is the symblo of the end but the value of the node,I ignore this kind of possible. Of course you can do it .
        {
            s = (node*)malloc(sizeof(node));
            s->data = x;
            printf("\n%d",s->data);
            p->next = s;
            p = s;
        }
        else
        cycle = 0;

    }
    head = head->next;
    p->next = NULL;

    printf("\n  yyy  %d %d %d %d",head->data,head->next->data,head->next->next->data, head->next->next->next->data);//print four node for test.
    return(head);
}
int length(node* head)
{
    int n = 0;
    node *p;
    p = head;
    while(p!=NULL)
    {
        p = p->next;
        n++;

    }
    return n;
}
void print(node* head)
{
    node *p;
    int n;
    n = length(head);
    printf("\nNow,These %d records are:",n);
    p = head;
    if(head!= NULL)
    {
        while(p != NULL)
        {
        printf("\n   uuu  %d   ",p->data);
        p = p->next;
        }
    }
}
//删除head单链表中值为num的节点:
node* del_node(node* head ,int num)
{
    //注意分两种情况,删除的是头结点或者非头结点
    node* p1, *p2;
    p2 = head;
    while(num != p2->data && NULL != p2->next)
    {
        p1 = p2;
        p2 = p2->next;
    }
    if(num == p2->data)
    {
        if(head == p2)
        {
            head = p2->next;
            free(p2);
        }
        else
        {
            p1->next = p2->next;
            free(p2);
        }
    }
    else
    {
        printf("%d could not been found ",num);
    }
return head;

}
//此处是给排好序的链表中插入值。
//注意三种情况,插入头部之前,插入尾部之后,其他。
node* insert(node* head,int num)
{
    node* p1,*p2,*p0;
    p1 = head;
    p0 = (node*)malloc(sizeof(node));//在出入某个值之前要先给改值的存放申请一段空间。
    p0->data = num;
    while(p0->data > p1->data && NULL!= p1->next)
    {
        p2 = p1;
        p1 = p1->next;
    }
    if(p0->data <= p1->data)
    {
        if(head == p1)//插入头部之前
       {
           p0->next = p1;
           head = p0;
       }
       else             //插入之中
       {
           p2->next = p0;
           p0->next = p1;
       }
    }
    else                //插入到尾部
    {
        p1->next = p0;
        p0->next = NULL;
    }
    return head;
}
int main()
{
    node* head = creat();
    print(head);
    node* head_new = del_node(head,1);//此处注意,del_node是有返回值的,返回一个新的head,所以,下一个print的时候要给与新的head值。
    print(head_new);
    insert(head_new,8);
    print(head_new);
}

此处不再给出结果,三种情况都试过,可以正常运行并打印出正确结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值