手写代码总结

(1)strcpy

#include <stdio.h>

char *my_strcpy(char *pdest, const char *psrc)
{
	char *ret = pdest;	

	while (*psrc != '\0')
	{
		*pdest = *psrc;
		psrc++;
		pdest++;
	}
	*pdest = '\0';

	return ret;
}

int main(void)
{
	const char src[32] = {"supercarrydoinb!"};
	char dest[32] = {0};

	my_strcpy(dest, src);

	printf("dest: %s\n", dest);

	return 0;
}

(2)strcat

#include <stdio.h>

char *my_strcat(char *pdest, const char *psrc)
{
	char *ret = pdest;

	while (*pdest != '\0')
	{
		pdest++;
	}

	while (*psrc != '\0')
	{
		*pdest = *psrc;
		psrc++;
		pdest++;
	}
	*pdest = '\0';

	return ret;
}

int main(void)
{
	const char src[32] = {"supercarrydoinb!"};
	char dest[32] = {"MVP!"};
	
	my_strcat(dest, src);

	printf("dest:  %s\n", dest);

	return 0;
}

(3)strcmp

#include <stdio.h>

//'\0'ascii值为0  '0'ascii值为48
char my_strcat(const char *pstr1, const char *pstr2)
{	
	while (*pstr1 != '\0' && *pstr2 != '\0')
	{
		pstr1++;
		pstr2++;
	}

	return *(pstr1) - *(pstr2);
}

int main(void)
{
	const char str1[32] = {"supercarrydoinb!"};
	const char str2[32] = {"super"};
	char ret = 0;	

	ret = my_strcat(str1, str2);

	printf("ret = %d\n", ret);

	return 0;
}

(4)strlen

#include <stdio.h>

//size_t在ubuntu中定义为long unsigned int 
//		keil里面表示unsigned int
size_t my_strlen(const char *psrc)//一般表示数据的长度
{
	size_t ret = 0;	

	while (*psrc != '\0')
	{
		psrc++;
		ret++;
	}

	return ret;
}

int main(void)
{
	const char src[32] = {"supercarrydoinb!"};
	size_t len = 0;

	len = my_strlen(src);

	printf("len =  %ld\n", len);

	return 0;
}

(5)判断内存大小端

#include <stdio.h>

int main(void)
{
	int num = 0x11223344;
	char *p = (char *)&num;
	
	if (0x11 == *p)
	{
		printf("大端!\n");
	}
	else if (0x44 == *p)
	{
		printf("小端!\n");
	}

	return 0;
}

(6)链表逆序、删除链表倒数第n个节点、判断单向链表是否有环

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "LinkList.h"

//创建链表标签
LinkList *create_Linklist(void)
{
	LinkList *plist = malloc(sizeof(LinkList));
	if (NULL == plist)
	{
		perror("fail to malloc!\n");
		return NULL;
	}

	plist->phead = NULL;
	plist->clen = 0;

	return plist;
}

//判断链表是否为空
int is_empty_link(LinkList *plist)
{
	if (NULL == plist->phead)
	{
		return 1;
	}

	return 0;
}

//头插
int push_head_link(LinkList *plist, DataType Data)
{
	LinkNode *ptmpnode = malloc(sizeof(LinkNode));
	if (NULL == ptmpnode)
	{
		perror("fail to malloc!\n");
		return -1;
	}
	ptmpnode->pNext = NULL;
	ptmpnode->Data = Data;

	ptmpnode->pNext = plist->phead;
	plist->phead = ptmpnode;
	plist->clen++;

	return 0;
}

//尾插
int push_tail_link(LinkList *plist, DataType Data)
{
	LinkNode *plastnode = malloc(sizeof(LinkNode));
	if (NULL == ptmpnode)
	{
		perror("fail to malloc!\n");
		return -1;
	}
	plastnode->pNext = NULL;
	plastnode->Data = Data;

	if (is_empty_link(plist))
	{
		plist->phead = plastnode;
	}
	else
	{
		LinkNode *ptmpnode = plist->phead;
		
		while (ptmpnode->pNext != NULL)
		{
			ptmpnode = ptmpnode->pNext;
		}

		ptmpnode->pNext = plastnode;
	}
	plist->clen++;

	return 0;
}

