双链表基本操作
编译环境:VS !!
对应操作已写注解!!
#include <stdio.h>
#include <stdlib.h>
typedef char datatype; //定义数据域类型结构
typedef struct node
{
datatype data;
struct node *prev;
struct node *next;
} listnode;
//创建新结点
listnode *buylsitnode(datatype d)
{
listnode *newnode = (listnode *)malloc(sizeof(listnode));
if (newnode == NULL)
{
printf("内存空间分配失败");
exit(1);
}
newnode->data = d;
newnode->next = NULL;
newnode->prev = NULL;
return newnode;
}
//获取表长度
int lengthlist(listnode *head)
{
listnode *cur = head;
int j = 0;
while (cur != NULL)
{
cur = cur->next;
j++;
}
return j;
}
//表头插入
void insertelematstart(listnode **head, datatype d)
{
listnode *newnode = buylsitnode(d);
if (*head == NULL)
{
*head = newnode;
}
else
{
listnode *cur = *head;
newnode->next = cur;
cur->prev = newnode;
cur = newnode;
// printf("%c\n", cur->next->data);
}
}
//表尾插入
void insertelematend(listnode **head, datatype d)
{
listnode *newnode = buylsitnode(d);
if (*head == NULL)
{
*head = newnode;
}
else
{
listnode *cur = *head;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = newnode;
newnode->prev = cur;
}
}
//指定位置插入
void insertelem(listnode **head, datatype d, int p)
{
listnode *newnode = buylsitnode(d);
int length = lengthlist(*head);
if (*head == NULL)
{
*head = newnode;
}
if (p > length || p < 0)
{
printf("插入失败,超出索引范围\n");
exit(1);
}
listnode *cur = *head;
if (p == 0)
{
newnode->next = cur;
cur->prev = newnode;
cur = newnode;
printf("插入成功\n");
}
else if (p == length)
{
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = newnode;
newnode->prev = cur;
printf("插入成功\n");
}
else
{
int j = 1;
while (j != p)
{
cur = cur->next;
j++;
}
cur->next->prev = newnode;
newnode->next = cur->next;
cur->next = newnode;
newnode->prev = cur;
printf("插入成功\n");
}
}
//删除元素
void delectelem(listnode **head, int p)
{
if (*head == NULL)
{
printf("不为空,删除失败");
exit(1);
}
int length = lengthlist(*head);
length--;
if (p > length || p < 0)
{
printf("删除失败,超出索引范围\n");
exit(1);
}
listnode *cur = *head;
if (p == 0)
{
cur->next->prev = cur->prev;
cur->prev->next = cur->next;
free(cur);
printf("删除成功\n");
}
else if (p == length)
{
while (cur->next != NULL)
{
cur = cur->next;
}
cur->prev->next = NULL;
printf("删除成功\n");
}
else
{
int j = 0;
while (j != p)
{
cur = cur->next;
j++;
}
cur->next->prev = cur->prev;
cur->prev->next = cur->next;
printf("删除成功\n");
}
}
//修改元素
void changeelem(listnode **head, int p, datatype d)
{
if (*head == NULL)
{
printf("不为空,修改失败\n");
exit(1);
}
int length = lengthlist(*head);
length--;
if (p > length || p < 0)
{
printf("修改失败,超出索引范围\n");
exit(1);
}
listnode *cur = *head;
int j = 0;
while (j != p)
{
cur = cur->next;
j++;
}
cur->data = d;
printf("修改成功\n");
}
//遍历打印
void print(listnode *head)
{
listnode *cur = head;
while (cur != NULL)
{
printf("%0.4c", cur->data);
cur = cur->next;
}
printf("\n");
}
//测试
int main(int argc, char const *argv[])
{
listnode *head = NULL;
//表头插入操作测试
insertelematstart(&head, 'c');
print(head);
//表尾插入操作测试
insertelematend(&head, 'd');
insertelematend(&head, 'e');
insertelematend(&head, 'f');
insertelematend(&head, 'g');
print(head);
//指定位置插入操作测试
insertelem(&head, 'p', 1);
print(head);
//删除操作测试
delectelem(&head, 2);
print(head);
//修改操作测试
changeelem(&head, 0, 'o');
print(head);
//长度打印
printf("%d\n", lengthlist(head));
return 0;
}
运行结果
该文章作为学习记录,水平有限。如有差错,请多多指点!!🐵