手写了一遍链表的代码,写完神清气爽再也不害怕写链表代码了hhhhh
链表的具体代码实现,链表的功能有创建、插入、删除、打印、查询。
写链表代码一定要注意:1. 使用前要申请空间 2. 删除时要释放空间 3. 注意指针的指向
c语言结构体实现
#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct linklist
{
int value;
struct linklist *next;
}LinkNode;
// 创建空链表
LinkNode* createLink()
{
LinkNode *head = (LinkNode*)malloc(sizeof(LinkNode));
head->next = NULL;
return head;
}
// 创建链表结点
LinkNode* createNode(int value)
{
LinkNode *node = (LinkNode*)malloc(sizeof(LinkNode));
node->value = value;
node->next = NULL;
return node;
}
// 头插法形成链表
LinkNode* insertInHead(int array[], int len)
{
LinkNode *head, *node;
for(int i=0; i<len; i++)
{
if(i == 0){
head = createNode(array[i]);
}
else{
node = createNode(array[i]);
node->next = head;
head = node;
}
}
return head;
}
// 尾插法形成链表
LinkNode* insertInTail(int array[], int len)
{
LinkNode *prev, *head;
for(int i=0; i<len; i++)
{
if(i == 0){
head = createNode(array[i]);
prev = head;
}
else{
LinkNode *node = createNode(array[i]);
prev->next = node;
prev = node;
}
}
return head;
}
// 指定位置插入结点
LinkNode* insertNode(LinkNode *head, int pos, int value)
{
int i=1;
LinkNode *curnode = head;
LinkNode *newnode = createNode(value);
if(pos == 1){
newnode->next = head;
head = newnode;
return head;
}
while(curnode != NULL)
{
if(i == pos-1)
{
if(curnode->next == NULL){
curnode->next = newnode;
}
else{
newnode->next = curnode->next;
curnode->next = newnode;
}
return head;
}
curnode = curnode->next;
i++;
}
printf("insert node error ! \n");
return NULL;
}
// 指定位置删除结点
LinkNode* deleteNode(LinkNode *head, int pos)
{
int i=1;
LinkNode *curnode = head;
if(pos == 1){
head = head->next;
free(curnode);
return head;
}
while(curnode != NULL)
{
if(i == pos-1)
{
if(curnode->next == NULL)
{
printf("pos is not in link \n");
}else if(curnode->next->next == NULL){
LinkNode *pointer = curnode->next;
curnode->next = NULL;
free(pointer);
}else{
LinkNode *pointer = curnode->next;
curnode->next = pointer->next;
free(pointer);
}
return head;
}
curnode = curnode->next;
i++;
}
printf("delete node error !\n");
return NULL;
}
// 判断链表是否为空
int isEmpty(LinkNode *head)
{
if(head->next == NULL){
return 1;
}
else{
return 0;
}
}
// 清空链表
void clearLinkList(LinkNode *head)
{
LinkNode *pointer;
while(head->next != NULL)
{
pointer = head->next;
head->next = pointer->next;
free(pointer);
}
free(head);
}
// 求链表长度
int getLinkListLength(LinkNode *head)
{
int len = 0;
LinkNode *pointer = head;
while(pointer != NULL){
pointer = pointer->next;
len++;
}
return len;
}
// 显示链表
void printLinkList(LinkNode *head)
{
LinkNode *pointer = head;
while(pointer != NULL)
{
printf("%d ", pointer->value);
pointer = pointer->next;
}
printf("\n");
}
int main()
{
LinkNode *head;
int array[5], n, len;
scanf("%d", &n);
for(int i=0; i<n; i++)
{
scanf("%d", &array[i]);
}
head = insertInHead(array, n);
printLinkList(head);
len = getLinkListLength(head);
printf("Link length: %d \n", len);
clearLinkList(head);
head = insertInTail(array, n);
printLinkList(head);
clearLinkList(head);
int result = isEmpty(head);
printf("isempty: %d \n", result);
printf("%d\n", head->value);
head = insertInTail(array, n);
head = insertNode(head, 4, 9);
printLinkList(head);
head = deleteNode(head, 5);
printLinkList(head);
return 0;
}
结果输出:
5
1 2 3 4 5
5 4 3 2 1 //头插法
Link length: 5 //求长度
1 2 3 4 5 //尾插法
isempty: 1 //判断是否为空
1
1 2 3 9 4 5 // 指定位置插入
1 2 3 9 5 // 指定位置删除
Python类实现
这个比较简单,看文献吧。时间不够不写了,。。。
参考文献: