C++实现单链表(含完整代码)

C++实现单链表(含完整代码)

使用C++实现单链表的基本操作

1、创建单链表
2、遍历单链表
3、插入单链表元素
4、单链表删除元素
5、判断单链表是否为空
6、单链表的长度
7、查找单链表元素

完整代码:


#include <iostream>
 
using namespace std;
 
struct List {
    int val;
    struct List* next;
};
 
void Init(struct List* L) { //创建链表
    int cur;
    cin >> cur;
    while (cur != -1) {
        struct List* ptr = (struct List*)malloc(sizeof(struct List));
        ptr->val = cur;
        ptr->next = NULL;
        L->next = ptr;
        L = L->next;
        cin >> cur;
    }
}
 
void Show(struct List* L) { //遍历链表值
    cout << "链表遍历:" ;
    
    while (L->next) {
        cout << L->next->val << " ";
        L = L->next;
    }
    cout << endl;
}
 
//在第K个位置前插入data元素,最后链表 的第K个位置就是data
void InsertList (struct List* L, int k, int data) {
    
    struct List* pre = NULL; //存储第K-1个元素的值
    struct List* ptr = (struct List*)malloc(sizeof(struct List));   //申请空间
    ptr->val = data;
    ptr->next = NULL;
    
    while (k && L->next) {  //查找到放置data元素的位置
        pre = L;
        L = L->next;
        k --;
    }
    
    if (k > 0) {    //如果K > 0 直接插到链表的表尾
        L->next = ptr;
        L = L->next;
    }else {
        pre->next = ptr;    //链接链表
        ptr->next = L;
    }
}
 
int lengthList (struct List* L) {   //求链表长度
    int len = 0;
    while (L->next) { 
        len ++;
        L = L->next;
    }
    return len;
}
void DeleteList (struct List* L, int x) {   //删除值为x的结点(链表无重复值)
    if (lengthList(L) <= 0) {
        cout << "表空,没有元素可删除" << endl;
        return;
    }
 
    struct List* ptr = L->next;
    struct List* pre = L;   //记录ptr的前一个位置的结点
    while (ptr) {
        if (ptr->val == x) {
            pre->next = ptr->next;  //把x值的结点的前一个结点的next指向ptr的next结点
            free(ptr);  //释放空间
            return;
        }
        pre = ptr;
        ptr = pre->next;
    }
}
 
void DeleteList_Position(struct List* L, int k) {   //删除第K个位置的结点
    if (lengthList(L) <= 0) {
        cout << "表空,没有元素可删除" << endl;
        return;
    }
 
    struct List* ptr = L->next;
    struct List* pre = L;   //记录ptr的前一个位置的结点
    k = k - 1;  //因为如果k = 1,直接用pre->next = ptr->next就把ptr删掉了,所以要减1
    while (k-- && ptr) {
        pre = ptr;
        ptr = ptr->next;
    } 
    if (ptr == NULL || k > 0) {
        cout << "要删除的位置不存在" << endl;
    }else {
        pre->next = ptr->next;  //删除ptr结点
        free(ptr);  //释放空间
    }
}
 
bool IsEmptyList(struct List* L) {  //判断链表是否为空
    if (L->next == NULL) {
        return true;
    }else {
        return false;
    }
}
 
 
int GetElemList(struct List* L, int i) {    //返回第i个位置的值
    struct List* ptr = L;
    int k = i;  //标记i的值,以防不存在输出显示
    while (i > 0 && ptr->next) {
        ptr = ptr->next;
        i --;
    }
 
    if (i == 0 && ptr != NULL) {    //当i == 0 和 ptr 不为空代表找到了第i个位置的元素
        return ptr->val;
    }else {
        cout << "第" << k << "个位置不存在" << endl;
        return -1;
    }
}
 
void ClearList(struct List* L) {    //清空链表
     struct List* ptr = L;
    if (lengthList(L) > 0) {
        while (ptr->next) {
           struct List* temp = ptr->next;
           ptr->next = ptr->next->next;
           free(temp);  //释放空间
        }
    }
}
 
