1、链表 的创建 加入 删除 遍历 等 ······
#include <stdio.h> #include <stdlib.h> typedef struct student { int num; struct student *next; }node; node* creat(); int length(node* n); void print(node* n); node* insert(node* n,int num); node* del(node* n,int num); node* sort(node* n); int main() { node* p; p= (node *)creat(); print(p); p=insert(p,5); p=del(p,1); print(p); p= sort(p); print(p); return 0; } node* creat() { node *head; node *p,*q; int a,flag=1; head=(node*)malloc(sizeof(node)); head->next=NULL; q=head; while(flag) { printf("input num=\n"); scanf("%d",&a); if(a) { p=(node*)malloc(sizeof(node)); q->next=p; p->num=a; q=p; } else flag=0; } head=head->next; p->next=NULL; return head; } int length(node* n) { int len=0; node* p=(node*)malloc(sizeof(node)); p=n; while(p!=NULL) { p=p->next; len++; } return len; } void print(node* n) { node* p=(node*)malloc(sizeof(node)); p=n; while(p!=NULL) { printf("num=%d\n",p->num); p=p->next; } } node* insert(node* n,int num) { node* p=(node*)malloc(sizeof(node)); p=n; while(p->next!=NULL) { p=p->next; } node* q=(node*)malloc(sizeof(node)); q->num=num; q->next=NULL; p->next=q; return n; } node* del(node* n,int num) { node* p=(node*)malloc(sizeof(node)); node*q; p=n; while((p->num!=num)&&(p->next!=NULL)) { q=p; p=p->next; } if(p->num==num) { if(p!=n) { q->next=p->next; free(p); p=NULL; } else { n=p->next; free(p); } } else printf("not find this number.\n"); return n; } node* sort(node* n) { node *p,*q; int i,j,temp; int len=length(n); if(n==NULL||n->next==NULL) return n; for(i=0;i<len-1;i++) { p=n; for(j=0;j<len-1-i;j++) { if(p->num>p->next->num) { q=p->next; temp=q->num; q->num=p->num; p->num=temp; } p=p->next; } } return n; }
2、可变长度的数组 添加 删除 增长
#include <stdio.h> #include<stdlib.h> typedef struct { int size; int* array; }Array; //int bluck 5; Array creat_arr(int size); void delete_arr(Array *a); int arr_size(const Array *a); int arr_get(const Array *a,int index); void arr_set(Array *a,int index,int value); void arr_bigger(Array *a,int bluck); int main(void) { Array a=creat_arr(10); int cnt=0; while(1) { int b; scanf("%d",&b); if(b!=-1) arr_set(&a,cnt++, b); else break; } int i=0; while(cnt--) { printf("a[%d]=%d\n",i++,arr_get(&a,i)); } delete_arr(&a); return 0; } Array creat_arr(int size) { Array a; a.size=size; a.array=(int*)malloc(size*sizeof(int)); return a; } void delete_arr(Array *a) { free(a->array); a->array=NULL; a->size=0; } int arr_size(const Array *a) { return a->size; } int arr_get(const Array *a,int index) { return a->array[index]; } void arr_set(Array *a,int index,int value) { if(index==arr_size(&a)) arr_bigger(&a,5); a->array[index]=value; } void arr_bigger(Array *a,int bluck) { int size=arr_size(&a); int *b=(int*)malloc(sizeof(int)*(size+bluck)); // strcpy(b,a->array,size); int i; for(i=0;i<size;i++) { b[i]=a->array[i]; } free(a->array); a->array=b; a->size=size+bluck; }
3、、、判断压栈出栈
#include <iostream> using namespace std; #define MAX 1000 int arr[MAX]={0}; int front = -1; void stk_push(int a) { if(front+1<MAX) arr[++front] = a; } int stk_top() { if(front>=0) return arr[front]; } void stk_pop() { if(front>=0) front--; } bool empty() { if(front<0)return true; else return false; } bool IsPushPopSeq(int intput[],int len1,int output[],int len2) { int index1 = 0,index2 = 0; while(index2<len2) { if(empty() || stk_top() != output[index2] && index1<len1) //判断压站出站 { stk_push(intput[index1++]); } else if(!empty() && stk_top() == output[index2]) { index2++; stk_pop(); } else return false; } return true; } int main() { cout << "Hello World!" << endl; int a[]={5,2,3,1,4},a1[]={1,2,3,4,5}; cout<<IsPushPopSeq(a1,5,a,5)<<endl; return 0; }
4、
、约瑟夫问题
#include<stdio.h> #include<stdlib.h> typedef struct note { int data; struct note* next; }node; node* creat_s(int num) { node*p=(node*)malloc(sizeof(node)); node*w=p; int i; for(i=1;i<=num;i++) { node*q=(node*)malloc(sizeof(node)); //约瑟夫问题 费劲 q->data=i; q->next=NULL; w->next=q; w=q; } node*q; q=p; p=p->next; free(q); w->next=p; return p; } //node*insert(node*p,int num) //{ // return p; //} node*del(node*p,int num) { node*now,*pre; pre=p; now=p->next; while(now->data!=num) { pre=now; now=now->next; } printf("kell the num:%d\n",now->data); pre->next=now->next; free(now); return pre->next; } int main() { int a,k=3; node*p=NULL; printf("please input a num\n"); scanf("%d",&a); p=creat_s(a); node*w=p; while(w->next!=w) { node*q; q=w->next->next; int num=q->data; w=del(w,num); } p=w; printf("the least people %d\n",p->data); return 0; }
5 栈
#include <stdio.h> #include <stdlib.h> typedef struct dui { int data; struct dui* next; }node; typedef struct lie { node *frist; node * rear; }duilie; duilie* creat(); duilie* push(duilie*p,int num); //栈 duilie* pop(duilie*p,int*num); void dest(duilie*p); void print(duilie* p); int main(void) { int a; duilie *p=creat(); p=push(p,1); p=push(p,2); p=push(p,3); p=push(p,4); print(p); p=pop(p,&a); print(p); // dest(p); return 0; } duilie *creat() { duilie* p; p->frist=NULL; p->rear=NULL; return p; } duilie* push(duilie*p,int num) { node*q=(node*)malloc(sizeof(node)); q->data=num; q->next=NULL; if(p->frist==NULL) { p->frist=q; p->rear=q; } else { p->frist->next=q; p->frist=q; } return p; } duilie* pop(duilie*p,int* num) { node*a; if(p->frist==NULL) { printf("meiyou shuju sb\n"); } else { *num=p->frist->data; printf("chuzhan %d\n",*num); a=p->frist; if(p->rear==p->frist) { p->frist=NULL; p->rear=NULL; } else { node*b=p->rear; while(b->next!=p->frist) { b=b->next; } p->frist=b; p->frist->next=NULL; free(a); } } return p; } void dest(duilie*p) { node*a=p->frist; node*b=NULL; while(a!=NULL) { b=a; a=a->next; free(b); b=NULL; } } void print(duilie* p) { node*a=p->rear; while(a!=NULL) { printf("%d\n",a->data); a=a->next; } }
7、、队列
#include <stdio.h> #include <stdlib.h> typedef struct dui { int data; struct dui* next; }node; typedef struct lie { node *frist; node * rear; }duilie; duilie* creat(); duilie* in(duilie*p,int num); duilie* out(duilie*p); void dest(duilie*p); void print(duilie* p); int main(void) { duilie *p=creat(); p=in(p,1); p=in(p,2); p=in(p,3); p=in(p,4); print(p); p=out(p); print(p); // dest(p); return 0; } duilie *creat() { duilie* p; p->frist=NULL; p->rear=NULL; return p; } duilie* in(duilie*p,int num) { node*q=(node*)malloc(sizeof(node)); //队列 q->data=num; q->next=NULL; if(p->frist==NULL) { p->frist=q; p->rear=q; } else { p->rear->next=q; p->rear=q; } return p; } duilie* out(duilie*p) { node*a; int num; if(p->frist==NULL) { printf("meiyou shuju sb\n"); } else { num=p->frist->data; printf("shuzhan %d\n",num); a=p->frist; if(p->rear==p->frist) { p->frist=NULL; p->rear=NULL; } else { p->frist=p->frist->next; free(a); } } return p; } void dest(duilie*p) { node*a=p->frist; node*b=NULL; while(a!=NULL) { b=a; a=a->next; free(b); b=NULL; } } void print(duilie* p) { node*a=p->frist; while(a!=NULL) { printf("%d\n",a->data); a=a->next; } }