//单链表基本操作 1 #include <stdio.h>2 3 #include <stdlib.h>4 5 6 typedef struct _NODE7{8int data;9struct _NODE *pNext;10 }NODE,*PNODE;11 12 PNODE Create_List(void)13{14int len = 0;15int data,i = 0;16 PNODE pHead = NULL;17 pHead = (PNODE)malloc(sizeof(NODE));18 PNODE pTail = pHead;19 pTail->pNext = NULL;20 21if(pHead == NULL)22 {23 printf("内存分配失败!\r\n");24 }...
单循环链表和单链表的唯一区别在于单循环链表的最后一个节点的指针域指向第一个节点,使得整个链表形成一个环.C实现代码如下:#include<stdio.h>typedef struct node
{int data;struct node *next;
}Node;//链表的初始化
Node* InitList(int number)
{int i;Node *pHead=(Node *)malloc(sizeof(Node));Node *TempHead=pHead;Node *Head=pHead;int data;for(i=0;i<number;i++){pHead=(Node *)malloc(sizeof(Node));printf("Please inp...
我们都知道,单向链表最后指向为NULL,也就是为空,那单向循环链表就是不指向为NULL了,指向头节点,所以下面这个程序运行结果就是,你将会看到遍历链表的时候就是一个死循环,因为它不指向为NULL,也是周而复始的执行。串成了一个环型。 #include <stdio.h>
#include <stdlib.h>
typedef struct node
{char name[20];struct node *link;
}student;student * creat(int n) /*建立单链表的函数,形参n为人数*/
{/* *h保存表头...
/* 以下双向链表操作纯本人原创,转载请注明来源 *//***********************************************************************************************************************File Name : 双向链表.cDate Created : 2019-10-22Author : 大竹竿Description : 实现双向链表各类操作****************************************************************************************************************...
概念快慢指针判断链表是否有环Bool ifLoopOfList(List head){List quick=NULL;List slow=NULL;quick=slow=head;do{quick=quick->next->next;slow=slow->next;printf("quick->%d\tslow->%d\n",quick->data,slow->data);if(quick==slow){return 1;}}while(quick);return 0;}
工程文件
#include <stdio.h>
#include <stdlib.h>
typedef struct node{int data;struct node *next;
}Node,*List;
typedef int Bool;
List createLoopList...
C语言
C语言和Python的方法是一样的,所以就C语言了,效率上快很多。/*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/
#define NULL ((void *)0)struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){if (l1 == NULL)return l2;if (l2 == NULL)return l1;if (l1->val > l2->val){l2->next=mergeTwoLists(l1,l2->next);return l2;}else{l1->nex...
//单链表基本操作 1 #include <stdio.h>2 3 #include <stdlib.h>4 5 6 typedef struct _NODE7 {8 int data;9 struct _NODE *pNext;10 }NODE,*PNODE;11 12 PNODE Create_List(void)13 {14 int len = 0;15 int data,i = 0;16 PNODE pHead = NULL;17 pHead = (PNODE)malloc(sizeof(NODE));18 PNODE pTail = pHead;19 pTail->pNext = NULL;20 21 if(pHead == NULL)22 {23 printf(...
注:空链表状态头节点的前驱、后继节点都指向自己
代码部分
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>typedef struct node {int data;struct node *prev;struct node *next;
}NODE, *PNODE;void init(PNODE *);
bool is_empty(PNODE);
void traverse(PNODE);
bool head_add(PNODE, int);
bool rear_add(PNODE, int);
int len(PNODE);
bool insert(PNODE, int, int);
bool delete(PNODE, int, int *);
bool cle...
代码
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/** 使用头节点*/ typedef struct node {int data;struct node *prev;struct node *next;
}NODE, *PNODE;// 初始化
void init(PNODE *);
// 是否为空
bool is_empty(PNODE);
// 遍历
void traverse(PNODE);
// 头插法追加节点
bool head_add(PNODE *, int);
// 尾插法追加节点
bool rear_add(PNODE *, int);
// 求长度(不算头节点)
int len(PNODE);
// 插入节点...