数据结构--单链表

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

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

node * init(int );//创建链表 
void display(node *);//遍历链表 
void dis(node *);//释放链表 
void modify(node *, int ,int );//修改链表 
void del(node * , int );//删除节点 
void insert(node *, int ,int );//插入节点 

int main(void)
{
    int n;
    node * head, * current;

    scanf("%d", &n);

    head = init(n);
    display(head);

    insert(head, 2, 100);
    display(head);

    modify(head, 5, 10);
    display(head);

    del(head, 4);
    display(head);

    dis(head);

    return 0;
}

node * init(int n)
{
    int i;
    node * head, * current, * pre;

    head = (node *)malloc(sizeof(node));
    head->next = NULL;
    pre = head;
    for (i = 1; i <= n; ++i)
    {
        current = (node *)malloc(sizeof(node));
        pre->next = current;
        current->num = i;
        current->data = 10 - i;
        current->next = NULL;
        pre = current;
    }

    return head;
}

void insert(node * head, int p, int k)
{
    node * current, * t, * newnode;

    current = head;
    while (current->next->num != p)//找到编号为p的节点的前驱 
        current = current->next;
    t = current->next;//t是编号为p的节点 
    newnode = (node *)malloc(sizeof(node));
    newnode->num = current->num;
    newnode->data = k;
    newnode->next = t;//新节点的后继是编号为p的节点 
    current->next = newnode;//新节点的前驱是编号为p的节点的前驱 
    current = newnode;//改变编号 
    while (current->next != NULL)
    {
        current->num++;
        current = current->next;
    }
    current->num++;
}

void modify(node * head, int p, int k)
{
    node * current;

    current = head->next;
    while (current->num != p)//找到编号为p的节点 
        current = current->next;
    current->data = k;
}

void del(node * head, int p)
{
    node * current, * t;

    current = head;
    while (current->next->num != p)//找到编号为p的节点的前驱 
        current = current->next;
    t = current->next;
    current->next = current->next->next;
    free(t);
    current = current->next;//改变编号 
    while (current->next != NULL)
    {
        current->num--;
        current = current->next;
    }
    current->num--;
}

void display(node * head)
{
    node * current;

    current = head->next;
    while (current->next != NULL)
    {
        printf("%d ", current->num);
        current = current->next;
    }
    printf("%d\n", current->num);

    current = head->next;
    while (current->next != NULL)
    {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("%d\n\n", current->data);
}

void dis(node * head)
{
    node * current, * p;

    current = head->next;
    while ((p = current->next) != NULL)
    {
        free(current);
        current = p;
    }
    free(current);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单链表是一种常见的数据结构,它由一个或多个节点组成,每个节点包含一个数据域和一个指针域,指针域指向下一个节点。 实现单链表的查找位序算法函数需要以下步骤: 1. 定义一个指针p指向链表的头节点,并定义一个变量count,用于计数。 2. 从头节点开始,遍历链表,当p指向某个节点时,计数器count加1。 3. 如果p指向的节点的数据与目标数据相等,则返回当前的计数器count,即为目标数据的位序。 4. 如果p指向的节点不是目标数据,则将p指向下一个节点,重复步骤3。 5. 如果遍历完链表后仍未找到目标数据,则返回-1,表示未找到。 下面是C语言实现单链表查找位序算法函数的代码示例: ```c #include <stdio.h> #include <stdlib.h> // 定义单链表节点结构 typedef struct Node { int data; // 数据域 struct Node* next; // 指针域 } Node; // 查找位序的算法函数 int findPosition(Node* head, int target) { Node* p = head; // 指向头节点 int count = 0; // 计数器初始化为0 while (p != NULL) { count++; // 计数器加1 if (p->data == target) { return count; // 找到目标数据,返回当前计数器的值 } p = p->next; // 指向下一个节点 } return -1; // 遍历完链表未找到目标数据,返回-1 } int main() { // 创建链表 Node* head = (Node*)malloc(sizeof(Node)); head->data = 1; // 头节点数据为1 Node* node1 = (Node*)malloc(sizeof(Node)); node1->data = 2; Node* node2 = (Node*)malloc(sizeof(Node)); node2->data = 3; head->next = node1; node1->next = node2; node2->next = NULL; // 查找位序示例 int target = 3; // 目标数据为3 int position = findPosition(head, target); if (position != -1) { printf("目标数据 %d 的位序为 %d\n", target, position); } else { printf("未找到目标数据 %d\n", target); } // 释放链表内存 free(node2); free(node1); free(head); return 0; } ``` 在上述代码中,我们首先定义了一个指向头节点的指针p和一个计数器count,然后使用while循环遍历链表。当p指向某个节点时,计数器加1,并判断该节点的数据是否与目标数据相等。如果找到了目标数据,则返回当前计数器的值,即为目标数据的位序。如果遍历完链表仍未找到目标数据,则返回-1表示未找到。最后在主函数中演示了调用该算法函数的示例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值