1,顺序表
线性表的顺序储存结构
#define MaxSize 100
typedef int ElemType;
typedef struct{
ElemType data[MaxSize];
int length;
}SqList;
顺序表的初始化
void InitList(SqList &L){
L.length = 0;
}
取顺序表中的第i个元素
int GetElem(Sqlist L, int i, ElemType &e){
if(i<1 || i>L.length)
return 0;
else{
e = L.data[i-1];
return 1;
}
}
顺序表的查找
int Locate(SqList L, ElemType x){
int i=0;
for(i=0;i<L.length, ++i){
if(L.data[i] = x)
return i+1;
}
return 0;
顺序表的插入
int ListInsert(SqList &L, int i, ElemType x){
int j=0;
if(i<1||i>L.length)
return 0;
for(j=L.length; j>i; j--)
L.data[j] = L.data[j-1];
L.data[i-1] = x;
L.length++;
}
顺序表的删除
int ListDelete(Sqlist &L, int i){
int j=0;
if(i<1||i>L.length)
return 0;
for(j=i; j<L.length; j++)
L.data[j-1] = L.data[j];
L.length--;
return 1;
}
顺序表的合并
void merge(Sqlist A, Sqlist B, Sqlist &C){
int i,j,k=0;
while(i<A.length && j<B.length){
if(A.data[i]<B.data[i]){
C.data[k] = A.data[i];
i++; k++;
}
else if(A.data[i]>B.data[j]){
C.data[k] = B.data[j];
j++; k++;
}
else{
C.data[k] = A.data[i];
i++; k++;
C.data[k] = B.data[j];
j++; k++;
}
}
while(i<A.length){
C.data[k] = A.data[i];
i++;k++;
}
while(j<B.length){
C.data[k] = B.data[i];
j++;k++;
}
C.length = k;
}
2,单链表
单链表的结构
typedef int ElemType;
typedef struct node{
ElemType data;
struct node *next;
}SLink,*linkedlist;
单链表的初始化
void InitList(){
SLink *L;
L = (SLink *)malloc(sizeof(SLink));
if (L == NULL)
printf("申请空间失败");
L->next = NULL;
}
单链表的销毁
void DestroyList(SLink* &L){
SLink * pre = L, *p=pre->next;
while(p!=NULL){
free(pre);
pre = p;
p = p->next;
}
free(pre);
}
单链表的查找
int Locate(SLink *L, ElemType e){
SLink *p = L->next;
int j=0;
while(p!=NULL && p->data!=e){
p = p->next;
j++;
}
if(p == NULL)
return 0;
else
return j;
}
单链表插入元素
linkedlist InsElem(linkedlist L, ElemType x, int i){
int j=0;
SLink *p = L, *s;
if(i<=0) return 0;
while(p!=NULL && j<i-1){
j++;
p = p->next;
}
if(p == NULL) return -1;
else{
s = (SLink *)malloc(sizeof(SLink));
s->data = x;
s->next = p->next;
p->next = s;
return L;
}
}
单链表删除元素
linkedlist DelElem(SLink *&L, int i){
int j=0;
SLink *p = L, *q;
if(i<=0) return 0;
while(p!=NULL && j<i-1){
j++;
p = p->next;
}
if(p == NULL) return 0;
else{
q = p->next;
if(q == NULL) return 0;
else{
p->next = q->next;
free(q);
return L;
}
}
}
创建单链表
linkedlist CreateList(){
SLink *s,*tc;
ElemType x;
int i=0;
L = (SLink *)malloc(sizeof(SLink));
tc = L;
while(scanf("%d",&x) != EOF){
s = (SLink *)malloc(sizeof(SLink));
s->data = x;
tc->next = s;
tc = s;
}
tc->next = NULL;
return L;
}
单链表的合并
void merge(SLink *ha, SLink *hb, SLink *hc){
SLink *pa=ha->next,*pb=hb->next,*tc;
hc = ha;
tc = hc;
free(hb);
while(pa!=NULL && pb!=NULL){
if(pa->data < pb->data){
tc->next = pa;
tc = pa;
pa = pa->next;
}
else if(pa->data > pb->data){
tc->next = pb;
tc = pb;
pb = pb->next;
}
else{
tc->next = pa;
tc = pa;
pa = pa->next;
tc->next = pb;
tc = pb;
pb = pb->next;
}
}
tc->next = NULL;
if(pa!=NULL)
tc->next = pa;
if(pb!=NULL)
tc->next = pb;
}
顺序表的逆置
#define MAXSIZE 100
typedef int ElemType;
typedef struct{
ElemType data[MAXSIZE];
int length;
}Sqlist;
void creat_sqlist(Sqlist *L,int n){
int i=0;
L->length = n;
printf("\n input %d data: ", n);
while(i<n){
scanf("%d", &L->data[i]);
i++;
}
}
void reverse_sqlist(Sqlist *L){
int i=0,j=0,n=0,t=0;
n = L->length;
if(n==0 && n==1) return;
j = n-1;
while(i<j){
t = L->data[i];
L->data[i] = L->data[j];
L->data[j] = t;
i++;
j--;
}
}
void print_sqlist(Sqlist *L){
int i=0, n=0;
n = L->length;
printf("\n output %d data: ", n);
while(i<n){
printf("%d", L->data[i]);
i++;
}
}
int main(){
Sqlist L;
int n;
printf("\n input n: ");
scanf("%d", &n);
creat_sqlist(&L,n);
reverse_sqlist(&L);
print_sqlist(&L);
}
约瑟夫环
#include
typedef int ElemType;
typedef struct node{
ElemType data;
struct node *next;
}SLink, *linkedList;
void initList() {
SLink *L;
L = (SLink *)malloc(sizeof(SLink));
L->next = NULL;
}
linkedList creatLink(ElemType n) {
linkedList L;
L = (SLink *)malloc(sizeof(SLink));
L->next = NULL;
for (int i = 1; i <= n; ++i) {
linkedList p;
p = (SLink *)malloc(sizeof(SLink));
p->data = i;
p->next = L->next;
L->next = p;
}
return L;
}
linkedList delElem(linkedList L, ElemType i) {
int j = 0;
linkedList p, q;
p = L;
while (p!=NULL && j<i-1) {
j++;
p = p->next;
}
q = p->next;
p->next = q->next;
return L;
}
int main() {
linkedList L;
int n, k, num = 0;
printf("\n input n: ");
scanf("%d", &n);
L = creatLink(n);
printf("\n input k: ");
scanf("%d", &k);
while (true)
{
if (num > n - 1) break;
if (n - num < k) break;
L = delElem(L, k);
num++;
L = creatLink(n-num);
}
printf("\n 次数:%d", num);
return 1;
}