linux c语言环境 非循环单链表实际应用示例

一、代码:

#include <stdio.h>
#include <stdlib.h>
#define _ULL unsigned long long

/*definition of single link:each node can only visit it's next node, and visit it's pre node,the last link point to NULL*/
/*use scene:sort number. get the minnest number from R[0]~R[n-1] and swap with R[0]; compare with other sort method*/

//Link is a struct which has a ull type and a Link * pointer type; mylink is a new name of struct Link by use of typedef
typedef struct Link
{
    _ULL value;
    struct Link *next;    
}mylink;

/*
quantity: init a mylink which has quantity number; add by tail;
https://blog.csdn.net/qq_52607834/article/details/115922172
*/
mylink* init_mylink(_ULL quantity)
{
    printf("begin to init...\n");
    _ULL i = 0;
    mylink* head_node = (mylink*)malloc(sizeof(mylink)); //init a head node
    head_node->value=0;
    printf("in init_mylink, i:%d\n", i);
    
    mylink* p_cursor_node = head_node; //a cursor pointer point to head_node
    for(i = 1; i < quantity; i ++)
    {
        /*
        create a new pointer and point it to an assigned value
        each tmp_mylink has two addresses: an address of itself + an address point to other link node
        */
        mylink* tmp_mylink = (mylink*)malloc(sizeof(mylink)); //malloc a new pointer
        tmp_mylink->value = i;     //value assignment
        tmp_mylink->next = NULL;   /
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C语言设计循环单链表的代码: ``` #include <stdio.h> #include <stdlib.h> // 定义循环单链表结构体 typedef struct Node { int data; struct Node *next; } Node, *LinkList; // 初始化循环单链表 LinkList InitList() { LinkList L = (LinkList)malloc(sizeof(Node)); L->next = L; return L; } // 求循环单链表中元素个数 int Length(LinkList L) { int len = 0; Node *p = L->next; while (p != L) { len++; p = p->next; } return len; } // 在循环单链表的第i个位置插入元素e int Insert(LinkList L, int i, int e) { if (i < 1 || i > Length(L) + 1) { return 0; } Node *p = L; for (int j = 1; j < i; j++) { p = p->next; } Node *s = (Node*)malloc(sizeof(Node)); s->data = e; s->next = p->next; p->next = s; return 1; } // 删除循环单链表中第i个位置的元素,并将其值赋给e int Delete(LinkList L, int i, int *e) { if (i < 1 || i > Length(L)) { return 0; } Node *p = L; for (int j = 1; j < i; j++) { p = p->next; } Node *q = p->next; *e = q->data; p->next = q->next; free(q); return 1; } // 获取循环单链表中第i个位置的元素的值 int GetElem(LinkList L, int i, int *e) { if (i < 1 || i > Length(L)) { return 0; } Node *p = L->next; for (int j = 1; j < i; j++) { p = p->next; } *e = p->data; return 1; } // 测试循环单链表的操作 int main() { LinkList L = InitList(); Insert(L, 1, 1); Insert(L, 2, 2); Insert(L, 3, 3); int len = Length(L); printf("Length: %d\n", len); int e; Delete(L, 2, &e); printf("Delete: %d\n", e); GetElem(L, 2, &e); printf("GetElem: %d\n", e); return 0; } ``` 注意:以上代码仅供参考,实际应用中需要根据具体需求进行修改。 关于循环单链表的操作,初始化和求元素个数比较简单,主要是插入、删除和取元素的操作需要注意。在插入和删除元素时,需要先找到要操作的位置,然后进行相应的操作。在取元素时,也需要先找到要取的位置,然后将其值赋给指定的变量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值