int main() {
    
    struct List* head = (struct List*)malloc(sizeof(struct List)); //头结点(不存值)
    head->next = NULL;  


   while(1)
   {
	    cout << "**********************************************" << endl;
		cout << "* 1、创建单链表(以-1结束)     2、打印单链表  *" << endl;
		cout << "* 3、插入单链表               4、单链表删除  *" << endl;
		cout << "* 5、判断是否为空             6、单链表长度  *" << endl;
		cout << "* 7、查找                     8、退出        *" << endl;
		cout << "**********************************************" << endl;
    
		int  k;
	    cout<<"请输入你的选择:";
	    cin>>k;
		switch(k)
		{

		case 1:
			  Init(head);      //创建单链表
              system("pause");
		      system("cls");
		      continue;

        case 2:
			  Show(head);            //遍历单链表
			  system("pause");
		      system("cls");
		      continue;

		case 3:

			  int i, data;
		   	  cout << "请输入要插入的位置和值:";
			  cin >> i;
			  cin >> data;
			  InsertList(head, i, data); 
			  Show(head);
			  system("pause");
			  system("cls");
		 	  continue;


		case 4:
			 
			  int x;
			  cout << "请输入要删除的值: ";
              cin >> x;
 
              DeleteList(head, x);    //删除链表中值为x的结点(链表值无重复)

			  system("pause");
			  system("cls");
		 	  continue;

		case 5:
			  if (IsEmptyList(head))
                  cout << "链表是空链表!" << endl;
			 else 
                  cout << "链表不空!" << endl;
		      
			  system("pause");
			  system("cls");
		 	  continue;
		case 6:
			  
			  cout << "链表的长度为: " << lengthList(head) << endl;

			  system("pause");
			  system("cls");
		 	  continue;

		case 7:
			  int n;
			  cout << "请输入要查找的位置: ";
			  cin >> n;
			  if (GetElemList(head, n) != -1)
                cout << "第" << n << "个位置的值为: " << GetElemList(head, n) << endl; 
              system("pause");
			  system("cls");
		 	  continue;

		case 8:
			  break;

         default:
			cout << "请输入正确的选项!!!" << endl;
			system("pause");
			system("cls");
			continue;
	   
		
		}
		system("cls");
		break;

   
   }
		system("pause");
		return 0;
	
}


运行截图:

在这里插入图片描述

  • 72
    点赞
  • 436
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
以下是用C++实现单链表操作的示例代码: ```cpp #include <iostream> using namespace std; // 定义链表节点结构体 struct ListNode { int val; // 存储节点的值 ListNode *next; // 指向下一个节点的指针 ListNode(int x) : val(x), next(NULL) {} // 构造函数 }; // 定义链表类 class LinkedList { public: LinkedList() { // 构造函数 head = NULL; } ~LinkedList() { // 析构函数 ListNode *cur = head; while (cur != NULL) { ListNode *temp = cur; cur = cur->next; delete temp; } } void add(int val) { // 添加节点 if (head == NULL) { head = new ListNode(val); } else { ListNode *cur = head; while (cur->next != NULL) { cur = cur->next; } cur->next = new ListNode(val); } } void remove(int val) { // 移除节点 if (head == NULL) { return; } if (head->val == val) { ListNode *temp = head; head = head->next; delete temp; } else { ListNode *cur = head; while (cur->next != NULL && cur->next->val != val) { cur = cur->next; } if (cur->next != NULL) { ListNode *temp = cur->next; cur->next = cur->next->next; delete temp; } } } void print() { // 打印链表 ListNode *cur = head; while (cur != NULL) { cout << cur->val << " "; cur = cur->next; } cout << endl; } private: ListNode *head; // 链表头指针 }; int main() { LinkedList list; list.add(1); list.add(2); list.add(3); list.print(); // 输出:1 2 3 list.remove(2); list.print(); // 输出:1 3 return 0; } ``` 该代码实现单链表的添加、移除和打印操作。需要注意的是,析构函数需要手动释放链表中所有节点的内存,否则可能会出现内存泄漏的问题。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柒月VII

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值