数据结构——单循环链表一部分功能的C语言实现

单循环链表

主要好处在于方便插入删除结点,适用于动态性比较强的数据结构;缺点是每一个结点只能通过上一个结点来访问,随机访问不方便。
//数据结构:单循环链表 
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct Node{
    int data;
    Node *next;
}Node,*PNode;//节点 

typedef struct List{
    PNode head;
    PNode tail;
    int length;
}List,*PList;//链表

void initList(PList x){
    PNode headNode=(PNode)malloc(sizeof(Node));
    if(!headNode)
        exit(0);
    headNode->data=0;
    headNode->next=headNode;
    x->head=headNode;
    x->tail=headNode;
    x->length=0;
}//初始化链表 

int insertNode(PList x,int i,int e){
    if(i>x->length)
        return 1;

    PNode newNode=(PNode)malloc(sizeof(Node));
    if(!newNode)
        exit(0);
    newNode->data=e;

    PNode p=x->head;
    for(int j=0;j<i;j++)
        p=p->next;

    newNode->next=p->next;
    p->next=newNode;

    if(i==x->length)
        x->tail=newNode;

    x->length++;

    return 0;
}//插入结点 

int deleteNode(PList x,int i){
    if(i>x->length)
        return 1;

    PNode p=x->head;
    for(int j=0;j<i-1;j++)
        p=p->next;

    PNode delNode=p->next;
    p->next=delNode->next;

    if(i==x->length)
        x->tail=p;

    free(delNode);
    x->length--;

    return 0;
}//删除结点 

void outputList(PList x){
    PNode p=x->head->next;
    for(int i=0;i<x->length;i++){
        printf("%3d",p->data);
        p=p->next;
    }
    printf("\n");
}//打印链表 

int deleteList(PList x){
    while(x->length>0){
        deleteNode(x,x->length);
        x->length--;
    }
    free(x->head);
    x->head=NULL;
    x->tail=NULL;
}//删除链表 

int main(){
    //初始化链表 
    PList myList=(PList)malloc(sizeof(List));
    initList(myList);

    //添加节点
    int e=0;
    printf("请输入要添加的数据(10个):");
    for(int i=0;i<10;i++){
        scanf("%d",&e);
        insertNode(myList,myList->length,e);
    } 
    outputList(myList);

    //删除节点
    deleteNode(myList,myList->length);
    outputList(myList);

    //删除链表
    deleteList(myList); 

    return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
链表中插入元素的C语言实现,可以按照以下步骤进行: 1. 创建一个新节点,用于存储要插入的元素。 2. 遍历链表,找到要插入节点的位置。可以使用一个指针指向当前节点,然后依次往下遍历,直到找到插入位置。 3. 将新节点的指针指向当前节点的下一个节点,同时将当前节点的指针指向新节点。 下面是一个简的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 链表节点结构体 struct node { int data; struct node *next; }; // 在链表中插入元素 void insert_node(struct node **head, int data, int index) { // 创建新节点 struct node *new_node = (struct node *)malloc(sizeof(struct node)); new_node->data = data; new_node->next = NULL; // 如果链表为空,直接将新节点作为头节点 if (*head == NULL) { *head = new_node; return; } // 如果插入位置是头节点之前,直接将新节点作为头节点 if (index <= 0) { new_node->next = *head; *head = new_node; return; } // 遍历链表,找到插入位置 struct node *current_node = *head; int i = 0; while (i < index - 1 && current_node->next != NULL) { current_node = current_node->next; i++; } // 如果插入位置超出了链表范围,直接将新节点作为尾节点 if (i < index - 1) { current_node->next = new_node; return; } // 插入新节点 new_node->next = current_node->next; current_node->next = new_node; } // 打印链表 void print_list(struct node *head) { printf("List: "); while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("\n"); } int main() { // 创建链表 struct node *head = NULL; insert_node(&head, 1, 0); insert_node(&head, 2, 1); insert_node(&head, 3, 2); print_list(head); // 在链表中插入元素 insert_node(&head, 4, 1); print_list(head); return 0; } ``` 输出结果为: ``` List: 1 2 3 List: 1 4 2 3 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值