//头删
int pop_head_link(LinkList *plist)
{
	if (is_empty_link(plist))
	{
		return -1;
	}

	LinkNode *ptmpnode = plist->phead;
	
	plist->phead = ptmpnode->pNext;
	free(ptmpnode);
	plist->clen--;

	return 0;
}

//尾删
int pop_tail_link(LinkList *plist)
{
	if (is_empty_link(plist))
	{
		return -1;
	}

	if (1 == plist->clen)
	{
		pop_head_link(plist);
	}
	else
	{
		LinkNode *ptmpnode = plist->phead;

		while (ptmpnode->pNext->pNext != NULL)
		{
			ptmpnode = ptmpnode->pNext;
		}

		free(ptmpnode->pNext);
		ptmpnode->pNext = NULL;
		plist->clen--;
	}

	return 0;
}

//遍历链表节点
void link_for_each(LinkList *plist)
{
	LinkNode *ptmpnode = plist->phead;

	while (ptmpnode != NULL)
	{
		printf("%d ", ptmpnode->Data);
		ptmpnode = ptmpnode->pNext;
	}
	putchar('\n');
}

//链表逆序
//断开链表,从原链表头开始向链表标签头插
void invert_link(LinkList *plist)
{
	LinkNode *ptmpnode = plist->phead;
	LinkNode *pinvertnode = NULL;

	plist->phead = NULL;//断开链表节点

	while (ptmpnode != NULL)
	{
		pinvertnode = ptmpnode;
		ptmpnode = ptmpnode->pNext;

		pinvertnode->pNext = plist->phead;//头插倒置
		plist->phead = pinvertnode;
	}
}

//删除链表第n个节点
//快慢指针相差n个位置
void pop_last_n_node(LinkList *plist, int n)
{
	if (is_empty_link(plist))
	{
		return;
	}

	LinkNode *pfast = plist->phead;
	LinkNode *pslow = NULL;
	int i = 0;
	
	for (i = 0; i < n; ++i)
	{
		if (pfast != NULL)
		{
			pfast = pfast->pNext;
		}
	}
	pslow = plist->phead;

	while (pslow->pNext != NULL)
	{
		pslow = pslow->pNext;
		pfast = pfast->pNext;
	}

	pslow->pNext = pslow->pNext->pNext;
	free(pslow->pNext);
	plist->clen--;
}

//判断链表是否有环
//快慢指针是否相遇或者快指针是否走到NULL
int judge_loop_link(LinkList *plist)
{
	if (is_empty_link(plist))
	{
		return -1;
	}

	LinkNode *pfast = plist->phead->pNext;
	LinkNode *pslow = plist->phead;

	while (pfast != pslow)
	{
		if (NULL == pfast)
		{
			return 0;
		}

		pfast = pfast->pNext->pNext;
		pslow = pslow->pNext;

		if (pfast == pslow)
		{
			return 1;
		}
	}
}

int main(void)
{
	

	return 0;
}

(9)创建双向循环链表

#include "DoubleLinkList.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
DouList *create_dou_link()//创建标签
{
    DouList *plist = NULL;
 
    plist = (DouList *)malloc(sizeof(DouList));
    if (NULL == plist)
    {
        perror("fail to malloc");
        return NULL;
    }
 
    plist->phead = NULL;
    plist->clen = 0;
 
    return plist;
}
 
int is_empty_dou_link(DouList *plist)//判断空链表
{
    if (NULL == plist->phead)
    {
        return 1;
    }
 
    return 0;
}
 
