用c语言写一个有增删查改操作的单链表
时间: 2024-06-01 12:10:26 浏览: 84
C语言数据结构单链表的增删查改
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *next;
};
//创建链表
struct node *createList()
{
struct node *head, *tail, *p;
head = (struct node *)malloc(sizeof(struct node));
head->next = NULL;
tail = head;
int n, x;
printf("请输入链表中元素的个数:");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printf("请输入第%d个元素:", i);
scanf("%d", &x);
p = (struct node *)malloc(sizeof(struct node));
p->data = x;
p->next = NULL;
tail->next = p;
tail = p;
}
return head;
}
//遍历链表
void traverseList(struct node *head)
{
struct node *p = head->next;
if (p == NULL) {
printf("链表为空!\n");
return
阅读全文