链表
- 定义
// 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();
一般以指针定义。
- 创建简单链表
// 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;
}
- 遍历结点
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());