Skip to content

Commit c7e3a1b

Browse files
author
wangzheng
committed
Merge branch 'master' of https://github.com/wangzheng0822/algo
2 parents 62636dc + 576940c commit c7e3a1b

File tree

22 files changed

+2110
-1
lines changed

22 files changed

+2110
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# 数据结构和算法之美
2-
# https://time.geekbang.org/column/126
2+
# 请点击查看:[https://time.geekbang.org/column/intro/126](https://time.geekbang.org/column/intro/126)

c-cpp/05_array/array.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
struct array {
6+
int size;
7+
int used;
8+
int *arr;
9+
};
10+
11+
void dump(struct array *array)
12+
{
13+
int idx;
14+
15+
for (idx = 0; idx < array->used; idx++)
16+
printf("[%02d]: %08d\n", idx, array->arr[idx]);
17+
}
18+
19+
void alloc(struct array *array)
20+
{
21+
array->arr = (int *)malloc(array->size * sizeof(int));
22+
}
23+
24+
int insert(struct array *array, int elem)
25+
{
26+
int idx;
27+
if (array->used >= array->size)
28+
return -1;
29+
30+
for (idx = 0; idx < array->used; idx++) {
31+
if (array->arr[idx] > elem)
32+
break;
33+
}
34+
35+
if (idx < array->used)
36+
memmove(&array->arr[array->used], &array->arr[idx],
37+
(array->used - idx) * sizeof(int));
38+
39+
array->arr[idx] = elem;
40+
array->used++;
41+
return idx;
42+
}
43+
44+
int delete(struct array *array, int idx)
45+
{
46+
if (idx < 0 || idx >= array->used)
47+
return -1;
48+
49+
memmove(&array->arr[idx], &array->arr[idx+1],
50+
(array->used - idx) * sizeof(int));
51+
array->used--;
52+
return 0;
53+
}
54+
55+
int search(struct array *array, int elem)
56+
{
57+
int idx;
58+
59+
for (idx = 0; idx < array->used; idx++) {
60+
if (array->arr[idx] == elem)
61+
return idx;
62+
if (array->arr[idx] > elem)
63+
return -1;
64+
}
65+
66+
return -1;
67+
}
68+
69+
int main()
70+
{
71+
int idx;
72+
struct array ten_int = {10, 0, NULL};
73+
74+
alloc(&ten_int);
75+
if (!ten_int.arr)
76+
return -1;
77+
insert(&ten_int, 1);
78+
insert(&ten_int, 3);
79+
insert(&ten_int, 2);
80+
printf("=== insert 1, 3, 2\n");
81+
dump(&ten_int);
82+
83+
idx = search(&ten_int, 2);
84+
printf("2 is at position %d\n", idx);
85+
idx = search(&ten_int, 9);
86+
printf("9 is at position %d\n", idx);
87+
88+
printf("=== delete [6] element \n");
89+
delete(&ten_int, 6);
90+
dump(&ten_int);
91+
printf("=== delete [0] element \n");
92+
delete(&ten_int, 0);
93+
dump(&ten_int);
94+
return 0;
95+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#include "LinkList.h"
2+
3+
void CreateListHead(LinkList *&L,ElemType a[],int n)
4+
{
5+
int i;
6+
LinkList *s;
7+
L = (LinkList *)malloc(sizeof(LinkList));
8+
L->next = NULL;
9+
for(i = 0;i < n;i++)
10+
{
11+
s=(LinkList*)malloc(sizeof(LinkList));
12+
s->data = a[i];
13+
s->next = L->next;
14+
L->next = s;
15+
}
16+
}
17+
void CreateListTail(LinkList *&L,ElemType a[],int n)
18+
{
19+
int i;
20+
LinkList * s,* r;
21+
L = (LinkList *)malloc(sizeof(LinkList));
22+
r = L;
23+
for(i = 0;i < n;i++)
24+
{
25+
s = (LinkList *)malloc(sizeof(LinkList));
26+
s->data = a[i];
27+
r->next = s;
28+
r = s;
29+
}
30+
r->next = NULL;
31+
}
32+
void InitList(LinkList *&L)
33+
{
34+
L=(LinkList *)malloc(sizeof(LinkList));
35+
L->next = NULL;
36+
}
37+
void DestroyList(LinkList *&L)
38+
{
39+
LinkList * pre = L,*p = L->next;
40+
while(p!=NULL)
41+
{
42+
free(pre);
43+
pre = p;
44+
p = L->next;
45+
}
46+
free(pre);
47+
}
48+
bool ListEmpty(LinkList *L)
49+
{
50+
return(L->next==NULL);
51+
}
52+
int ListLength(LinkList *L)
53+
{
54+
int n = 0;
55+
LinkList * p = L;
56+
while(p->next!=NULL)
57+
{
58+
n++;
59+
p=p->next;
60+
}
61+
return(n);
62+
}
63+
void ShowList(LinkList *L)
64+
{
65+
LinkList * p = L->next;//Ö¸Ïò¿ªÊ¼½Úµã
66+
while(p!=NULL)
67+
{
68+
printf(" %c ",p->data);
69+
p = p->next;
70+
}
71+
printf("\n");
72+
}
73+
bool GetListElem(LinkList *L,int i,ElemType &e)
74+
{
75+
int j = 0;
76+
LinkList *p = L;
77+
while(j<i&&p!=NULL)
78+
{
79+
j++;
80+
p=p->next;
81+
}
82+
if(p==NULL)
83+
return false;
84+
else
85+
{
86+
e=p->data;
87+
return true;
88+
}
89+
}
90+
int LocateElem(LinkList*L,ElemType e)
91+
{
92+
int i=1;
93+
LinkList *p = L->next;
94+
while(p!=NULL&&p->data!=e){
95+
p=p->next;
96+
i++;
97+
}
98+
if(p==NULL)
99+
{
100+
return(0);
101+
}
102+
else
103+
return(i);
104+
}
105+
bool ListInsert(LinkList *&L,int i,ElemType e)
106+
{
107+
int j=0;
108+
LinkList *p =L,*s;
109+
while(j<i-1&&p!=NULL)
110+
{
111+
j++;
112+
p=p->next;
113+
}
114+
if(p==NULL)
115+
{
116+
return false;
117+
}
118+
else
119+
{
120+
s= (LinkList*)malloc(sizeof(LinkList));
121+
s->data = e;
122+
s->next = p->next;
123+
p->next = s;
124+
return true;
125+
}
126+
}
127+
bool ListDelete(LinkList *&L,int i,ElemType &e)
128+
{
129+
int j=0;
130+
LinkList * p =L,*q;
131+
while(j<i-1&&p!=NULL)
132+
{
133+
j++;
134+
p=p->next;
135+
}
136+
if(p==NULL)
137+
return false;
138+
else
139+
{
140+
q=p->next;
141+
if(q==NULL)
142+
return false;
143+
e=q->data;
144+
p->next=q->next;
145+
free(q);
146+
return true;
147+
}
148+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef LINKLIST_H
2+
#define LINKLIST_H
3+
4+
#include <stdlib.h>
5+
#include <stdio.h>
6+
typedef char ElemType;
7+
typedef struct LNode
8+
{
9+
ElemType data;
10+
struct LNode*next;
11+
}LinkList;
12+
13+
void CreateListHead(LinkList *&L,ElemType a[],int n);
14+
void CreateListTail(LinkList *&L,ElemType a[],int n);
15+
void InitList(LinkList *&L);
16+
void DestroyList(LinkList *&L);
17+
bool ListEmpty(LinkList *L);
18+
int ListLength(LinkList *L);
19+
void ShowList(LinkList *L);
20+
bool GetListElem(LinkList *L,int i,ElemType &e);
21+
int LocateElem(LinkList*L,ElemType e);
22+
bool ListInsert(LinkList *&L,int i,ElemType e);
23+
bool ListDelete(LinkList *&L,int i,ElemType &e);
24+
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include"LinkList.h"
2+
char array1[]= {'a','b','c','b','a'};
3+
bool isPalindrome(LinkList * list,int length);
4+
int main()
5+
{
6+
LinkList * list;
7+
8+
int length = sizeof(array1)/sizeof(array1[0]);
9+
InitList(list);
10+
CreateListTail(list,array1,length);//用尾插法创建一个单链表
11+
if(isPalindrome(list,length))
12+
printf("isPalindrome\n");
13+
else
14+
printf("isNotPalindrome\n");
15+
return 0;
16+
}
17+
bool isPalindrome(LinkList * list,int length)
18+
{
19+
int i;
20+
char buff1[length],buff2[length];
21+
ElemType e;
22+
for(i=1; i<=length; i++)
23+
{
24+
GetListElem(list,i,e);//遍历获取链表元素并放入数组中
25+
buff1[i-1]=e;//正向数组
26+
buff2[length-i]=e;//反向数组
27+
}
28+
i=0;
29+
while(i<=length) {
30+
if(buff1[i]==buff2[i])//比较
31+
{
32+
i++;
33+
}
34+
else
35+
{
36+
return false;
37+
}
38+
}
39+
return true;
40+
}

0 commit comments

Comments
 (0)