用c类封装实现链表初始化,头插、尾插、中间插、删除结点、删除链表、初始化链表获取链表长度,拷贝构造函数,析构函数
时间: 2023-08-17 07:05:24 浏览: 82
好的,下面是使用C语言进行封装实现链表的相关函数:
```c
#include <stdio.h>
#include <stdlib.h>
//定义链表节点结构体
typedef struct Node{
int data;
struct Node *next;
}Node;
//定义链表结构体
typedef struct List{
Node *head;
void (*initList)(struct List *);
void (*insertHead)(struct List *, int);
void (*insertTail)(struct List *, int);
void (*insertMid)(struct List *, int, int);
void (*deleteNode)(struct List *, int);
void (*deleteList)(struct List *);
int (*getListLength)(struct List *);
void (*copyList)(struct List *, struct List *);
void (*destroyList)(struct List *);
}List;
//链表初始化
void initList(List *list){
list->head = NULL;
}
//头插
void insertHead(List *list, int value){
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = list->head;
list->head = newNode;
}
//尾插
void insertTail(List *list, int value){
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if(list->head == NULL){
list->head = newNode;
}else{
Node *p = list->head;
while(p->next != NULL){
p = p->next;
}
p->next = newNode;
}
}
//中间插
void insertMid(List *list, int value, int pos){
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
if(pos == 1){
newNode->next = list->head;
list->head = newNode;
return;
}
Node *p = list->head;
for(int i=1; i<pos-1 && p!=NULL; i++){
p = p->next;
}
if(p == NULL){
printf("插入位置无效!\n");
return;
}
newNode->next = p->next;
p->next = newNode;
}
//删除节点
void deleteNode(List *list, int value){
Node *p = list->head;
Node *pre = NULL;
while(p != NULL && p->data != value){
pre = p;
p = p->next;
}
if(p == NULL){
printf("链表中不存在该节点!\n");
return;
}
if(pre == NULL){
list->head = p->next;
}else{
pre->next = p->next;
}
free(p);
}
//删除链表
void deleteList(List *list){
Node *p = list->head;
while(p != NULL){
Node *temp = p;
p = p->next;
free(temp);
}
list->head = NULL;
}
//获取链表长度
int getListLength(List *list){
int count = 0;
Node *p = list->head;
while(p != NULL){
count++;
p = p->next;
}
return count;
}
//拷贝构造函数
void copyList(List *newList, List *oldList){
Node *p = oldList->head;
while(p != NULL){
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = p->data;
newNode->next = NULL;
if(newList->head == NULL){
newList->head = newNode;
}else{
Node *q = newList->head;
while(q->next != NULL){
q = q->next;
}
q->next = newNode;
}
p = p->next;
}
}
//析构函数
void destroyList(List *list){
deleteList(list);
}
//测试
int main(){
List list;
list.initList = initList;
list.insertHead = insertHead;
list.insertTail = insertTail;
list.insertMid = insertMid;
list.deleteNode = deleteNode;
list.deleteList = deleteList;
list.getListLength = getListLength;
list.copyList = copyList;
list.destroyList = destroyList;
list.initList(&list);
list.insertHead(&list, 3);
list.insertHead(&list, 2);
list.insertHead(&list, 1);
list.insertTail(&list, 4);
list.insertTail(&list, 5);
list.insertMid(&list, 6, 4);
list.deleteNode(&list, 3);
printf("链表长度为:%d\n", list.getListLength(&list));
List newList;
newList.initList = initList;
newList.insertHead = insertHead;
newList.insertTail = insertTail;
newList.insertMid = insertMid;
newList.deleteNode = deleteNode;
newList.deleteList = deleteList;
newList.getListLength = getListLength;
newList.copyList = copyList;
newList.destroyList = destroyList;
newList.initList(&newList);
list.copyList(&newList, &list);
printf("拷贝后的链表长度为:%d\n", newList.getListLength(&newList));
list.destroyList(&list);
printf("链表已销毁!\n");
return 0;
}
```
阅读全文