数据结构与算法 PTA 8-1【顺序表的实现】(C语言)

 数据结构与算法顺序表的实现

题目:

解答:

顺序表功能实现:

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

typedef int ElemType;

typedef struct {
    ElemType data[MAX_SIZE];
    int length;
} SeqList;
//判断是否为满 
/*
int sequlist_full(Seqlist *list){
	
	return l->last == N-1 ? 1 : 0; 
  
}*/
// 取顺序表的长度作为返回值 
int ListLength(SeqList *list) {
    return list->length;
}

//根据pos位置获取线性表的数据元素 
ElemType ListElement(SeqList *list, int pos) {
    if (pos < 0 || pos >= list->length) {
        printf("Invalid position\n");
        return 1;
    }
    return list->data[pos];
}

// 获取数据元素在顺序表中的位置 
int ListPosition(SeqList *list, ElemType value) {
    for (int i = 0; i < list->length; i++) {
        if (list->data[i] == value) {
            return i;
        }
    }
    return -1;
}

// 在顺序表中修改一个数据元素 

int ListModify(SeqList *list, ElemType oldValue, ElemType newValue) {
    int pos = ListPosition(list, oldValue);
    if (pos != -1) {
        list->data[pos] = newValue;
        return 1;
    }
    return 0;
}

// 在顺序表中删除重复的数据元素 
int seqlist_pos_delete(SeqList *list, int pos){
	ElemType value;
	int i = 0;
}
int ListPurge(SeqList *list) {
    int count = 0;
    int i=0;
    int j=i+1;
	int k=j; 
    for (int i = 0; i < list->length; i++) {
        for (int j = i + 1; j < list->length; j++) {
            if (list->data[i] == list->data[j]) {
                for (int k = j; k < list->length - 1; k++) {
                    list->data[k] = list->data[k + 1];
                }
                list->length--;
                count++;
                j--;
            }
        }
    }
    return count;
}

// 合并两个顺序表list1和list2,并删除重复的数据元素
int ListMerge(SeqList *list1, SeqList *list2) {
    int count = ListPurge(list1);
    for (int i = 0; i < list2->length; i++) {
        if (ListPosition(list1, list2->data[i]) == -1) {
            list1->data[list1->length] = list2->data[i];
            list1->length++;
        }
    }
    return count;
}

mian函数测试:

() {
    SeqList list;
    list.length = 7;
    list.data[0] = 10;
    list.data[1] = 20;
    list.data[2] = 30;
    list.data[3] = 40;
    list.data[4] = 40;
    list.data[5] = 50;
    list.data[6] = 60;

    printf("List length: %d\n", ListLength(&list));
    printf("Element at position 3: %d\n", ListElement(&list, 3));
    printf("Position of value 40: %d\n", ListPosition(&list, 40));

    int modified = ListModify(&list, 40, 45);
    if (modified) {
        printf("Value modified successfully\n");
    } else {
        printf("Value not found\n");
    }

    int purged = ListPurge(&list);
    printf("Number of elements purged: %d\n", purged);

    SeqList list2;
    list2.length = 4;
    list2.data[0] = 30;
    list2.data[1] = 40;
    list2.data[2] = 50;
    list2.data[3] = 70;

    int merged = ListMerge(&list, &list2);
    printf("Number of elements merged: %d\n", merged);

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值