链表操作总结

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

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

struct node *head, *tail, *p, *q, *r;

void SequenceCreate ()                     /*顺序建立链表*/
{
    /*建立一个空结点*/
    head = ( struct node *) malloc ( sizeof ( struct node ));
    head->next = NULL;
    tail = head;
    int n;
    scanf("%d", &n);
    while( n-- )
    {
        p = ( struct node *) malloc ( sizeof ( struct node ));
        scanf("%d", &p->data);
        /*把数据节点挂到尾部*/
        tail->next = p;
        tail = p;
    }
}

void InvertedSCreate ()                   /*逆序建立链表*/
{
    /*建立一个空结点*/
    head = ( struct node *) malloc ( sizeof ( struct node ));
    head->next = NULL;
    int n;
    scanf("%d", &n);
    while ( n-- )
    {
        p = ( struct node *) malloc ( sizeof ( struct node ));
        scanf("%d", &p->data);
        /*把数据节点插到头节点后作链第一个节点*/
        p->next = head->next;
        head->next = p;
    }
}

void Insert ( struct node *p, int key )   /*插入key*/
{
    /*插入前最重要的是找到 p 节点,即插入位置的前驱节点*/
    q = ( struct node * ) malloc ( sizeof( struct node ));
    q->data = key;
    q->next = NULL;
    q->next = p->next;
    p->next = q;
    //逆序建立链表到懂了的话这段很容易理解
}

void del ( struct node *head, int key )  /*删除key*/
{
    int flag = 0;
    p = head;
    while(p->next)
    {
        /*最重要的依然是找到前驱结点*/
        if(p->next->data == key)
        {
            q = p->next;
            p->next = q->next;
            free(q);
            break;
        }
        else p = p->next;
    }
}

void LinkListprint ( struct node *head ) /*输出链表*/
{
    p = head->next;
    while(p->next)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("%d\n", p->data);
}

int main()
{

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值