美文网首页
小算法总结

小算法总结

作者: 刘威振勇敢的心 | 来源:发表于2014-10-16 16:48 被阅读0次
  1. Reverse any integer using c program, for example 21 --> 12
#include <stdio.h>
int main()
{
        int num, r, reverse = 0;
        printf("Enter any number:");
        scanf("%d", &num);
        while(num)
        {
            r = num % 10;
            reverse = reverse*10 + r;
            num /= 10;
        }
        printf("Reversed of number: %d\n", reverse);
        return 0;
}
  1. 线性表的简单实现
#include <stdio.h>
#include <string.h>

#define ERROR 0
#define OK 1

// 学生对象
struct STU
{
    char name[20];
    char stuno[10];
    int age;
    int score;
} stu[50];

// 线性表
struct LIST
{
    struct STU stu[50];
    int length;
} L;

// 打印线性表
int printlist(struct LIST L)
{
    int i;
    printf("name\tstuno\tage\tscore\n");
    for(i=0; i<L.length; i++)
        printf("%s\t%s\t%d\t%d\n",  L.stu[i].name,  L.stu[i].stuno, L.stu[i].age,  L.stu[i].score);
    printf("\n");
}

// 在线性表L的第i(i是从1开始的)个位置插入学生对象e
int listinsert(struct LIST *L, int i, struct STU e)
{
    struct STU *p,*q;
    if (i < 1 || i > L->length+1) return ERROR;
    q = &(L->stu[i-1]); // q指向要插入的位置
    for(p = &L->stu[L->length-1]; p>=q; --p) 
        *(p+1)=*p; // q后面所有的元素往后面移
    *q=e;
    ++L->length;
    return OK;
}/*ListInsert Before i */


int main()
{
    struct STU e;
    L.length=0;

    strcpy(e.name,"刘威震");
    strcpy(e.stuno,"100001");
    e.age=80;
    e.score=1000;
    listinsert(&L,1,e);

    printlist(L);
    printf("List length now is  %d.\n\n",L.length);

    strcpy(e.name,"王小宁");
    strcpy(e.stuno,"100002");
    e.age=80;
    e.score=1000;
    listinsert(&L,1,e);

    printlist(L);
    printf("List length now is  %d.\n\n",L.length);

    getchar();
    return 0;
}
  • 线性表的简单实现二
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<string.h>

#define ERROR 0
#define OK 1
#define EQUAL 1

struct STU{
    char name[20];
    char stuno[10];
    int age;
    int score;
}stu[50];
typedef struct STU ElemType;

struct LIST
{
    ElemType elem[50];
    int length;
};
typedef struct LIST List;


int init(List **L)
{
    *L=(List *)malloc(sizeof(List));
    (*L)->length=0;
}

int ListLength(List *L)
{
    return L->length;
}

void GetElem(List L,int i,ElemType *e)
{
    *e=L.elem[i];
}
int EqualList(ElemType *e1,ElemType *e2)
{
    if (strcmp(e1->name,e2->name))
        return 0;
    if (strcmp(e1->stuno,e2->stuno))
        return 0;
    if (e1->age!=e2->age)
        return 0;
    if (e1->score!=e2->score)
        return 0;
    return 1;
}
int LocateElem(List *La,ElemType e,int type)
{
    int i;
    switch (type)
    {
    case EQUAL:
        for(i=0;i<La->length;i++)
            if(EqualList(&La->elem[i],&e))
                return 1;
        break;
    default:
        break;
    }
    return 0;
}
void UnionList(List *La, List *Lb)
{
    int La_len,Lb_len;
    int i;
    ElemType e;

    La_len=ListLength(La);  Lb_len=ListLength(Lb);
    for(i=0;i<Lb_len;i++)
    {
        GetElem(*Lb,i,&e);
        if(!LocateElem(La,e,EQUAL))
            ListInsert(La,++La_len,e);
    }

}

int printlist(List L)
{
    int i;
    printf("name       stuno        age     score\n");
    for(i=0;i<L.length;i++)
        printf("%-10s %s\t%d\t%d\n",  L.elem[i].name,  L.elem[i].stuno,
        L.elem[i].age,  L.elem[i].score);
    printf("\n");
}

