周日链表 12.2

线性表的定义:
定义n个数据元素的有限序列,记作(a1, a2, …, an)ai 是表中数据元素,n 是表长度;
线性表的特点:
1.除第一个元素外,其他每一个元素有一个且仅有一个
直接前驱;
2.除最后一个元素外其他每一个元素有一个且仅有一个
直接后继;

链表是顺序访问,数组可以随机访问;
顺序访问的效率要大于随机访问的效率;

1.第一种情况:在第一个结点前插入

#include <stdio.h>
#include <stdlib.h>
struct node 
{
    int num;
    struct node *next;
};
typedef struct node Node;
typedef struct node * link;

void create_link(link * head)
{
    *head = NULL;
}

void insert_node_head(link * head,link new_node)
{
    new_node->next = *head;
    *head=new_node;
}
void display_node(link head)
{
    link p;
    p = head;
    while(p!=NULL)
    {
        printf("num=%3d\n",p->num);
        p=p->next;
    }
    
}
int main()
{
    link head = NULL;
    link new_node = NULL;
    int i;
  
    create_link(&head);
    for(i=0;i<10;i++)
    {
        new_node = (link)malloc(sizeof(Node));
        if(new_node == NULL)
        {
            printf("malloc error!\n");
            exit(-1);
        }
        new_node->num = i;
        insert_node_head(&head,new_node);
    }

    

    display_node(head);
    return 0;
}

2.从尾部的插入,以及从中间位置插入某个值。

#include <stdio.h>
#include <stdlib.h>

struct node
{
    int num;
    struct node * next;
};
typedef struct node Node;
typedef struct node * link;

void create_link(link *head)
{
    * head = NULL;
}

void insert_node_tail(link * head, link new_node)
{
    link p;
    p= *head;
    if(p == NULL)
    {
        *head = new_node;
        new_node->next=NULL;
    }
    else
    {
        while(p->next!=NULL)
        {
            p=p->next;
        }
        p->next=new_node;
        new_node->next=NULL;
    }
}
void display_node(link head)
{
    link p;
    p=head;
    while(p!=NULL)
    {
        printf("num=%3d\n",p->num);
        p=p->next;
    }
}

void create_node(link *new_node , int i)
{
    *new_node = (link)malloc(sizeof(Node));
        if((*new_node) == NULL)
        {
            printf("malloc error!\n");
            exit(-1);
        }
        (*new_node)->num = i;

}
void insert_node_mid(link * head, link new_node, int num_loc)
{
    link p;
    p= *head;
    while(p->num != num_loc && p->next != NULL)
    {
        p=p->next;
    }
    if(p == NULL)
    {
        new_node->next = * head;
        *head = new_node;
    }
    else if( p->next == NULL && p->num != num_loc)
    {
        p->next = new_node;
        new_node->next = NULL;
    }
    else
    {
        new_node->next = p->next;
        p->next = new_node;
    }
}
int main()
{
    link head = NULL;
    link new_node = NULL;
    int i;
    int num_loc, num_val;

    create_link(&head);
    for(i=0;i<10;i++)
    {
        create_node(&new_node, i);
        insert_node_tail(&head,new_node);
    }
    printf("please input loc:");
    scanf("%d",&num_loc);
    printf("please input val:");
    scanf("%d",&num_val);
    create_node(&new_node,num_val);
    insert_node_mid(&head, new_node, num_loc);
    display_node(head);
    return 0;
}

3.前插、中间插、以及删除某个节点。


#include <stdio.h>
#include <stdlib.h>

struct node
{
    int num;
    struct node * next;
};
typedef struct node Node;
typedef struct node * Link;

void create_link(Link * head )
{
    * head = NULL;
}

void insert_node_head(Link * head, Link new_node)
{
    new_node->next = *head;
    *head = new_node;
}
void display_node(Link head)
{
    Link p;
    p = head;
    while(p != NULL)
    {
        printf("num=%3d\n",p->num);
        p=p->next;
    }
}
void create_node(Link * new_node, int i)
{
*new_node = (Link)malloc(sizeof(Node));
        if(*new_node == NULL)
        {
            printf("malloc error!\n");
            exit(-1);
        }
        (*new_node)->num = i;

}

void insert_node_mid(Link * head, Link new_node, int num_loc)
{
    Link p, q;
    p=q=*head;
    while( p->num != num_loc && p->next != NULL)
    {
        q=p;
        p=p->next;
    }
    if(p == *head)
    {
        new_node->next = *head;
        *head = new_node;
    }
    else if( p->next == NULL && p->num != num_loc)
    {
        p->next = new_node;
        new_node->next = NULL;
    }
    else
    {
        q->next=new_node;
        new_node->next=p;
    }

}

void delete_node(Link * head, int num_val)
{
    Link p, q;
    p=q=*head;
    while(p->num != num_val && p->next != NULL )
    {
        q=p;
        p=p->next;
    }
    if( (*head)==NULL)
    {
        printf("Link is empty!\n");
    }
    else if(p->next == NULL && p->num != num_val)
    {
        printf("no such node\n");
    }
    else
    {
        if(p == *head)
        {
            *head = (*head)->next;
            free(p);
        }
        else
        {
            q->next = p->next;
            free(p);
        }
    }

}
int main()
{
    Link head = NULL;
    Link new_node = NULL;
    int i;
    int num_loc, num_val;

    create_link(&head);
    for(i=0;i<10;i++)
    {
        create_node(&new_node, i);
        insert_node_head(&head, new_node);
    }
    /*printf("please input loc:");
    scanf("%d",&num_loc);
    printf("please input val:");
    scanf("%d",&num_val);
    create_node(&new_node, num_val);
    insert_node_mid(&head, new_node, num_loc);*/

    display_node(head);
    printf("please input deleted numeber:");
    scanf("%d",&num_val);

    delete_node(&head,num_val);
    display_node(head);
    return 0;
   
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值