c语言中结点,C语言在链表中插入结点

/********************************************************************************

该程序实现链表的结点插入:

程序中的void Deletestu(TYM *head)是用来释放已经生成的链表空间,程序中的

TYM *Insert(TYM *head,int pos,int n)函数是用来插入新的结点并返回整个链表的首地址。

在这程序中,主函数首先默认链表的长度为5*TYM,然后输入五个变量的值并输出。接着是

插入结点通过输入i和n的值来设置插入的位置和长度,最后将整理后的新链表中的信息一

起输出。

*********************************************************************************/

#include

#include

typedef struct stu                                                       //定义TYM的结构类型

{

char name[20];

int num;

struct stu *next;

}TYM;

void Deletestu(TYM *head)                                       //释放函数

{

TYM *p;                                                                 //定义TYM类型的指针

for(p=head;p!=NULL;head = p)            

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C语言实现双向链表插入结点并输出的代码: ```c #include <stdio.h> #include <stdlib.h> // 双向链表结构体 struct Node { int data; struct Node* prev; struct Node* next; }; // 在链表插入结点 struct Node* insertAtBeginning(struct Node* head, int newData) { // 创建新结点 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = newData; newNode->prev = NULL; newNode->next = head; // 如果链表不为空,修改头结点的prev指针 if (head != NULL) { head->prev = newNode; } // 返回新的头结点 return newNode; } // 在链表插入结点 struct Node* insertAtEnd(struct Node* head, int newData) { // 创建新结点 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = newData; newNode->next = NULL; // 如果链表为空,新结点即为头结点 if (head == NULL) { newNode->prev = NULL; return newNode; } // 找到链表尾部结点并修改指针 struct Node* curr = head; while (curr->next != NULL) { curr = curr->next; } curr->next = newNode; newNode->prev = curr; // 返回头结点 return head; } // 输出链表 void printList(struct Node* head) { struct Node* curr = head; printf("List: "); while (curr != NULL) { printf("%d ", curr->data); curr = curr->next; } printf("\n"); } int main() { struct Node* head = NULL; // 插入结点并输出链表 head = insertAtEnd(head, 1); printList(head); head = insertAtEnd(head, 2); printList(head); head = insertAtBeginning(head, 3); printList(head); head = insertAtEnd(head, 4); printList(head); return 0; } ``` 输出结果: ``` List: 1 List: 1 2 List: 3 1 2 List: 3 1 2 4 ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值