线性表操作(C语言)

题目描述

顺序表是我们数据结构中的基本储存形式,现在给定一个顺序表,有如下操作:
Insert X Y:在顺序表中X位置插入Y元素,遍历输出当前顺序表的所有元素。
Delete X:删除顺序表中的X元素,如果有多个X元素,只删除第一个X,遍历输出当前顺序的所有元素。
Locate X:输出顺序表中X元素的所在的位置,如果有多个X元素,只输出第一个X元素的位置。
GetElem X:输出顺序表中X位置上的元素。

输入描述

多组测试数据。
对于每组测试数据,首先输入的是两个整数n,m(0


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

typedef struct node* List;
struct node
{
    int data;
    struct node *next;
};

int n;

void Print(List s)
{
    int k;
    k = 0;
    s = s->next;
    if(s == NULL)
    {
        printf("Wrong Input!\n");
        return ;
    }
    while(s != NULL)
    {
        if(k != 0)
            printf(" ");
        printf("%d", s->data);
        s = s->next;
        k++;
    }
    printf("\n");
    return ;
}

List Create()
{
    List p, ptr, head;
    head = (List)malloc(sizeof(struct node));
    head->next = NULL;
    ptr = head;
    for(int i = 0; i < n; i++)
    {
        int x;
        scanf("%d", &x);
        p = (List)malloc(sizeof(struct node));
        p->data = x;
        p->next = NULL;
        ptr->next = p;
        ptr = p;
    }
    return head;

}

void Insert(List head, int x, int y)
{
    List h = head;
    int i;
    if(x > n+1)
    {
        printf("Wrong Input!\n");
        return ;
    }
    for(i = 0; i < x-1; i++)
    {
        h = h->next;

    }
    List q = (List)malloc(sizeof(struct node));
    q->data = y;
    q->next = h->next;
    h->next = q;
    n++;
    Print(head);

}



void Delete(List head, int x)
{
    List p = head->next;
    List ptr = head;
    while(p != NULL)
    {
        if(p->data == x)
        {
            break;
        }
        else
        {
            ptr = p;
            p = p->next;
        }

    }
    if(p == NULL)
    {
         printf("Wrong Input!\n");
         return ;
    }

    else
    {
        ptr->next = p->next;
        n--;
        Print(head);
    }

}

void Locate(List head, int x)
{
    int k = 1;
    List p = head->next;
    List ptr = head;
    while(p != NULL)
    {
        if(p->data == x)
        {
            break;
        }
        else
        {
            ptr = p;
            p = p->next;
            k++;
        }

    }
    if(p == NULL)
    {
         printf("Wrong Input!\n");
         return ;
    }
    else
        printf("%d\n", k);
}

void GetElem(List head, int x)
{
    if(x > n+1)
    {
        printf("Wrong Input!\n");
        return ;
    }
    List p, ptr;
    p = head->next;
    for(int i = 1; i < x; i++)
    {
        p = p->next;
    }
    printf("%d\n", p->data);
}

int main()
{
    int m, x, y, i, j;

    char s[50];
    scanf("%d %d", &n, &m);

    List L = Create();
    while(m--)
    {
        scanf("%s", s);
        if(strcmp(s, "Insert") == 0)
        {
            scanf("%d %d", &x, &y);
            Insert(L, x, y);
        }
        else if(strcmp(s, "Delete") == 0)
        {
            scanf("%d", &x);
            Delete(L, x);
        }
        else if(strcmp(s, "Locate") == 0)
        {
            scanf("%d", &x);
            Locate(L, x);
        }
        else if(strcmp(s, "GetElem") == 0)
        {
            scanf("%d", &x);
            GetElem(L, x);
        }
    }


    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42819248/article/details/82728979
今日推荐