链表,双链表--增删改查

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
	int data;
	struct Node *next;
} Node, *List;

// create List
bool createList(List *L) {
    // use malloc distribution memory
	*L = (Node *)malloc(sizeof(Node));
	if (*L == NULL) {
		return false;
	}
    // you can initial value of head node 
	// not recommended 
	//there set the value of node will hinder top insert ,need change node locate
	
    //(*L)->data = 0;

    //next pointer set to NULL
	(*L)->next = NULL;

	return true;
}

// print List
void printList(List L) {
	printf("new print\n");
	if (L == NULL) {
		printf("List is null\n");
		return;
	}
	//because top Node 
	Node *p = L->next; 
	while (p != NULL) {
		printf("List’ value is:%d\n", p->data);
		p = p->next;
	}
}

// top Insert List
bool topInsertList(List L, int addnum) {
	if (L == NULL) {
		return false;
	}
	Node *p = L;
	Node *add = (Node *)malloc(sizeof(Node)); 
	add->data = addnum;
	add->next = p->next;
    L->next=add;
	return true;
}

// tail Insert List
bool tailInsertList(List L, int addnum) {
	if (L == NULL) {
		return false;
	}
	Node *p = L;
	while (p->next != NULL) { 
		p = p->next;
	}
	Node *add = (Node *)malloc(sizeof(Node)); 
	if (add == NULL) {
		return false;
	}
	add->data = addnum;
	add->next = NULL;
	p->next = add; 
	return true;
}

//free the List
void freeList(List L){
	// free the memory of List
	Node *current = L;
	Node *next;
	while (current != NULL) {
		next = current->next;
		free(current);
		current = next;
	}
}

//deteleNodebyindex
bool deteleNode(List L,int index){
	if(L==nullptr||L->next==NULL)
		return false;
	Node *now_p=L->next;
	Node *prior=L;
	int i=1;
	while(now_p->next!=NULL&&i<=index)
	{
		prior=now_p;
		now_p=now_p->next;
		i++;
	}
	if(index>i)
		return false;
	prior->next=now_p->next;
	return true;
}

//middle insert
//can prior inster and next inster,yourself modify
bool middleInsert(List L,int index,int addmun){
	if(L==nullptr||L->next==NULL)
		return false;
	Node *now_p=L->next;
	Node *prior=L;
	Node *p=(Node*)malloc(sizeof(Node));
	p->data=addmun;
	int i=1;
	while(now_p->next!=NULL&&i<=index)
	{
		prior=now_p;
		now_p=now_p->next;
		i++;
	}
	if(index>i)
		return false;
	p->next=now_p;
	prior->next=p;
	return true;
}

int main() {
	List L;
	// createList
	if (createList(&L)) {
		printf("List created successfully.\n");
	} else {
		printf("Failed to create list.\n");
	}
	// print List
	printList(L);
	
	
	
	// topInsertList
	if (topInsertList(L, 5)&&topInsertList(L, 6)) {
		printf("List insert successfully.\n");
	} else {
		printf("Failed to insert list.\n");
	}
	// print List
	printList(L);
	
	
	
	// tailInsertList
	if (tailInsertList(L, 1)) {
		printf("List insert successfully.\n");
	} else {
		printf("Failed to insert list.\n");
	}
	// print List
	printList(L);
	
	
	//deteleNode
	deteleNode(L,2);
	// print List
	printList(L);
	
		
	// middleInsert
	middleInsert(L,1,899);
	// print List
	printList(L);
	
	
	//free list
	freeList(L);
	// print List
	L=NULL;
	printList(L);
	
	return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

typedef struct Node {
	int data;
	struct Node *prev;
	struct Node *next;
} Node, *DList;

// create DList
bool createDList(DList *DL) {
	*DL = (Node *)malloc(sizeof(Node));
	if (*DL == NULL) {
		return false;
	}
	(*DL)->data = 0; // head node data can be set to 0 or any other value
	(*DL)->prev = NULL;
	(*DL)->next = NULL;
	return true;
}

// print DList
void printDList(DList DL) {
	printf("DList print\n");
	if (DL == NULL) {
		printf("DList is null\n");
		return;
	}
	Node *p = DL->next;
	while (p != NULL) {
		printf("DList’ value is:%d\n", p->data);
		p = p->next;
	}
}

// top Insert DList
bool topInsertDList(DList DL, int addnum) {
	if (DL == NULL) {
		return false;
	}
	Node *add = (Node *)malloc(sizeof(Node));
	if (add == NULL) {
		return false;
	}
	add->data = addnum;
	add->next = DL->next;
	add->prev = DL;
	if (DL->next != NULL) {
		DL->next->prev = add;
	}
	DL->next = add;
	return true;
}

// tail Insert DList
bool tailInsertDList(DList DL, int addnum) {
	if (DL == NULL) {
		return false;
	}
	Node *p = DL;
	while (p->next != NULL) {
		p = p->next;
	}
	Node *add = (Node *)malloc(sizeof(Node));
	if (add == NULL) {
		return false;
	}
	add->data = addnum;
	add->next = NULL;
	add->prev = p;
	p->next = add;
	return true;
}

// free the DList
void freeDList(DList DL) {
	Node *current = DL->next;
	while (current != NULL) {
		Node *next = current->next;
		free(current);
		current = next;
	}
	free(DL); // Free the head node
}

// delete Node by index
bool deleteNode(DList DL, int index) {
	if (DL == NULL || DL->next == NULL) {
		return false;
	}
	Node *p = DL->next;
	int i = 1;
	while (p != NULL && i < index) {
		p = p->next;
		i++;
	}
	if (p == NULL || i < index) {
		return false;
	}
	if (p->next != NULL) {
		p->next->prev = p->prev;
	}
	p->prev->next = p->next;
	free(p);
	return true;
}

// middle insert
bool middleInsert(DList DL, int index, int addnum) {
	if (DL == NULL || DL->next == NULL) {
		return false;
	}
	Node *p = DL->next;
	int i = 1;
	while (p != NULL && i < index) {
		p = p->next;
		i++;
	}
	if (p == NULL || i < index) {
		return false;
	}
	Node *add = (Node *)malloc(sizeof(Node));
	if (add == NULL) {
		return false;
	}
	add->data = addnum;
	add->next = p;
	add->prev = p->prev;
	p->prev->next = add;
	p->prev = add;
	return true;
}

int main() {
	DList DL;
	// create DList
	if (createDList(&DL)) {
		printf("DList created successfully.\n");
	} else {
		printf("Failed to create DList.\n");
	}
	// print DList
	printDList(DL);
	
	// top Insert DList
	if (topInsertDList(DL, 5) && topInsertDList(DL, 6)) {
		printf("DList insert successfully.\n");
	} else {
		printf("Failed to insert DList.\n");
	}
	// print DList
	printDList(DL);
	
	// tail Insert DList
	if (tailInsertDList(DL, 1)) {
		printf("DList insert successfully.\n");
	} else {
		printf("Failed to insert DList.\n");
	}
	// print DList
	printDList(DL);
	
	// delete Node
	if (deleteNode(DL, 2)) {
		printf("DList node deleted successfully.\n");
	} else {
		printf("Failed to delete DList node.\n");
	}
	// print DList
	printDList(DL);
	
	// middle Insert
	if (middleInsert(DL, 1, 899)) {
		printf("DList node inserted successfully.\n");
	} else {
		printf("Failed to insert DList node.\n");
	}
	// print DList
	printDList(DL);
	
	// free DList
	freeDList(DL);
	// print DList
	DL = NULL;
	printDList(DL);
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值