元素相互依赖,串联而成(除了火车头,每节车厢只连前一节车厢)
一个链表只有一个表头(火车只有一个火车头)
元素不能随机访问
创建链表头
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *next;
}Node,*LinkedList;
void clear(LinkedList head){
Node *current_node = head;
while(current_node != NULL){
Node *delete_node = current_node;
current_node = current_node->next;
free(delete_node);
}
}
int main(){
LinkedList linkedlist = NULL;
clear(linkedlist);
}
链表插入操作
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *next
}Node,*LinkedList;
LinkedList insert(LinkedList head,Node *node,int index){
if(head == NULL){
if(index != 0){
return head;
}
head = node;
return head;
}
if(index == 0){
node->next = head;
head = node;
return head;
}
Node *current_node = head;
int count = 0;
while(current_node->next != NULL && count < index - 1){
current_node = current_node->next;
count++;
}
if(count == index + 1){
node->next = current_node->next;
current_node->next = node;
}
return head;
}
int main(){
LinkedList linkedlist = NULL;
for(int i = 0; i <= 10; i++){
Node *node = (Node *)malloc(sizeof(Node));
node->data = i;
node->next = NULL;
linkedlist = insert(linkedlist,node,i-1);
}
}
链表遍历
#include <stdio.h>
#include <stdlib.h>
void output(LinkedList head){
if(head == NULL){
return head;
}
Node *current_node = head;
while(current_node != NULL){
printf("%d ",current_node->data);
current_node = current_node->next;
}
printf("\n");
}
链表反转
LinkedList reverse(LinkedList head){
if(head == NULL){
return head;
}
Node *next_node,*current_node;
current_node = head->next;
next_node = NULL;
while(current_node != NULL){
next_node = current_node->next;
current_node->next = head;
head = current_node;
current_node = next_node;
}
return head;
}
链表删除
LinkedList delete_node(Linkedlist head,int index){
if(head == NULL){
return head;
}
Node *current_node = head;
int count = 0;
if(index == 0){
head = head->next;
free(current_node);
return head;
}
while(current_node->next != NULL && count < index - 1){
current_node = current_node->next;
count++;
}
if(count == index - 1 && count < index - 1){
Node *delete_node = current_node->next;
current_node->next = delete_node->next;
free(delete_node);
}
return head;
}
约瑟夫环问题
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *next;
}Node, *LinkedList;
LinkedList insert(LinkedList head, Node *node, int index) {
if (head == NULL) {
if (index != 0) {
return head;
}
head = node;
head->next = head;
return head;
}
if (index == 0) {
node->next = head->next;
head->next = node;
return head;
}
Node *current_node = head->next;
int count = 0;
while (current_node != head && count < index - 1) {
current_node = current_node->next;
count++;
}
if (count == index - 1) {
node->next = current_node->next;
current_node->next = node;
}
if(node == head->next){
head = node;
}
return head;
}
int main() {
LinkedList linkedlist = NULL;
int n,m;
scanf("%d %d",&n,&m);
for(int i = 1; i <= n; i++){
Node *node = (Node *)malloc(sizeof(Node));
node->data = i;
node->next = NULL;
linkedlist = insert(linkedlist,node,i - 1);
}
return 0;
}