forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/iostalks/algo
- Loading branch information
Showing
16 changed files
with
1,558 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
struct array { | ||
int size; | ||
int used; | ||
int *arr; | ||
}; | ||
|
||
void dump(struct array *array) | ||
{ | ||
int idx; | ||
|
||
for (idx = 0; idx < array->used; idx++) | ||
printf("[%02d]: %08d\n", idx, array->arr[idx]); | ||
} | ||
|
||
void alloc(struct array *array) | ||
{ | ||
array->arr = (int *)malloc(array->size * sizeof(int)); | ||
} | ||
|
||
int insert(struct array *array, int elem) | ||
{ | ||
int idx; | ||
if (array->used >= array->size) | ||
return -1; | ||
|
||
for (idx = 0; idx < array->used; idx++) { | ||
if (array->arr[idx] > elem) | ||
break; | ||
} | ||
|
||
if (idx < array->used) | ||
memmove(&array->arr[array->used], &array->arr[idx], | ||
(array->used - idx) * sizeof(int)); | ||
|
||
array->arr[idx] = elem; | ||
array->used++; | ||
return idx; | ||
} | ||
|
||
int delete(struct array *array, int idx) | ||
{ | ||
if (idx < 0 || idx >= array->used) | ||
return -1; | ||
|
||
memmove(&array->arr[idx], &array->arr[idx+1], | ||
(array->used - idx) * sizeof(int)); | ||
array->used--; | ||
return 0; | ||
} | ||
|
||
int search(struct array *array, int elem) | ||
{ | ||
int idx; | ||
|
||
for (idx = 0; idx < array->used; idx++) { | ||
if (array->arr[idx] == elem) | ||
return idx; | ||
if (array->arr[idx] > elem) | ||
return -1; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
int main() | ||
{ | ||
int idx; | ||
struct array ten_int = {10, 0, NULL}; | ||
|
||
alloc(&ten_int); | ||
if (!ten_int.arr) | ||
return -1; | ||
insert(&ten_int, 1); | ||
insert(&ten_int, 3); | ||
insert(&ten_int, 2); | ||
printf("=== insert 1, 3, 2\n"); | ||
dump(&ten_int); | ||
|
||
idx = search(&ten_int, 2); | ||
printf("2 is at position %d\n", idx); | ||
idx = search(&ten_int, 9); | ||
printf("9 is at position %d\n", idx); | ||
|
||
printf("=== delete [6] element \n"); | ||
delete(&ten_int, 6); | ||
dump(&ten_int); | ||
printf("=== delete [0] element \n"); | ||
delete(&ten_int, 0); | ||
dump(&ten_int); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
#include "LinkList.h" | ||
|
||
void CreateListHead(LinkList *&L,ElemType a[],int n) | ||
{ | ||
int i; | ||
LinkList *s; | ||
L = (LinkList *)malloc(sizeof(LinkList)); | ||
L->next = NULL; | ||
for(i = 0;i < n;i++) | ||
{ | ||
s=(LinkList*)malloc(sizeof(LinkList)); | ||
s->data = a[i]; | ||
s->next = L->next; | ||
L->next = s; | ||
} | ||
} | ||
void CreateListTail(LinkList *&L,ElemType a[],int n) | ||
{ | ||
int i; | ||
LinkList * s,* r; | ||
L = (LinkList *)malloc(sizeof(LinkList)); | ||
r = L; | ||
for(i = 0;i < n;i++) | ||
{ | ||
s = (LinkList *)malloc(sizeof(LinkList)); | ||
s->data = a[i]; | ||
r->next = s; | ||
r = s; | ||
} | ||
r->next = NULL; | ||
} | ||
void InitList(LinkList *&L) | ||
{ | ||
L=(LinkList *)malloc(sizeof(LinkList)); | ||
L->next = NULL; | ||
} | ||
void DestroyList(LinkList *&L) | ||
{ | ||
LinkList * pre = L,*p = L->next; | ||
while(p!=NULL) | ||
{ | ||
free(pre); | ||
pre = p; | ||
p = L->next; | ||
} | ||
free(pre); | ||
} | ||
bool ListEmpty(LinkList *L) | ||
{ | ||
return(L->next==NULL); | ||
} | ||
int ListLength(LinkList *L) | ||
{ | ||
int n = 0; | ||
LinkList * p = L; | ||
while(p->next!=NULL) | ||
{ | ||
n++; | ||
p=p->next; | ||
} | ||
return(n); | ||
} | ||
void ShowList(LinkList *L) | ||
{ | ||
LinkList * p = L->next;//Ö¸Ïò¿ªÊ¼½Úµã | ||
while(p!=NULL) | ||
{ | ||
printf(" %c ",p->data); | ||
p = p->next; | ||
} | ||
printf("\n"); | ||
} | ||
bool GetListElem(LinkList *L,int i,ElemType &e) | ||
{ | ||
int j = 0; | ||
LinkList *p = L; | ||
while(j<i&&p!=NULL) | ||
{ | ||
j++; | ||
p=p->next; | ||
} | ||
if(p==NULL) | ||
return false; | ||
else | ||
{ | ||
e=p->data; | ||
return true; | ||
} | ||
} | ||
int LocateElem(LinkList*L,ElemType e) | ||
{ | ||
int i=1; | ||
LinkList *p = L->next; | ||
while(p!=NULL&&p->data!=e){ | ||
p=p->next; | ||
i++; | ||
} | ||
if(p==NULL) | ||
{ | ||
return(0); | ||
} | ||
else | ||
return(i); | ||
} | ||
bool ListInsert(LinkList *&L,int i,ElemType e) | ||
{ | ||
int j=0; | ||
LinkList *p =L,*s; | ||
while(j<i-1&&p!=NULL) | ||
{ | ||
j++; | ||
p=p->next; | ||
} | ||
if(p==NULL) | ||
{ | ||
return false; | ||
} | ||
else | ||
{ | ||
s= (LinkList*)malloc(sizeof(LinkList)); | ||
s->data = e; | ||
s->next = p->next; | ||
p->next = s; | ||
return true; | ||
} | ||
} | ||
bool ListDelete(LinkList *&L,int i,ElemType &e) | ||
{ | ||
int j=0; | ||
LinkList * p =L,*q; | ||
while(j<i-1&&p!=NULL) | ||
{ | ||
j++; | ||
p=p->next; | ||
} | ||
if(p==NULL) | ||
return false; | ||
else | ||
{ | ||
q=p->next; | ||
if(q==NULL) | ||
return false; | ||
e=q->data; | ||
p->next=q->next; | ||
free(q); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef LINKLIST_H | ||
#define LINKLIST_H | ||
|
||
#include <stdlib.h> | ||
#include <stdio.h> | ||
typedef char ElemType; | ||
typedef struct LNode | ||
{ | ||
ElemType data; | ||
struct LNode*next; | ||
}LinkList; | ||
|
||
void CreateListHead(LinkList *&L,ElemType a[],int n); | ||
void CreateListTail(LinkList *&L,ElemType a[],int n); | ||
void InitList(LinkList *&L); | ||
void DestroyList(LinkList *&L); | ||
bool ListEmpty(LinkList *L); | ||
int ListLength(LinkList *L); | ||
void ShowList(LinkList *L); | ||
bool GetListElem(LinkList *L,int i,ElemType &e); | ||
int LocateElem(LinkList*L,ElemType e); | ||
bool ListInsert(LinkList *&L,int i,ElemType e); | ||
bool ListDelete(LinkList *&L,int i,ElemType &e); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include"LinkList.h" | ||
char array1[]= {'a','b','c','b','a'}; | ||
bool isPalindrome(LinkList * list,int length); | ||
int main() | ||
{ | ||
LinkList * list; | ||
|
||
int length = sizeof(array1)/sizeof(array1[0]); | ||
InitList(list); | ||
CreateListTail(list,array1,length);//用尾插法创建一个单链表 | ||
if(isPalindrome(list,length)) | ||
printf("isPalindrome\n"); | ||
else | ||
printf("isNotPalindrome\n"); | ||
return 0; | ||
} | ||
bool isPalindrome(LinkList * list,int length) | ||
{ | ||
int i; | ||
char buff1[length],buff2[length]; | ||
ElemType e; | ||
for(i=1; i<=length; i++) | ||
{ | ||
GetListElem(list,i,e);//遍历获取链表元素并放入数组中 | ||
buff1[i-1]=e;//正向数组 | ||
buff2[length-i]=e;//反向数组 | ||
} | ||
i=0; | ||
while(i<=length) { | ||
if(buff1[i]==buff2[i])//比较 | ||
{ | ||
i++; | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
Oops, something went wrong.