前插法创建单链表(C语言实现)

#include<stdio.h>
#include<stdlib.h>
struct node{
    int data;//结点的数据域
    struct node *next;//结点的指针域
};
int main()
{
    struct node *head,*p;
    int i,n;
    head = (struct node *)malloc(sizeof(struct node));//为头结点申请空间
    head->next = NULL;//头结点指针域指向空
    printf("请输入链表长度:");
    scanf("%d",&n);//输入表长
    for(i=1;i<=n;i++)
    {
        p = (struct node *)malloc(sizeof(struct node));//为新结点申请空间
        scanf("%d",&p->data);//向新结点p的数据域中添加元素
        p->next = head->next;
        head->next = p;//头结点的指针域指向新结点
    }
    while(1)//遍历元素
    {
        printf("%d ",p->data);
        if(p->next==NULL)
            break;//如果是最后一个结点则跳出循环
        p = p->next;//p指向下一个结点
    }
    return 0;
}

  • 10
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是循环单链表的 C 语言实现代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct Node { int data; struct Node *next; } Node; // 创建链表 Node *createList(int n) { Node *head = NULL, *tail = NULL; for (int i = 0; i < n; i++) { Node *node = (Node *)malloc(sizeof(Node)); printf("请输入第 %d 个节点的值:", i + 1); scanf("%d", &node->data); node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } tail->next = head; // 将尾节点指向头节点,形成循环链表 return head; } // 遍历链表 void traverseList(Node *head) { if (head == NULL) { printf("链表为空!\n"); return; } Node *p = head; do { printf("%d ", p->data); p = p->next; } while (p != head); printf("\n"); } // 插入节点 Node *insertNode(Node *head, int pos, int value) { Node *node = (Node *)malloc(sizeof(Node)); node->data = value; node->next = NULL; if (pos == 1) { // 在头节点插入 Node *tail = head; while (tail->next != head) { tail = tail->next; } node->next = head; head = node; tail->next = head; } else { // 在其他位置插入 Node *p = head; for (int i = 1; i < pos - 1; i++) { p = p->next; if (p == head) { // pos 超出链表长度 printf("插入位置无效!\n"); return head; } } node->next = p->next; p->next = node; } return head; } // 删除节点 Node *deleteNode(Node *head, int pos) { if (head == NULL) { printf("链表为空!\n"); return NULL; } if (pos == 1) { // 删除头节点 Node *tail = head; while (tail->next != head) { tail = tail->next; } Node *p = head; head = head->next; tail->next = head; free(p); } else { // 删除其他节点 Node *p = head, *q = NULL; for (int i = 1; i < pos; i++) { q = p; p = p->next; if (p == head) { // pos 超出链表长度 printf("删除位置无效!\n"); return head; } } q->next = p->next; free(p); } return head; } // 主函数 int main() { int n, pos, value; printf("请输入链表长度:"); scanf("%d", &n); Node *head = createList(n); printf("创建的链表为:"); traverseList(head); printf("请输入要插入的节点位置和值(用空格隔开):"); scanf("%d %d", &pos, &value); head = insertNode(head, pos, value); printf("插入节点后的链表为:"); traverseList(head); printf("请输入要删除的节点位置:"); scanf("%d", &pos); head = deleteNode(head, pos); printf("删除节点后的链表为:"); traverseList(head); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值