【C语言】-单链表元素的添加、删除

#include <stdio.h>
#include <stdlib.h>
// 定义数据结构
/* 节点 Node */
typedef int Element;
struct Node
{
    Element data;
    struct Node *next;
};

// 创建单链表
struct Node * createList(void);

struct Node * createList(void)
{
    struct Node *head = NULL;
    struct Node *temp = NULL;
    struct Node *tail = NULL; 
    int data; 
    scanf("%d",&data);
    while (data)
    {
        temp = (struct Node *)malloc(sizeof(struct Node));  
        if (temp == NULL)
        {
            printf("alloc error!");
        }
        else
        {
            temp->data = data;
            temp->next = NULL;
            if (head == NULL)
            {
                head = tail = temp;
            }
            else
            {
                tail->next = temp;
                tail = temp;
            }
        }
        scanf("%d",&data);
    }   
    return head;
}

//  输出单链表各个元素的数值
void printList(struct Node *head);

void printList(struct Node *head)
{
    struct Node *p = NULL;
    p = head;
    while (p)
    {
        printf("%d  ",p->data);
        p = p->next;
    }
}

// 求链表长度
int length(struct Node *head);

int length(struct Node *head)
{
    int count = 0;
    struct Node *p;
    p = head;
    while (p)
    {
        count++;
        p=p->next;
    }
    return count;
}

// 求pos位置的数据
int elementAt(struct Node *head,int pos);

int elementAt(struct Node *head,int pos)
{
    if (pos <= 0 || pos > length(head))
    {
        return 0;
    }
    int loc = 1;
    struct Node *p = head;
    while (loc < pos)
    {
        loc++;
        p = p->next;
    }
    //p->data = 12;
    return p->data;
}

void insertHeadNode(struct Node **head,int value);

void insertHeadNode(struct Node **head,int value)
{
    struct Node *tmp;
    tmp = (struct Node *)malloc(sizeof(struct Node));
    tmp->data = value;
    tmp->next = NULL;
    if (head == NULL)
    {
        *head = tmp;
    }
    else
    {
        tmp->next = *head;
        *head = tmp;
    }
}

// 在尾部添加一个元素
void insertTailNode(struct Node **head,int value);

void insertTailNode(struct Node **head,int value)
{
    struct Node *tmp;
    tmp = (struct Node *)malloc(sizeof(struct Node));
    tmp->data = value;
    tmp->next = NULL;
    if (head == NULL)
    {
        *head = tmp;
    }
    else{
        struct Node *q = NULL;
        struct Node *p = *head;
        while (p)
        {
            q = p;
            p = p->next;
        }
        q->next = tmp;
    }
}

//在列表的第 pos 个位置添加一个 value 元素
int insertNode(struct Node **head,int pos,int value);

int insertNode(struct Node **head,int pos,int value)
{
    if (pos<=0)
    {
        return 0;
    }
    if (pos == 1)
    {
        insertHeadNode(head, value);
        return 1;
    }
    if (pos > length(*head))
    {
        insertTailNode(head, value);
        return 1;
    }
    struct Node *tmp;
    tmp = (struct Node *)malloc(sizeof(struct Node));
    tmp->data = value;
    tmp->next = NULL;
    struct Node *p,*q;
    p = *head;
    int loc = 1;

    // 定位指针位置
    while (loc < pos)
    {
        loc++;
        q = p;
        p = p->next;
    }
    tmp->next = q->next;
    q->next = tmp;
    return 1;
}

int main(int argc, const char * argv[])
{
    struct Node *head = NULL;
    head = createList();
    printList(head);
    //1、
    int pos = 0;
    printf("第%d个元素的数值:%d",pos,elementAt(head,pos));
    //3、
    printf("\n");
    insertHeadNode(&head, 100);
    printList(head);
    printf("\n");
    insertTailNode(&head, 200);
    printList(head);
    printf("\n");
    insertNode(&head, 2, 300);
    printList(head);
    return 0;
}

原文链接: https://www.cnblogs.com/whuishine/archive/2013/03/06/2946225.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/79854

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午7:15
下一篇 2023年2月9日 下午7:16

相关推荐