LeetCode刷题 C++数据结构

C++ 数据结构

链表

  1. 定义
// Definition for singly-linked list.
struct ListNode 
{
	int val;
	ListNode *next;
	ListNode() : val(0), next(nullptr) {}
	ListNode(int x) : val(x), next(nullptr) {}
	ListNode(int x, ListNode *next) : val(x), next(next) {}
};

链表的数据结构定义当前结点的值和指向下一结点的指针。
创建一个新结点:ListNode* node = new ListNode(); ListNode node = ListNode();一般以指针定义。

  1. 创建简单链表
// This program illustrates the creation || of linked lists.
#include <iostream>
using namespace std;
struct ListNode
{
    double value;
    ListNode *next;
};
int main()
{
    ListNode *head = nullptr;
    // Create first node with 12.5
    head = new ListNode; // Allocate new node
    head->value = 12.5;    // Store the value
    head->next = nullptr; // Signify end of list
    // Create second node with 13.5
    ListNode *secondPtr = new ListNode;
    secondPtr->value = 13.5;
    secondPtr->next = nullptr; // Second node is end of list
    head->next = secondPtr; // First node points to second
    // Print the list
    cout << "First item is " << head->value << endl;
    cout << "Second item is " << head->next->value << endl;
    return 0;
}
  1. 遍历结点
ListNode *ptr = head;
while (ptr != nullptr)
{
    cout << ptr->value << " "; //处理结点(显示结点内容)
    ptr = ptr->next; //移动到下一个结点
}

先把链表指向开头,然后向后移动直到终结点。
只要有节点的向后移动,在判断里一定要加是否为空! 并且空指针是没有val值的

字符串

基本用法
http://c.biancheng.net/view/400.html

判断字符是不是数字,大写,小写

/* 用ASCII码判断,比如48-57的ASCII码是0-9数字 */
// if((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z'))
int tmp = (int)str[i];
if (tmp >= 48 && tmp <= 57)
{
     continue;
 }
 else
 {
     return false;
 }

/* 用C++提供的stringstream对象 */ 
#include <sstream> 
string string("aabb112");
bool flag1 = isdigit(string[6]);
bool flag2 = isalpha(string[6]);
bool flag3 = isupper(string[6]);
bool flag4 = islower(string[6]);
bool flag5 = isalnum(string[6]);

大小写转换

#include<algorithm>
char a = toupper(string[2]);
char b = tolower(string[1]);

反转字符串
reverse(str.begin(), str.end());

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值