结构体实现链表

链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。因此我们可以通过结构体,结构体指针来实现一个单链表,包括基本的增删查改。注:(所有给出的操作均保证有解且合法)。

1.链表的初始化

typedef struct ListNode
{
    int data; //数据域
    struct ListNode *next; //指针域 
}node; //定义结构体

node *L = NULL; //头指针
node *T = NULL; //尾指针

void Init()
{
    L = (node*)malloc(sizeof(node));
    T = (node*)malloc(sizeof(node));
}

2.链表的头插法

void To_HeadListNode(int x) //将元素x插入链表中
{
    node *p = (node*)malloc(sizeof(node));
    p->data = x;
    p->next = L->next;
    L->next = p;
}

3.链表的尾插法

void To_TailListNode(int x) //将元素x插入链表中
{
    node *p = (node*)malloc(sizeof(node));
    if(L->next == NULL) L->next = p;
    T->next = p;
    p->data = x;
    p->next = NULL;
    T = p;
}

4.按位插入

void Insert(int k, int x) //在第k个结点后插入x
{
    node *p = (node*)malloc(sizeof(node));
    node *t = L;
    while(k--)
    {
        t = t->next;
    }
    p->data = x;
    p->next = t->next;
    t->next = p;
}

5.查找

(1)找到第k个结点的值

int Findvalue(int k)
{
    node *p = L;
    while(k--)
    {
        p = p->next;
    }
    return p->data;
}

(2)查找值为x的结点下标

int FindIndex(int x)
{
    node *p = L;
    int j = 0;
    while(p != NULL && p->data != x)
    {
        p = p->next;
        j++;
    }
    return j;
}

6.删除

void Delete(int k) //删除第k个结点后的结点
{
    node *t = L;
    while(k--)
    {
        t = t->next;
    }
    node *p = t->next;
    t->next = t->next->next;
    free(p);
}

7.遍历

void Print() //链表的遍历
{
    node *p = L->next;
    while(p != NULL)
    {
        printf("%d ",p->data);
        p = p->next;
    }
}
将文本文件中的结构体数据转换为链表形式通常涉及以下步骤: 1. 文件读取:首先,需要读取存储结构体数据的文本文件。通常这涉及到使用文件I/O操作,比如在C语言中使用`fopen`、`fread`、`fclose`等函数来打开文件,读取内容,并最终关闭文件。 2. 数据解析:读取到的数据通常是以特定格式(如逗号分隔、空格分隔或固定长度等)存储的,需要根据存储格式解析文本中的每一行数据,识别出结构体中的每个字段。 3. 结构体创建:根据结构体的定义创建相应的数据结构。在C语言中,这通常意味着定义一个结构体类型,并为每个字段分配空间。 4. 数据转换:将解析出来的数据赋值给新创建的结构体变量,然后将这个结构体变量链接到链表中。在链表中,每个节点通常包含数据和一个指针,指向下一个链表节点。 5. 链表操作:进行必要的链表操作,如插入、删除、遍历等,以便对结构体数据进行管理。 6. 错误处理:在读取文件和解析数据的过程中要进行错误处理,确保程序的健壮性。 下面是一个简化的例子,描述如何将文本文件中的结构体数据转换为链表节点的过程: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义链表节点 typedef struct Node { struct Data data; // 结构体类型 struct Node* next; } Node; // 定义数据结构 typedef struct Data { int id; char name[50]; // 可以添加更多的字段 } Data; // 从文件读取数据并创建链表节点 Node* createNodeFromFile(const char* filename) { FILE* file = fopen(filename, "r"); if (file == NULL) { perror("File opening failed"); return NULL; } Node* head = NULL; Node* tail = NULL; Data data; while (fscanf(file, "%d, %s", &data.id, data.name) != EOF) { // 创建新节点 Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; // 插入到链表尾部 if (head == NULL) { head = newNode; } else { tail->next = newNode; } tail = newNode; } fclose(file); return head; } int main() { // 假设有一个名为 "data.txt" 的文件,里面存储了结构体数据 Node* list = createNodeFromFile("data.txt"); // 使用链表 list 进行后续操作... // 释放链表内存 Node* current = list; while (current != NULL) { Node* temp = current; current = current->next; free(temp); } return 0; } ``` 请注意,上述代码仅为示例,实际应用中需要根据文本文件的具体格式和结构体的定义来进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Eliauk-GX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值