C语言实现顺序表的插入

盆友宇飞给我了道题,意识到自学还是干不过本科生。
在这里插入图片描述
下面是实现的代码:对循序表只是初相识了,甚至发现结构也运用得不是很熟练(根本不熟)。

#include <stdio.h>
/*自定义*/
#define MAX 6
typedef int elemtype;

/*自定义一个结构*/
typedef struct        
{
    elemtype List[MAX];
    int num;
}qltype;           /*qltype是一个结构名字*/

/*声明用到的函数*/
int listinsert(qltype*, int, elemtype );
int showlist(qltype*);    

/*函数主体*/
int main()
{
    qltype *L = NULL;
    qltype Mylist = {{1, 2, 4, 5, 6}, 5};   /*定义时没有将List填满,留出3的空位*/ 
    L = &Mylist;
    
    listinsert(L, 3, 3);
    showlist(L);
    
    getchar();
    return 0;
}


/*插入函数*/
int  listinsert(qltype* L, int n, elemtype element)    /*传入一个结构的指针, 插入的位置, 插入的元素*/
{
    int key;   
    
    if (L -> num == MAX)  /*如果顺序表已满*/
    {
        printf("the list is full");
        return 0;
    }
    
    if (n < 1 || n > L -> num + 1)    /*如果插入的位置不对*/
    {
        printf("check the number if in the range");
        return 0;
    }
    
    if (n <=  L -> num) 
    {
        for (key = L -> num - 1; key >= n - 1; key--)  /*key初始是List的最后一个元素, 一直到要插入的位置, 每次的key将前一个元素后移一位*/
        {                                              /*即这里的循环操作是将插入位置之后的元素统一后移一位*/
            L -> List[key + 1] = L -> List[key]; 
        }
        
        L -> List[n - 1] = element; /*将元素element插入在位置n上*/
        L -> num++;   /*顺序表长度+1*/
        return 1;
    }
}


/*显示顺序表中的元素*/
int showlist(qltype* L)
{
    int i;
    for (i = 0; i <= L -> num -1; i++)
    {
        printf("%d", L -> List[i]);
    }
    return 1;
}
  • 6
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Sure! Here's an example of how you can implement the insertion operation on a sequential list (array) using the C programming language: ```c #include <stdio.h> #define MAX_SIZE 10 typedef struct { int data[MAX_SIZE]; int length; } SeqList; // Function to insert an element into the list at a given position void insert(SeqList *list, int position, int element) { // Check if the list is full if (list->length >= MAX_SIZE) { printf("Error: List is full, cannot insert element.\n"); return; } // Check if the position is valid if (position < 0 || position > list->length) { printf("Error: Invalid position, cannot insert element.\n"); return; } // Shift elements to the right to make space for the new element for (int i = list->length - 1; i >= position; i--) { list->data[i + 1] = list->data[i]; } // Insert the new element at the given position list->data[position] = element; // Increment the length of the list list->length++; } int main() { SeqList list = {{1, 2, 3, 4, 5}, 5}; int position = 2; int element = 10; printf("Before insertion: "); for (int i = 0; i < list.length; i++) { printf("%d ", list.data[i]); } printf("\n"); // Insert the element at the given position insert(&list, position, element); printf("After insertion: "); for (int i = 0; i < list.length; i++) { printf("%d ", list.data[i]); } printf("\n"); return 0; } ``` In this example, we define a structure `SeqList` which represents the sequential list. It contains an array `data` to store the elements and an integer `length` to keep track of the number of elements in the list. The maximum size of the list is defined as `MAX_SIZE`. The `insert` function takes a pointer to the `SeqList` and two arguments: `position` (the index at which the element should be inserted) and `element` (the value to be inserted). It first checks if the list is full or if the position is invalid. If not, it shifts the elements to the right from the given position to create space for the new element, inserts the element at the given position, and increments the length of the list. In the `main` function, we create a `SeqList` object `list` with some initial elements. We then call the `insert` function to insert an element (value 10) at position 2. Finally, we print the list before and after the insertion to verify the result. Please note that this is just a basic implementation of the insertion operation on a sequential list. There are various other methods and optimizations that can be applied depending on your specific requirements.
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值