顺序表代码c实现:
#include<stdio.h>
#include<Windows.h>
#define LIST_INIT_SIZE 5
#define LIST_ADD_SIZE 1
typedef int ElemType;// 顺序表内容元素的类型,这么写完全是为了好维护
typedef struct {
ElemType *elem;
int length;//表中最后一个元素的位置tail
int size; //顺序表的容量
}Sqlist;
void addElem(Sqlist &L, ElemType e) {
if (L.length == L.size) {
printf("扩若了");
ElemType *temp = (ElemType *)realloc(L.elem, (L.size + LIST_ADD_SIZE)*sizeof(ElemType));
if (!temp) {
exit(-1);
}
L.elem = temp;
L.size += LIST_ADD_SIZE;
}
L.elem[L.length] = e;
L.length++;
}
void printList(Sqlist &L) {
printf("打印整个个顺序表\n");
for (int i = 0; i < L.length; i++)
{
printf("%d ", L.elem[i]);
}
printf("\n");
}
void init(Sqlist &L) {
L.elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem) {
exit(-1);
}
L.length = 0;
L.size = LIST_INIT_SIZE;
}
void getElem(Sqlist &L, int i, ElemType &e) {
if (i<1 || i>L.length) {
return;
}
e = L.elem[i - 1];
}
void insert(Sqlist &L, int i, ElemType e) {
if (i<1 || i>L.length+1) {
return;
}
if (L.length == L.size) {
ElemType *temp = (ElemType *)realloc(L.elem, (L.size + LIST_ADD_SIZE));
if (!temp) {
exit(-1);
}
L.elem = temp;
L.size += LIST_ADD_SIZE;
}
for (int m = L.length - 1; m >= i - 1; --m) {//从后往前
L.elem[m + 1] = L.elem[m];
}
L.elem[i-1] = e;
L.length++;
}
void del(Sqlist &L, int i) {
if (i<1 || i>L.length ) {
return;
}
ElemType e = L.elem[i - 1];
for (int m = i; m < L.length; m++)
{
L.elem[m - 1] = L.elem[m];
}
L.length--;
}
int find(Sqlist &L, ElemType e) {
int i = 1;
while (i <= L.length&&L.elem[i - 1] != e) {
i++;
}
if (i > L.length) {
i = 0;
}
return i;
}
int main() {
Sqlist L;
printf("测试初始化和添加函数\n");
init(L);
addElem(L, 1);
addElem(L, 2);
addElem(L, 3);
printList(L);
/*printf("测试插入函数\n");
insert(L, 2, 5);
printList(L);
printf("测试获取函数\n" );
int num = 0;
getElem(L, 1, num);
printf("%d\n",num);
printf("测试删除函数\n");
del(L, 2);
printList(L);
printf("测试查找函数\n");
int index = find(L, 3);
if (index == 0) {
printf("没找到\n");
}
else {
printf("%d\n", index);
}*/
}
链表C语言实现:
#include<stdio.h>
#include<Windows.h>
typedef int E;// 顺序表内容元素的类型,这么写完全是为了好维护
typedef struct HeroNode{
E value;
struct HeroNode *next;
void toString() {
printf("%d ", value);
}
}*LinkedList;
void createList(LinkedList &L) {
L = (LinkedList)malloc(sizeof(HeroNode));
L->next = NULL;
L->value = 0;//其实L的值我们不关心,这一行可以不写
}
void addFromHead(LinkedList &L, HeroNode *p) {//L的位置一直不在动
p->next = L->next;
L->next = p;
}
void addFromTail( HeroNode *p,LinkedList &tail) {//tail移动,tail为参数合适
tail->next = p;
tail = p;//这里p之前一定要置空
}
void printLinkedList(LinkedList L) {
HeroNode *p = L->next;
while (p) {
p->toString();
p = p->next;
}
}
int getLength(LinkedList &L) {
int counts = 0;
HeroNode *p = L;
while (p->next) {
counts++;
p = p->next;
}
return counts;
}
E getElem(LinkedList &L, int i) {
if (i<1 || i>getLength(L)) {
return -1;
}
int num = 1;
HeroNode *p = L->next;//1
while (1&&num<=getLength(L)) {
if (num == i) {
return p->value;
}
num++;
p = p->next;
}
return -1;
}
void insert(LinkedList &L,int i,HeroNode *p) {
if (i > getLength(L) + 1 || i <= 0) {
return ;
}
int num = 1;
HeroNode *temp = L;//1
while (num <= getLength(L)+1) {
if (num == i)
{
p->next = temp->next;
temp->next = p;
break;
}
num++;
temp = temp->next;
}
}
int main() {
LinkedList L;
createList(L);
HeroNode *p = (LinkedList)malloc(sizeof(HeroNode)); p->value = 5;
HeroNode *p1 = (LinkedList)malloc(sizeof(HeroNode)); p1->value = 6;
HeroNode *p2 = (LinkedList)malloc(sizeof(HeroNode)); p2->value = 3;
addFromHead(L, p); addFromHead(L, p1); addFromHead(L, p2);
printLinkedList(L);//365,因为是头插法
LinkedList L1;
createList(L1);
LinkedList tail = L1;
HeroNode *p3 = (LinkedList)malloc(sizeof(HeroNode)); p3->value = 5; p3->next = NULL;
HeroNode *p4= (LinkedList)malloc(sizeof(HeroNode)); p4->value = 6; p4->next = NULL;
HeroNode *p5 = (LinkedList)malloc(sizeof(HeroNode)); p5->value = 3; p5->next = NULL;
addFromTail( p3, tail); addFromTail( p4, tail);// addFromTail(p5, tail);
printLinkedList(L1);//563
printf("\n");
//测试
printf("%d", getLength(L1));
printf("%d",getElem(L1, 1)); printf("\n");
HeroNode *p6= (LinkedList)malloc(sizeof(HeroNode)); p6->value = 89; p6->next = NULL;
insert(L1, 3, p6);
printLinkedList(L1);
return 0;
}
链表c++实现:(感觉简单多了,相较于c)
#include<bits/stdc++.h>
using namespace std;
typedef int E;
class HeroNode {
public:
E value;
HeroNode *next;
HeroNode(E value) {
this->value=value ;
this->next = NULL;
}
void toString() {
printf("value=%d ", value);
}
};
class LinkedList {
public:
HeroNode *head = new HeroNode(0);
void addFromHead(HeroNode *p) {//Head的位置一直不在动
p->next = head->next;
head->next = p;
}
void printLinkedList() {
HeroNode *p = head->next;
while (p) {
p->toString();
p = p->next;
}
}
int getLength() {
int counts = 0;
HeroNode *p = head;
while (p->next) {
counts++;
p = p->next;
}
return counts;
}
E getElem( int i) {
if (i<1 || i>getLength()) {
return -1;
}
int num = 1;
HeroNode *p = head->next;//1
while (1 && num <= getLength()) {
if (num == i) {
return p->value;
}
num++;
p = p->next;
}
return -1;
}
void insert(int i,HeroNode *p) {
if (i > getLength() + 1 || i <= 0) {
return ;
}
int num = 1;
HeroNode *temp = head->next;//1
while (num <= getLength()+1) {
if (num == i)
{
p->next = temp->next;
temp->next = p;
break;
}
num++;
temp = temp->next;
}
}
};
int main() {
LinkedList list;
HeroNode *h1=new HeroNode(8);
HeroNode *h2 = new HeroNode(5);
HeroNode *h3 = new HeroNode(3);
list.addFromHead(h1);
list.addFromHead(h2);
list.addFromHead(h3);
list.printLinkedList();
cout<<list.getLength();
cout << list.getElem(2);
HeroNode *h4 = new HeroNode(78);
list.insert(1, h4);
list.printLinkedList();
}
链表处理多项式c++实现:
#include<bits/stdc++.h>
using namespace std;
class Node {
public:
int c;//changshu
int z;//zhishuxiang
Node *next=NULL;
Node(int c, int z) {
this->c = c;
this->z = z;
}
void toString() {
printf("%dx^%d", c, z);
}
};
class NodeList {
public:
Node *head = new Node(0, 0);
Node *tail = head;
Node* getFirstNode() {
return head->next;
}
void addFromTail(Node *p) {//tail移动,tail为参数合适
tail->next = p;
tail = p;//这里p之前一定要置空
}
void printNodeList() {
printf("f(x)=");
Node *p = head->next;
while (p) {
if (p->c == 0) {
p = p->next;
continue;
}
if (p->c > 0&&p!=head->next) {
printf("+");
}
p->toString();
p = p->next;
}
printf("\n");
}
void addBetweenList(Node *fa,Node *fb) {
while (fa&&fb) {
if (fa->z < fb->z) {
tail->next = fa;
tail = fa;
fa = fa->next;
}
else if (fa->z > fb->z) {
tail->next = fb;
tail = fb;
fb = fb->next;
}
else if(fa->z +fb->z==0){
fa = fa->next; fb = fb->next;
}
else {
Node *temp=new Node(fa->c+fb->c,fa->z);
tail->next = temp;
tail = temp;
fa = fa->next; fb = fb->next;
}
}
//将可能剩余的残留合并
if (fa != NULL) {
tail->next = fa;
tail = fa;
}
else if (fb!=NULL) {
tail->next = fb;
tail = fb;
}
}
};
int main() {
NodeList L,L1,L2;
Node *n1 = new Node(1,0);
Node *n2 = new Node(-10, 6);
Node *n3 = new Node(2, 8);
Node *n4 = new Node(7, 14);
Node *n5 = new Node(-1, 4);
Node *n6 = new Node(10, 6);
Node *n7 = new Node(-3, 10);
Node *n8 = new Node(8, 14);
Node *n9 = new Node(4, 18);
Node *n10 = new Node(7, 20);
L.addFromTail(n1); L.addFromTail(n2); L.addFromTail(n3); L.addFromTail(n4);
L1.addFromTail(n5); L1.addFromTail(n6); L1.addFromTail(n7); L1.addFromTail(n8); L1.addFromTail(n9); L1.addFromTail(n10);
L.printNodeList(); L1.printNodeList();
Node* fa = L.getFirstNode();
Node* fb = L1.getFirstNode();
L2.addBetweenList(fa, fb);
L2.printNodeList();
}
栈 c++模板实现:
#include<bits/stdc++.h>
using namespace std;
template<class T>
class Stack
{
public:
Stack() {
arr = new T(size);
}
bool isEmpty() {
return head == tail;
}
bool isFull() {
return tail == size-1;
}
void push(T p) {
if (isFull()) {
cout << "满了" << endl;
return;
}
tail++;
arr[tail] = p;
}
T pop() {
if (isEmpty()) {
cout << "空了" << endl;
return NULL;
}
T temp = arr[tail];
tail--;
return temp;
}
T peek() {
return arr[tail];
}
void printStack() {
for (int i = head+1; i <=tail; i++)
{
cout << arr[i]<<" ";
}
}
private:
int head = -1;
int tail = -1;
T *arr;
int size = 2;
};
int main() {
Stack<int>s;
s.push(1);
s.push(2);
s.printStack();
cout<<s.peek();
}
进制转化c++实现:
#include<bits/stdc++.h>
using namespace std;
int main() {
int D,result ;
cin >>D>>result;
stack<int>s;
while (result != 0) {
s.push(result % D);
result = result / D;
}
if (s.empty()) {
cout << 0;
}
while (!s.empty()) {
cout << s.top();
s.pop();
}
return 0;
}
字符串表达式括号使用是否合法检测C++实现:
c++实现:
#include<bits/stdc++.h>
using namespace std;
bool isLeft(char c) {
return (c == '{' || c == '[' || c == '(');
}
bool isRight(char c) {
return (c == '}' || c == ']' || c == ')');
}
bool isMatch(char c, char d) {
if (c == '{'&&d == '}') {
return true;
}
if (c == '['&&d == ']') {
return true;
}
if (c == '('&&d == ')') {
return true;
}
return false;
}
bool isStrLegal(char *arr) {
stack<char> s;
int len = strlen(arr);
for (int i = 0; i < len; i++)
{
if (isLeft(arr[i])) {
s.push(arr[i]);
}
else if (isRight(arr[i])) {
char temp = s.top();
if (isMatch(temp, arr[i])) {
s.pop();
}
else {
return false;
}
}
}
return s.empty();
}
int main() {
char arr[] = "{(})";
int len = sizeof(arr);
if (isStrLegal(arr)) {
cout << "合法" << endl;
}
else {
cout << "不合法" << endl;
}
}
//稀疏数组c++这一节课其实挺绕的,自己多思考(更新于2020.2.24)
#include<iostream>
//稀疏数组c++
using namespace std;
#define SIZE 100
typedef struct {
int i, j;//非零行列下标
int value;
void toString() {
cout << i << " " << j << " " << value << endl;
}
}Ele;
typedef struct S{
Ele data[SIZE];
int m, n, t;
void printLikeArr() {
cout << "打印稀疏矩阵,还原的普通矩阵" << endl;
int p = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++) {
if (data[p].i == i && data[p].j== j) {
//cout << data[p].value << " ";
printf("%d ",data[p].value);
p++;
}
else {
cout << 0 << " ";
}
}
cout << endl;
}
}
void transpose(S &B) {
int q = 0;
B.m = this->n; B.n = this->m; B.t = this->t;
for (int col = 0; col < this->n; col++)
{
for (int p = 0; p < this->t; p++)
{
if (this->data[p].j == col) {
B.data[q].i = this->data[p].j;
B.data[q].j = this->data[p].i;
B.data[q].value= this->data[p].value;
q++;
}
}
}
}
void quickTranspose(S &B) {
int q ;
int num[100] = { 0 }; //统计当前稀疏数组每一列非零元素的个数
int pos[100] = { 0 };//每列首个非零元素在转置矩阵中的索引
B.m = this->n; B.n = this->m; B.t = this->t;
for (int p = 0; p <this->t; p++)//完成初始化
{
num[this->data[p].j]++;
}
for (int j = 1; j < this->n; j++)//完成初始化,注意从一开始
{
pos[j] = pos[j - 1] + num[j - 1];//比如第一列两个元素,第二列的肯定放在0+2的位置
}
for (int p = 0; p < this->t; p++)//遍历当前稀疏数组每一个数据
{
Ele temp = this->data[p];//按顺序取出来一个
q = pos[temp.j];//获得这个元素所在列的首个非零元素的,注意(除非是j第一次被使用),不然其实pos【j】里面内容已经被++修改了
B.data[q].i = temp.j;
B.data[q].j = temp.i;
B.data[q].value = temp.value;
pos[temp.j]++;
}
}
}sparseArray;
int main() {
int arr[6][7] = {0};
int counts = 0;
int p = 0;//索引
arr[0][1] = 12; arr[0][2] = 9; arr[2][0] = -3;//自定义初始化一个二维数组
arr[2][5] = 14; arr[3][2] = 24; arr[4][1] = 18; arr[5][0] = 15; arr[5][3] = -7;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++) {
if (arr[i][j] != 0) {
counts++;
}
}
}
sparseArray s;
s.m = 6; s.n = 7; s.t = counts;//完成稀疏矩阵功能行的初始化
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++) {
if (arr[i][j] != 0) {
s.data[p].i = i;
s.data[p].j = j;
s.data[p].value = arr[i][j];
p++;
}
}
}
for (int i = 0; i < counts; i++)
{
s.data[i].toString();
}
s.printLikeArr();
sparseArray B;//转置矩阵
s.transpose(B);
cout << "输出s的转置稀疏矩阵B" << endl;
for (int i = 0; i < counts; i++)
{
B.data[i].toString();
}
B.printLikeArr();
sparseArray C;
s.quickTranspose(C);
C.printLikeArr();
}
c++实现二叉树的创建和前序遍历
#include<iostream>
using namespace std;
class Node {
public:
char value;Node * left;Node * right;
Node(char v) {
value = v;
}
void toString() {
cout << value << " ";
}
void preOrder() {
this->toString();
if (this->left != NULL)
this->left->preOrder();
if (this->right != NULL)
this->right->preOrder();
}
};
class NodeTree {
public:
Node * root;
void creatTree(Node* &node) {//先序输入创建二叉树
char ch;cin >> ch;
if (ch == '#')
node = NULL;
else {
node = new Node(ch);
creatTree(node->left);
creatTree(node->right);
}
}
void preOrder() {//先序遍历,还有两种,重复不写了
if (root==NULL)
{
cout << "二叉树为空" << endl;
return;
}
else
this->root->preOrder();
}
};
int main() {
NodeTree *tree=new NodeTree();
tree->creatTree(tree->root);//由头结点创建
tree->preOrder();
}