int push_head_dou_link(DouList *plist, DataType data)//头插
{
    DouNode *pnode = NULL;
 
    pnode = malloc(sizeof(DouNode));
    if (NULL == pnode)
    {
        perror("fail to malloc");
        return -1;
    }
 
    pnode->data = data;
    pnode->ppre = NULL;
    pnode->pnext = NULL;
 
    if (is_empty_dou_link(plist))//空链表直接插
    {
        plist->phead = pnode;
    }
    else
    {
        pnode->pnext = plist->phead;
        plist->phead->ppre = pnode;
        plist->phead = pnode;
    }
    plist->clen++;
 
    return 0;
}
 
int push_tail_dou_link(DouList *plist, DataType data)//头插
{
    DouNode *p = NULL;
    DouNode *pnode = NULL;
 
    pnode = malloc(sizeof(DouNode));
    if (NULL == pnode)
    {
        perror("fail to malloc");
        return -1;
    }
    pnode->data = data;
    pnode->ppre = NULL;
    pnode->pnext = NULL;
 
    if (is_empty_dou_link(plist))//空链表直接插
    {
        plist->phead = pnode;
    }
    else
    {   
        p = plist->phead;
        while (p->pnext != NULL)
        {
            p = p->pnext;
        }
 
        p->pnext = pnode;
        pnode->ppre = p;
    }
    plist->clen++;
 
    return 0;
}
 
int pop_head_dou_link(DouList *plist)//头删
{
    if (is_empty_dou_link(plist))//空链表直接结束程序
    {
        return -1;
    }
 
    DouNode *pfree = NULL;
    pfree = plist->phead;
 
    plist->phead = pfree->pnext;//标签指向第二个节点首地址
    if (plist->phead != NULL)//判断是否空链表
    {
        plist->phead->ppre = NULL;//将第二个节点的ppre变为NULL
    }
    free(pfree);
 
    plist->clen--;
 
    return 0;
}
 
int pop_tail_dou_link(DouList *plist)//尾删
{
    if (is_empty_dou_link(plist))//空链表程序结束
    {
        return -1;
    }
 
    DouNode *pfree = NULL;
 
    pfree = plist->phead;
 
    while (pfree->pnext)//指针指向最后一个节点
    {
        pfree = pfree->pnext;
    }
 
    if (pfree->ppre != NULL)//链表有两个以上节点
    {
        pfree->ppre->pnext = NULL;
    }
    else //链表只有一个节点
    {
        plist->phead = NULL;
    }
 
    free(pfree);
    plist->clen--;
 
    return 0;
}

//双向非回环链表首节点的前驱指针指向NULL 尾节点后继指针指向NULL
//创建回环的原理是遍历指针节点到尾节点,将尾节点的后继指针指向头结点
//将头结点的前驱节点指向尾节点
void loop_dou_link(DouList *plist)//将非回环链表改为双向回环链表
{
    DouNode *ptmpnode = plist->phead;

    while (ptmpnode->pnext != NULL)//将指针移动到末尾节点
    {
        ptmpnode = ptmpnode->pnext;
    }
 
    ptmpnode->pnext = plist->phead;
    plist->phead->ppre = ptmpnode;
}
 
void dou_link_for_remain(DouList *plist)//打印约瑟夫回环一次处理后链表中剩下的成员信息
{
    int i = 0;
    DouNode *ptmpnode = plist->phead;
 
    for (i = 0; i < plist->clen; i++)
    {
        printf("%d  ", ptmpnode->data.id);
        printf("%s  ", ptmpnode->data.name);
        printf("%d\n", ptmpnode->data.score);
        ptmpnode = ptmpnode->pnext;
    }
    printf("=========================\n");
}
 
