7.3 Question A: Algorithm 2-8~2-11: Basic operation of linked list

The linked list is one of the most basic data structures in the data structure, and it is a linear list implemented with a chain storage structure. Compared with the sequence table, it is unnecessary to move the following elements when inserting and deleting. Now give you some integers, and then frequently insert and delete some of these elements, and at some point in them will let you find a certain element or output all the elements in the current linked list.

Here is a basic algorithm description:

Figure 1: The definition of the linked list type and the description of the algorithm for obtaining the elements of the linked list

Figure 2: Description of the insertion algorithm of the linked list

Figure 3: Description of the deletion algorithm of the linked list

Figure 4: Description of the creation algorithm of the linked list

enter

There is only one set of input data, the first line has n+1 integers, the first integer is the number of integers remaining in this line, and the following is n integers. This line of integers is used to initialize the list, and the order of input is opposite to the order in the list, that is, if the list is 1, 2, 3, the order of input is 3, 2, 1.

There is an integer m in the second line, which means there are m lines below. There is a string in each line, and the string is one of "get", "insert", "delete", and "show". If it is "get" or "delete", it is followed by an integer a, which means to get or delete the ath element; if it is "insert", it is followed by two integers a and e, which means it is in the ath position. Insert e in front; there is no integer after "show".

Output

If the acquisition is successful, output the element; if the deletion is successful, output "delete OK"; if the acquisition fails or the deletion fails, output "get fail" and "delete fail". If the insert is successful, it will output "insert OK", otherwise it will output "insert fail". If it is "show", output all elements in the list, if the list is empty, output "Link list is empty". Note: All double quotes are not output.

Sample input Copy

3 3 2 1
21
show
delete 1
show
delete 2
show
delete 1
show
delete 2
insert 2 5
show
insert 1 5
show
insert 1 7
show
insert 2 5
show
insert 3 6
show
insert 1 8
show
get 2

Sample output Copy

1 2 3
delete OK
2 3
delete OK
2
delete OK
Link list is empty
delete fail
insert fail
Link list is empty
insert OK
5
insert OK
7 5
insert OK
7 5 5
insert OK
7 5 6 5
insert OK
8 7 5 6 5
7

prompt

prompt:

1. Because the input data contains a large number of insert and delete operations (believe it or not, I believe it anyway), you must use a linked list, otherwise it is likely to time out. This is also the characteristic of checking the linked list.

2. The elements of the initial linked list are in reverse order. This is enough to use the method of creating a list in the title (insert from the head).

to sum up:

This question examines the characteristics of linked lists. In the sequence table, how to judge when to use the sequence table and when to use the linked list? It depends on their characteristics. The characteristic of the sequence table is random access and random access, that is to say, it is more appropriate to use the sequence table if the access and query are more frequent; the characteristic of the linked list is that it is not necessary to move the subsequent nodes when inserting and deleting, if the insert and delete operations are compared It is more appropriate to use a linked list frequently.

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef struct node///定义链表数据结构
{
    int data;
    struct node *next;
}LNode, *LinkList;

void Create(LinkList &L,int n)///头插法创建链表
{
    LinkList p;
    int i;
    L=(LinkList)malloc(sizeof(LNode));
    L->next=NULL;
    for(int i=0;i<n;i++){
        p=(LinkList)malloc(sizeof(LNode));
        scanf("%d",&p->data);
        p->next=L->next;
        L->next=p;
    }
}
void Show(LinkList L)///输出链表中所有元素
{
    LNode *p=L->next;
    if(p==NULL) printf("Link list is empty\n");
    else{
        while(p!=NULL){
            printf("%d ",p->data);
            p=p->next;
        }
        printf("\n");
    }
}
void Get(LinkList L,int x)///输出链表中第x个元素
{
    LinkList p=L->next;
    while(x!=1&&p!=NULL)
    {
        p=p->next;
        x--;
    }
    if(p!=NULL){
        printf("%d\n",p->data);
    }
    else{
        printf("get fail\n");
    }
}
void Delete(LinkList &L,int x)///删除链表中第x个元素
{
    LinkList p=L->next,q=L;
    while(x!=1&&p!=NULL){
        q=p;
        p=p->next;
        x--;
    }
    if(p==NULL){
        printf("delete fail\n");
    }
    else{
        q->next=p->next;
        free(p);
        printf("delete OK\n");
    }
}
void Insert(LinkList &L,int x,int y)///在链表第x个位置插入元素y
{
    LNode *p=L->next,*q=L;
    while(x!=1&&p!=NULL){
        q=p;
        p=p->next;
        x--;
    }
    if(x==1){
        LNode *t;
        t=(LinkList)malloc(sizeof(LNode));
        t->data=y;
        t->next=p;
        q->next=t;
        printf("insert OK\n");
    }
    else{
        printf("insert fail\n");
    }
}
int main()
{
    int n,m,x,y;
    char s[20];
    scanf("%d",&n);
    LinkList L;
    L=(LinkList)malloc(sizeof(LNode));
    Create(L,n);
    scanf("%d",&m);
    while(m--){
        scanf("%s",s);
        if(s[0]=='s'){
            Show(L);
        }
        else if(s[0]=='g'){
            scanf("%d",&x);
            Get(L,x);
        }
        else if(s[0]=='d'){
            scanf("%d",&x);
            Delete(L,x);
        }
        else if(s[0]=='i'){
            scanf("%d %d",&x,&y);
            Insert(L,x,y);
        }
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/wangws_sb/article/details/114869832