动态链表的增删改查功能

#include <stdio.h>
#include <stdlib.h>
/*typedef struct student{
 int num;
 char name[20]; 
} ElemType; */
typedef int ElemType;
struct Node{
 ElemType data;
 struct Node * next;
};
void insert(struct Node *p ,int site,ElemType value);
void dele(struct Node *p,int site);
void output(struct Node *p);
void create(struct Node *p,int t);
int find(struct Node *p,ElemType value);
void update(struct Node *p,int site,ElemType value);
int main()
{
 struct Node *head,*q;
 int i,t,site,value;
 printf("请输入元素的总个数:");
 scanf("%d",&t); 
 head=(struct Node *)malloc(sizeof(struct Node));


 printf("请输入%d个元素",t);
 create(head,t);
 printf("数组中的内容为:") ; 
 output(head);


printf("请输入你要插入元素的位置和你要插入的数为:");
 scanf("%d %d",&site,&value); 
 insert(head,site,value);
 printf("数组中的内容为:") ;
 output(head);

printf("请输入你要删除元素的位置为:");
 scanf("%d",&site); 
 dele(head,site);
 printf("数组中的内容为:") ;
 output(head);


printf("请输入你要更改元素的位置和更改后的数为:");
 scanf("%d %d",&site,&value);
 update(head,site,value); 
 printf("数组中的内容为:") ;
 output(head);


printf("请输入你要查找的元素为:"); 
 scanf("%d",&value); 
 if(value!=NULL)
 {
  printf("你要查找元素的下标为:%d\n",find(head,value)); 
 }
 else{
  printf("你输入的元素不在查找的范围之内");
 }
} 



void create(struct Node *p,int t)//创建链表 
{
 struct Node *n;
 int i;
 for(i=0;i<t;i++)
 {
  n=(struct Node *)malloc(sizeof(struct Node));
     scanf("%d",&n->data);
     n->next=NULL;
     p->next=n;
     p=n;  
 } 
} 



void insert(struct Node *p ,int site,ElemType value)//增 
{
 int i;
 struct Node *n=(struct Node *)malloc(sizeof(struct Node));
 n->data=value;
 for(i=1;i<site;i++)//将p定位到插入位置site前面的一个结点 
 {
  p=p->next;
 }
 n->next=p->next;
 p->next=n;
}



void dele(struct Node *p,int site)//删 
{
 int i;
 for(i=1;i<site;i++)
 {
  p=p->next;
 }
 p->next=p->next->next;
}




void update(struct Node *p,int site,ElemType value)//改
{
 int i;
 for(i=1;i<=site;i++)
    p=p->next;
    p->data=value; 
}




int find(struct Node *p,ElemType value)//查
{
 int k=1;
 for(p=p->next;p!=NULL;p=p->next,k++)
 {
  if(p->data==value)
    return k-1; 
 }
 return NULL;
}




void output(struct Node *p)//输出
{
 for(p=p->next;p!=NULL;p=p->next)
 {
  printf("%d ",p->data);
 }
 printf("\n");
}

输出显示:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 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; // 头指针 Node* tail = NULL; // 尾指针 for (int i = 0; i < n; i++) { Node* newNode = (Node*)malloc(sizeof(Node)); // 创建一个新节点 if (newNode == NULL) { printf("Error: memory allocation failed!\n"); exit(1); } printf("Please input the data of node %d: ", i + 1); scanf("%d", &newNode->data); newNode->next = NULL; if (i == 0) { // 第一个节点 head = newNode; tail = newNode; } else { // 后续节点 tail->next = newNode; tail = newNode; } } return head; } /* 打印链表 */ void printList(Node* head) { printf("List: "); for (Node* p = head; p != NULL; p = p->next) { printf("%d ", p->data); } printf("\n"); } /* 向链表中插入节点 */ Node* insertNode(Node* head, int index, int data) { if (index < 0) { printf("Error: invalid index!\n"); return head; } Node* newNode = (Node*)malloc(sizeof(Node)); // 创建一个新节点 if (newNode == NULL) { printf("Error: memory allocation failed!\n"); exit(1); } newNode->data = data; newNode->next = NULL; if (index == 0) { // 在链表头部插入节点 newNode->next = head; return newNode; } Node* p = head; for (int i = 0; i < index - 1; i++) { // 找到待插入位置的前一个节点 if (p == NULL) { printf("Error: invalid index!\n"); return head; } p = p->next; } newNode->next = p->next; p->next = newNode; return head; } /* 删除链表中的节点 */ Node* deleteNode(Node* head, int index) { if (index < 0) { printf("Error: invalid index!\n"); return head; } if (index == 0) { // 删除链表头部节点 Node* p = head; head = head->next; free(p); return head; } Node* p = head; for (int i = 0; i < index - 1; i++) { // 找到待删除节点的前一个节点 if (p == NULL || p->next == NULL) { printf("Error: invalid index!\n"); return head; } p = p->next; } Node* q = p->next; p->next = q->next; free(q); return head; } /* 修链表中的节点 */ void modifyNode(Node* head, int index, int data) { if (index < 0) { printf("Error: invalid index!\n"); return; } Node* p = head; for (int i = 0; i < index; i++) { // 找到待修节点 if (p == NULL) { printf("Error: invalid index!\n"); return; } p = p->next; } p->data = data; } /* 链表中的节点 */ int searchNode(Node* head, int data) { int index = 0; for (Node* p = head; p != NULL; p = p->next) { if (p->data == data) { return index; } index++; } return -1; } /* 主函数 */ int main() { int n; printf("Please input the length of the list: "); scanf("%d", &n); Node* head = createList(n); printList(head); // 插入节点 head = insertNode(head, 2, 100); printList(head); // 删除节点 head = deleteNode(head, 4); printList(head); // 修节点 modifyNode(head, 1, 200); printList(head); // 找节点 int index = searchNode(head, 200); if (index != -1) { printf("Node 200 is found at index %d.\n", index); } else { printf("Node 200 is not found in the list.\n"); } return 0; } ``` 这个程序实现了链表的创建、打印、插入、删除、修功能。在程序中,我们定义了一个链表节点结构体,包含一个数据域和一个指针域,指向下一个节点。通过遍历链表,我们可以依次访问每个节点的数据域。在主函数中,我们首先输入链表的长度,然后调用 `createList()` 函数创建链表,并调用 `printList()` 函数打印链表。接着,我们演示了如何向链表中插入节点、删除节点、修节点和找节点。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱睡觉的小馨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值