DouNode *Joseph_loop(DouList *plist)//约瑟夫回环、实质是删除链表节点直到留下最后一个节点为止
{
    DouNode *pfreenode = NULL;//
    DouNode *ptmpnode = NULL;//指向回环
 
    ptmpnode = plist->phead;
 
    while (ptmpnode != ptmpnode->pnext)//判断回环是否只剩下一个节点
    {
        
        pfreenode = ptmpnode;//指向当前所在回环的位置
 
        pfreenode = pfreenode->pnext->pnext;//回环向后移动两个单位
        pfreenode->ppre->pnext = pfreenode->pnext;
        pfreenode->pnext->ppre = pfreenode->ppre; 
 
        ptmpnode = pfreenode->pnext;//记录要删除的回环的下一个位置,保证循环的延续
        if (pfreenode == plist->phead)//判断要删除的节点是否是表头后的第一个节点、若是,给表头接入要删除节点的下一个节点
        {
            plist->phead = ptmpnode;
        }
 
        free(pfreenode);
        plist->clen--;
        dou_link_for_remain(plist);//打印链表中剩下的节点信息
    }
 
    return ptmpnode;
}
 

(10)快速排序

#include <stdio.h>
#include <string.h>

void quicksort(int *p, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}

	int i = begin;
	int j = end;
	int key = p[begin];//基准值

	while (i < j)
	{
		while (p[j] >= key && i < j)
		{
			--j;
		}
		p[i] = p[j];

		while (p[i] <= key && i < j)
		{
			++i;
		}
		p[j] = p[i];
	}
	p[i] = key;

	quicksort(p, begin, i-1);
	quicksort(p, i+1, end);
}

int main(void)
{
	int buf[] = {1, -2, 3, 4, -5, -6, 7, 8, 9, 0};
	int len = sizeof(buf) / sizeof(buf[0]);

	quicksort(buf, 0, len-1);

	return 0;
}

(11)冒泡排序

#include <stdio.h>
#include <string.h>

void bubblesort(int *parray, int len)
{
	int i = 0;
	int j = 0;
	int tmp = 0;

	for (i = 0; i < len-1; ++i)
	{
		for (j = 0; j < len-1-i; ++j)
		{
			if (parray[j] > parray[j+1])
			{
				tmp = parray[j];
				parray[j] = parray[j+1];
				parray[j+1] = tmp;
			}
		}
	}

}

int main(void)
{
	int array[] = {1, 2, 3, -4, 5, -6, -7, 8, 9, 0};
	int len = sizeof(array) / sizeof(sizeof(array[0]));

	bubblesort(array, len);

	return 0;
}

(12)二分查找

#include <stdio.h>
#include <string.h>

int binarysearch(int *p, int len, int target)
{
	int left = 0;
	int right = len-1;
	int mid = 0;

	while (left <= right)
	{
		mid = left + (right - left) / 2;

		if (p[mid] = target)
		{
			return mid;	
		}

		if (p[mid] < target)
		{
			left = mid + 1;
		}
		else if (p[mid] > target)
		{
			right = mid -1;
		}
	}

	return -1;
}

int main(void)
{
	int array[] = {1 ,2, 3, 4, 5};
	int len = sizeof(array) / sizeof(array[0]);
	int target = 2;
	int result = 0;

	result = binarysearch(array, len, target);
	if (-1 == result)
	{
		printf("未找到!\n");
	}
	else
	{
		printf("%d在数组的第%d个位置\n", array[result], result);
	}

	return 0;
}

(13)斐波拉契数列第n项以及斐波拉契数列前n项的和、求n!(n阶乘)

#include <stdio.h>

int fibo(int n)
{
	if (1 == n || 2 == n)
	{
		return 1;
	}
	else
	{
		return fibo(n-1) + fibo(n-2);
	}
}

int fibosum(int n)
{
	if (1 == n || 2 == n)
	{
		return n;
	}
	else
	{
		return fibosum(n-1) + fibo(n);
	}
}

int factorial_n(int n)
{
	if (1 == n)
	{
		return 1;
	}

	return factorial_n(n-1) * n;
}
int main(void)
{
	int n = 5;
	int ret_fibo = 0;
	int ret_fibosum = 0;
	int ret_factorial = 0;

	ret_fibo = fibo(n);
	ret_fibosum = fibosum(n);
	ret_factorial = factorial_n(n);

	printf("fibo = %d, fibosum = %d\n", ret_fibo, ret_fibosum);
	printf("factorial_n = %d\n", ret_factorial);
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值