<Sequence table> Exercise "Data Structure (C Language Version)"

Table of contents

topic:

【Programming question】

1. Add, delete, check and modify the sequence table

2.. Merge two sorted arrays. OJ link

3. Remove duplicates in sorted array. OJ link 

Parse:

Postscript: ●Due to the limited level of the author, the article will inevitably contain errors, please readers to correct, slang into articles, sincerely hope for advice!

                                                                                           ——By Author: Xinxiao Guzhi


topic:

【Programming question】

1. Add, delete, check and modify the sequence table

2.. Merge two sorted arrays. OJ link

3. Remove duplicates in sorted array. OJ link 

Parse:

1.

// SeqList.h
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
 
typedef int SLDateType;
typedef struct SeqList
{
	SLDateType* a;
	size_t size;
	size_t capacity; // unsigned int
}SeqList;
 
// 对数据的管理:增删查改 
void SeqListInit(SeqList* ps);
void SeqListDestory(SeqList* ps);
 
void SeqListPrint(SeqList* ps);
void SeqListPushBack(SeqList* ps, SLDateType x);
void SeqListPushFront(SeqList* ps, SLDateType x);
void SeqListPopFront(SeqList* ps);
void SeqListPopBack(SeqList* ps);
 
// 顺序表查找
int SeqListFind(SeqList* ps, SLDateType x);
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x);
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos);
// SeqList.c
#include "SeqList.h"
 
void SeqListInit(SeqList* ps)
{
	assert(ps);
 
	ps->a = NULL;
	ps->size = 0;
	ps->capacity = 0;
}
 
void SeqListDestory(SeqList* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->size = ps->capacity = 0;
}
 
void SeqListPrint(SeqList* ps)
{
	assert(ps);
 
	for (size_t i = 0; i < ps->size; ++i)
	{
		printf("%d ", ps->a[i]);
	}
 
	printf("%\n");
}
 
void CheckCacpity(SeqList* ps)
{
	if (ps->size == ps->capacity)
	{
		size_t newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		ps->a = (SLDateType*)realloc(ps->a, newcapacity*sizeof(SLDateType));
		ps->capacity = newcapacity;
	}
}
 
// 以下几个接口先讲不复用Insert和Erase的实现,最后再讲复用实现
void SeqListPushBack(SeqList* ps, SLDateType x)
{
	//assert(ps);
	//CheckCacpity(ps);
 
	//ps->a[ps->size] = x;
	//ps->size++;
 
	SeqListInsert(ps, ps->size, x);
}
 
void SeqListPushFront(SeqList* ps, SLDateType x)
{
	assert(ps);
 
	/*CheckCacpity(ps);
 
	size_t end = ps->size;
	while (end > 0)
	{
		ps->a[end] = ps->a[end - 1];
		--end;
	}
 
	ps->a[0] = x;
	++ps->size;*/
 
	SeqListInsert(ps, 0, x);
}
 
void SeqListPopFront(SeqList* ps)
{
	assert(ps);
 
	//size_t start = 0;
	//while (start < ps->size-1)
	//{
	//	ps->a[start] = ps->a[start + 1];
	//	++start;
	//}
	//size_t start = 1;
	//while (start < ps->size)
	//{
	//	ps->a[start-1] = ps->a[start];
	//	++start;
	//}
 
	//--ps->size;
	SeqListErase(ps, 0);
}
 
void SeqListPopBack(SeqList* ps)
{
	assert(ps);
 
	//ps->a[ps->size - 1] = 0;
	//ps->size--;
	SeqListErase(ps, ps->size-1);
}
 
int SeqListFind(SeqList* ps, SLDateType x)
{
	for (size_t i = 0; i < ps->size; ++i)
	{
		if (ps->a[i] == x)
		{
			return i;
		}
	}
 
	return -1;
}
 
