之前学习链表的时候,用c++实现似乎都没有什么问题,但我换用c语言实现的时候,遇到了许多问题,尤其是segmentation fault,于是我花了一些时间,终于完成了c语言版本的链表,包括,单链表,循环链表,双链表,双循环链表;
首先明确一个概念:
头指针是链表的必需元素,链表可以没有头结点但不能没有头指针;
头结点的数据域可以用来存放链表长度;
没有头结点就是:数据从第一个结点开始存储,返回指向第一个结点的指针作为头指针;
有头结点就是:数据从第二个结点开始存储,返回指向第一个结点的指针作为头指针;
单链表
/*
头指针是链表的必需元素,链表可以没有头结点但不能没有头指针;
头结点的数据域可以用来存放链表长度;
没有头结点就是:数据从第一个结点开始存储,返回指向第一个结点的指针作为头指针;
有头结点就是:数据从第二个结点开始存储,返回指向第一个结点的指针作为头指针;
*/
//C语言版本的链表实现
#include <stdio.h>
#include <stdlib.h>
//定义结点结构体
typedef struct listNode
{
int data;
struct listNode *next;
} ListNode;
//初始化结点
ListNode *createNode(int num)
{
ListNode *node = (ListNode *)malloc(sizeof(ListNode));
node->data = num; //指针在使用前要分配空间,
node->next = NULL;
return node;
}
//在链表尾增加元素
void ListAdd(ListNode *head, ListNode *B)
{
ListNode *tempA = (ListNode *)malloc(sizeof(ListNode));
tempA = head;
while (tempA)
{
if (tempA->next == NULL)
{
tempA->next = B;
tempA = B;
}
tempA = tempA->next;
}
}
//创建链表,长度指定,值为1,2,3...
ListNode *createList(int len)
{
ListNode *head = createNode(len); //头结点,data存储链表长度
for (int i = 0; i < len; ++i)
{
ListNode *temp = createNode(i + 1);
ListAdd(head, temp);
}
return head;
}
//创建循环链表
ListNode *createCycleList(int len)
{
ListNode *head = createNode(len); //头结点,data存储链表长度
for (int i = 0; i < len; ++i)
{
ListNode *temp = createNode(i + 1);
ListAdd(head, temp);
if (i == len - 1)
temp->next = head->next;
}
return head;
}
//打印链表
void printList(ListNode *head)
{
ListNode *temp = head->next;
while (temp)
{
printf("%d\n", temp->data);
temp = temp->next;
}
}
//打印循环链表
void printCycleList(ListNode *head)
{
int ListLen=head->data;
ListNode *temp = head->next;
while (ListLen--)
{
printf("%d\n", temp->data);
temp = temp->next;
}
}
//在任意位置插入
void InsertData(ListNode *head, int data, int position)
{
ListNode *temp = head;
ListNode *tempData = createNode(data);
while (--position)
temp = temp->next;
tempData->next = temp->next;
temp->next = tempData;
head->data = (head->data) + 1;
}
//在任意位置删除
void DeleteData(ListNode *head, int position)
{
ListNode *temp = head;
while (--position)
temp = temp->next;
temp->next = temp->next->next;
head->data = head->data - 1;
}
int main()
{
printf("list_c test\n");
// ListNode* A=createNode(1);
// printf("%d",A->data);
// ListNode* head=createList(6);
// printList(head);
ListNode *head = createCycleList(6);
InsertData(head, 9, 3);
printCycleList(head);
printf("\n");
DeleteData(head, 3);
printCycleList(head);
return 0;
}
双链表
#include <stdio.h>
#include <stdlib.h>
//定义双链表结点结构体
typedef struct dullistNode
{
int data;
struct dullistNode *next;
struct dullistNode *prior;
} DulListNode;
//初始化双链表结点
DulListNode *createDulNode(int num)
{
DulListNode *node = (DulListNode *)malloc(sizeof(DulListNode));
node->data = num; //指针在使用前要分配空间,
node->next = NULL;
node->prior = NULL;
return node;
}
//在双链表尾增加元素
void DulListAdd(DulListNode *head, DulListNode *B)
{
DulListNode *tempA = (DulListNode *)malloc(sizeof(DulListNode));
tempA = head;
while (tempA)
{
if (tempA->next == NULL)
{
tempA->next = B;
B->prior = tempA;
tempA = B;
}
tempA = tempA->next;
}
}
//创建双链表,长度指定,值为1,2,3...
DulListNode *createDulList(int len)
{
DulListNode *head = createDulNode(len); //头结点,data存储链表长度
for (int i = 0; i < len; ++i)
{
DulListNode *temp = createDulNode(i + 1);
DulListAdd(head, temp);
}
return head;
}
//创建双循环链表,长度指定,值为1,2,3...
DulListNode *createDulCycleList(int len)
{
DulListNode *head = createDulNode(len); //头结点,data存储链表长度
for (int i = 0; i < len; ++i)
{
DulListNode *temp = createDulNode(i + 1);
DulListAdd(head, temp);
if(i==len-1)
{
temp->next=head->next;
head->next->prior=temp;
}
}
return head;
}
//打印双链表
void printDulList(DulListNode *head)
{
DulListNode *temp = head->next;
while (temp)
{
printf("%d\n", temp->data);
temp = temp->next;
}
}
//打印双循环链表
void printDulCycleList(DulListNode *head)
{
int len=head->data;
DulListNode *temp = head->next;
while (len--)
{
printf("%d\n", temp->data);
temp = temp->next;
}
}
//在任意位置插入
void InsertData(DulListNode *head, int data, int position)
{
DulListNode *temp = head;
DulListNode *tempData = createDulNode(data);
while (--position)
temp = temp->next;
tempData->next = temp->next;
temp->next->prior = tempData;
temp->next = tempData;
tempData->prior = temp;
head->data = (head->data) + 1;
}
//在任意位置删除
void DeleteData(DulListNode *head, int position)
{
DulListNode *temp = head;
while (--position)
temp = temp->next;
temp->next->next->prior = temp;
temp->next = temp->next->next;
head->data = (head->data) - 1;
}
int main()
{
printf("------DulList------\n");
printf("------Length------\n");
int num;
scanf("%d",&num);
DulListNode *head = createDulList(num);
printf("------print------\n");
printDulList(head);
printf("------insert------\n");
InsertData(head, 88, 5);
printf("------print------\n");
printDulList(head);
printf("------delete------\n");
DeleteData(head, 1);
printf("------print------\n");
printDulList(head);
printf("------length------\n");
scanf("%d",&num);
DulListNode* Dulhead =createDulCycleList(num);
printf("------print------\n");
printDulCycleList(Dulhead);
return 0;
}
就不放运行结果图了,要次饭了,嘻嘻嘻~~