int ListInsert(List *L,int i,struct STU e)
{
    struct STU *p,*q;
    if (i<1||i>L->length+1) return ERROR;
    q=&(L->elem[i-1]);
    for(p=&L->elem[L->length-1];p>=q;--p)
        *(p+1)=*p;
    *q=e;
    ++L->length;
    return OK;
}/*ListInsert Before i */


int main()
{
    struct STU e;
    List *La,*Lb;

    printf("\n\n-------------------List Demo is running...----------------\n\n");
    printf("First is InsertList function.\n");
    init(&La);

    strcpy(e.name,"stu1");
    strcpy(e.stuno,"100001");
    e.age=80;
    e.score=1000;
    ListInsert(La,1,e);
    strcpy(e.name,"stu2");
    strcpy(e.stuno,"100002");
    e.age=80;
    e.score=1000;
    ListInsert(La,2,e);

    printlist(*La);
    printf("List A length now is  %d.\n\n",La->length);
    getch();

    strcpy(e.name,"stu3");
    strcpy(e.stuno,"100003");
    e.age=80;
    e.score=1000;
    ListInsert(La,3,e);

    printlist(*La);
    printf("List A length now is  %d.\n\n",La->length);
    getch();

    init(&Lb);

    strcpy(e.name,"zmofun");
    strcpy(e.stuno,"100001");
    e.age=80;
    e.score=1000;
    ListInsert(Lb,1,e);
    strcpy(e.name,"bobjin");
    strcpy(e.stuno,"100002");
    e.age=80;
    e.score=1000;
    ListInsert(Lb,2,e);

    strcpy(e.name,"stu1");
    strcpy(e.stuno,"100001");
    e.age=80;
    e.score=1000;
    ListInsert(Lb,3,e);

    printlist(*Lb);
    printf("List B length now is  %d.\n\n",Lb->length);
    getch();

    printf("Second is UnionList function.\n");
    printf("Now union List A and List B.....\n");
    UnionList(La,Lb);
    printlist(*La);
    printf("List A length now is  %d.\n\n",La->length);
    getch();
    printf("\n\n\n\n\n\nWelcome to visit http://zmofun.heha.net !\n\n\n\n\n\n\n");
    return 0;
}

相关文章

  • 小算法总结

    Reverse any integer using c program, for example 21 --> 1...

  • 算法:二分查找

    前言:最近小编在看《算法图解》,将会总结一系列算法相关的文章。关于算法的系列文章,小编将准备分“三步”来编写: 第...

  • 《啊哈!算法》小总结

    这几天断断续续看完了《啊哈!算法》,做点小复笔记 冒泡排序:比较相邻的元素,如果顺序不一样就将他们调换顺序,直到完...

  • 排序算法小总结

    排序算法时间复杂度冒泡排序O(n2)选择排序O(n2)插入排序O(n2)希尔排序O(n1.5)快速排序O(N*lo...

  • iOS算法总结-堆排序

    iOS算法总结-堆排序 iOS算法总结-堆排序

  • iOS算法总结-冒泡排序

    iOS算法总结-冒泡排序 iOS算法总结-冒泡排序

  • Apriori

    Apriori算法原理总结-刘建平FP Tree算法原理总结-刘建平PrefixSpan算法原理总结-刘建平用Sp...

  • 算法--leetcode-283-移动零

    相当于是使用 for 进行交换的一个小技巧的练习,后面会给出一些算法的小技巧,都是总结的一些算法的小技巧。

  • 剑指offer算法题006:旋转数组的最小数字

    小编在求职找找工作期间剑指offer上的算法题刷了很多遍,并且每道题小编当时都总结了一种最适合面试时手撕算法的最优...

  • 剑指offer算法题027:字符串的排列

    小编在求职找找工作期间剑指offer上的算法题刷了很多遍,并且每道题小编当时都总结了一种最适合面试时手撕算法的最优...

网友评论

      本文标题:小算法总结

      本文链接:https://www.haomeiwen.com/subject/wrtptttx.html