// 顺序表在pos位置插入x
void SeqListInsert(SeqList* ps, size_t pos, SLDateType x)
{
	assert(ps);
	assert(pos <= ps->size);
 
	CheckCacpity(ps);
 
	//int end = ps->size - 1;
	//while (end >= (int)pos)
	//{
	//	ps->a[end + 1] = ps->a[end];
	//	--end;
	//}
 
	size_t end = ps->size ;
	while (end > pos)
	{
		ps->a[end] = ps->a[end - 1];
		--end;
	}
 
 
	ps->a[pos] = x;
	ps->size++;
}
 
// 顺序表删除pos位置的值
void SeqListErase(SeqList* ps, size_t pos)
{
	assert(ps && pos < ps->size);
 
	//size_t start = pos;
	//while (start < ps->size-1)
	//{
	//	ps->a[start] = ps->a[start + 1];
	//	++start;
	//}
 
	size_t start = pos+1;
	while (start < ps->size)
	{
		ps->a[start-1] = ps->a[start];
		++start;
	}
 
	ps->size--;
}

2.

Solution 1:

/*
解题思路:从最大值开始合并,所以合并从两个数组的末尾元素开始合并,依次把最大的元素放到末尾即可。
*/
void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n){
    //end1:nums1的末尾
    //end2:nums2的末尾
    //end:合并之后整个数组的末尾
     int end1 = m-1;
     int end2 = n-1;
     int end = m+n-1;
    
    while(end1 >= 0 && end2 >= 0)
    {   //选出尾最大的元素,存放到整个数组的末尾
        //每存放一个元素,尾向前移动一个位置
        if(nums1[end1] > nums2[end2])
        {
            nums1[end--] = nums1[end1--]; 
        }
        else
        {
            nums1[end--] = nums2[end2--];
        }
    }
    //剩余元素依次向末尾存放
    while(end1 >= 0)
    {
        nums1[end--] = nums1[end1--];
    }
    
    while(end2 >= 0)
    {
        nums1[end--] = nums2[end2--];
    }
}

Solution 2:

int cmp(int* a, int* b)
 {
    return *a - *b;
 }

void merge(int* nums1, int nums1Size, int m, int* nums2, int nums2Size, int n)
 {
    for (int i = 0; i != n; ++i) 
{
        nums1[m + i] = nums2[i];
    }
    qsort(nums1, nums1Size, sizeof(int), cmp);
}

3.

Solution 1:

/*
解题思路:大致思路同上题,用非重复的元素去覆盖重复元素
*/
int removeDuplicates(int* nums, int numsSize){
    int src1 = 0, src2 = 1;
    int dst = 0;
    // 跟上题的思路一致,相同的数只保留一个,依次往前放
    while(src2 < numsSize)
    {
        nums[dst] = nums[src1];
        ++dst;
		//如果两个指针指向的元素不重复,则指针同时向后移动
        if(nums[src1] != nums[src2])
        {
            ++src1;
            ++src2;
        }
        else
        {
            //如果重复,找到第一个不重复的元素
            while(src2 < numsSize && nums[src1] == nums[src2])
            {
                ++src2;
            }
            //更新指针
            src1 = src2;
            ++src2;
        }
    }
 
    if(src1 < numsSize)
    {
            nums[dst] = nums[src1];
            ++dst;
    }
 
 
    return dst;
}

Solution 2:

int removeDuplicates(int* nums, int numsSize)
{
    if (numsSize == 0)
    {
        return 0;
    }
    int fast = 1, slow = 1;
    while (fast < numsSize)
    {
        if (nums[fast] != nums[fast - 1])
        {
            nums[slow] = nums[fast];
            ++slow;
        }
        ++fast;
    }
    return slow;
}

Postscript:
●Due to the limited level of the author, the article will inevitably contain errors, please readers to correct, slang into articles, sincerely hope for advice!

                                                                                           ——By Author: Xinxiao Guzhi

Guess you like

Origin blog.csdn.net/m0_57859086/article/details/124048749