单链表的基本操作

单链表的基本操作

包括:初始化,插入,删除,逆置,求表长


  1. 定义链表的数据结构
struct node {
  int data;
  node *next;
};
  1. 初始化链表
node* create(node* head) {
	head = new node;
	head->next = NULL;
	return head;
}
  1. 链表的插入(头插法)
node* insert(node *head, int value) {
	//new node
	node *new_node = new node;
	new_node->data = value;
	
	//record head->next
	node *temp;
	temp->next = head->next;
	head->next = new_node;
	new_node->next = temp->next;
	return head;
}
  1. 链表的删除,删除第i个节点,(有一个空的头结点作为第0个)
//测试集,空指针,空链表,删除的位置是负数,删除的位置在链表中,删除的位置超过链表的长度
node* delete_by_position(node *head, int n) {

    //空指针
    if(!head) {
        cout<<"NULL point!"<<endl;
        return head;
    }
    
    //指针应该定位到第n-1个元素
    node *p = head;
    //已经数了多少个了,因为要删除第n个元素 ,所以应该数了n-1次,然后p指针指向第n-1个元素
    int count = 0;
    //move to position of n-1
    while(p->next && n-1>count) {
        p = p->next;
        count++;
    }
    if(n-1 != count || !p->next) {
        cout<<"node not found!"<<endl;
        return head;
    }
    
    //if found, delete that node
    node* temp = p->next;
    p->next = temp->next;
    delete temp;
    return head;
}
  1. 链表的删除,删除节点值为value的第一个元素
//测试集,空指针,空链表,删除的元素在链表中,删除的元素不在链表中
node* delete_by_value(node* head, int value) {
    if (!head) {
        cout<<"node not found!"<<endl;
        return head;
    }
    bool flag =false;
    node *p = head;
    while (p->next) {
        if (p->next->data == value) {
            flag = true;
            break;
        }
        p = p->next;
    }
    if(flag) {
        node* temp = p->next;
        p->next = temp->next;
        free temp;
    } else {
        cout<<"node not found!"<<endl;
    } 
    return head;
}
  1. 遍历链表
void display (node *head) {
    if(!head) {
        return ;
    }
    node* p = head;
    while(p->next) {
        cout<<p->next->data<<endl;
        p = p->next;
    }
}
  1. 求链表的长度
int getLength(node *head) {
    if (!head) {
        return -1;
    }
    int count = 0;
    node *p = head;
    while(p->next) {
        count++;
        p = p->next;
    }
    return count;
}
  1. 链表逆置 这里采用原地逆置,通过头指针和额外定义的两个指针不断移动更改链接来实现逆置
    在这里插入图片描述
node* reverse(node *head) {
    //NULL point
    if(!head) {
        cout<<"it is NULL point!"<<endl;
        return head;
    }
    node *a,*p;
    //initialize
    a = head->next;
    while(a->next) {
        p = a->next;
        a->next = p->next;
        p->next = head->next;
        head->next = p;
    }
    return head;
}

主函数
int main () {
	//new head
	node* head;
	
	//创建头结点
	head = create(head);
	
	//输入n个元素,并以头插的形式插入到链表
	int n;
	cin>>n;
	while(n-->0) {
		int value;
		cin>>value;
		head = insert(head,value);
	}
	
	//遍历
	display(head);
	
	//删除第n个元素 注意n取值
	cin>>n;
	head = delete_by_position(head,n);
	
	//删除元素值为value的第一个节点
	int value;
	cin>>value;
	head = delete_by_value(head,value);
	
	//链表逆置
	head = reverse(head);
	display(head);
	
	//求表长
	cout<<getLength(head)<<endl;
	display(head);
	return 0;
} 
### 单链表基本操作详解 #### 结构定义 单链表由一系列节点组成,每个节点包含数据域和指向下一个节点的指针域。典型的结构体定义如下: ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; } LinkList, LNode; #define OK 1 ``` --- #### 创建单链表(头插法) 动态构建带头结点的单链表,便于统一管理。 ```c LinkList* CreateList() { LinkList* head = (LinkList*)malloc(sizeof(LNode)); head->next = NULL; head->data = 0; // 头结点data保存链表长度 return head; } ``` --- #### 遍历操作 从首元结点开始逐个访问并打印数据元素[^1]。 ```c int DisplayList(LinkList* L) { LNode* p = L->next; // 工作指针初始化 while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); return OK; } ``` --- #### 查找操作 按位查找第 `i` 个元素的内容或按值查找第一个匹配的位[^1]。 ##### 按位查找 ```c LNode* GetElem(LinkList* L, int i) { if (i < 1) return NULL; LNode* p = L->next; int j = 1; while (p && j < i) { p = p->next; j++; } return p; // 返回第i个结点的指针 } ``` ##### 按值查找 ```c int LocateElem(LinkList* L, int value) { LNode* p = L->next; int index = 1; while (p) { if (p->data == value) return index; p = p->next; index++; } return -1; // 未找到返回-1 } ``` --- #### 插入操作 在第 `i` 个位插入新元素 `value`,需先定位到第 `i-1` 个结点[^7]。 ```c int InsertList(LinkList* L, int i, int value) { if (i < 1) return 0; LNode* prev = L; int j = 0; while (prev && j < i - 1) { prev = prev->next; j++; } if (!prev) return 0; // 越界判断 LNode* newNode = (LNode*)malloc(sizeof(LNode)); newNode->data = value; newNode->next = prev->next; prev->next = newNode; L->data++; // 更新链表长度 return OK; } ``` --- #### 删除操作 删除第 `i` 个位上的元素,并释放对应内存空间[^2]。 ```c int DeleteList(LinkList* L, int i) { if (i < 1 || i > L->data) return 0; LNode* p = L; int j = 0; while (j < i - 1) { p = p->next; j++; } LNode* toDelete = p->next; if (!toDelete) return 0; p->next = toDelete->next; free(toDelete); L->data--; // 减少链表长度 return OK; } ``` --- #### 操作(就地反转) 通过三指针技术将整个链表方向翻转,时间复杂度 O(n)[^8]。 ```c void ReverseList(LinkList* L) { LNode* pre = NULL; LNode* cur = L->next; LNode* next; while (cur) { next = cur->next; // 记录下一节点 cur->next = pre; // 当前节点指向前驱 pre = cur; // 移动pre cur = next; // 移动cur } L->next = pre; // 头指针重新指向新的首元结点 } ``` --- #### 完整测试示例 ```c int main() { LinkList* list = CreateList(); InsertList(list, 1, 10); InsertList(list, 2, 20); InsertList(list, 3, 30); printf("原序列: "); DisplayList(list); // 输出: 10 20 30 DeleteList(list, 2); printf("删去第2个后: "); DisplayList(list); // 输出: 10 30 ReverseList(list); printf("后: "); DisplayList(list); // 输出: 30 10 printf("查找值为30的位:%d\n", LocateElem(list, 30)); return 0; } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值