The second chapter of the data structure linear table operation (corrected)

True or False

2-1-1
For a sequentially stored linear list of length N, the time complexities for query and insertion are O(1) and O(N), respectively.

Translation: For a sequential storage linear list of length N, the time complexity of query and insertion are O(1) and O(N) respectively.

T F

Sequentially stored linear tables support random access, so the query time is constant time, but insertion needs to adjust the position of each subsequent element, so it is linear time.
However, the textbook P27-28 says: The average time complexity of the sequence table search by value algorithm is O(n), and the average time complexity of the sequence table insertion algorithm is O(n)
.

2-1-2
If the most commonly used operations are to visit a random position and to insert and delete the last element in a linear list, then sequential storage works the fastest.

Translation: If the most common operation is to access a random location and insert and delete the last element in a linear list, then sequential storage is the fastest.

T F

Frequently check and delete the last element, the sequence table is fully competent.

2-1-3
In a singly linked list of N nodes, the time complexities for query and insertion are O(1) and O(N), respectively.

Translation: In a singly linked list of N nodes, the time complexity of query and insertion are O(1) and O(N) respectively.

T F

The search is O(N), because it needs to go down the next pointer. The insertion is O(1), and you only need to change the pointer.
But textbook P33-35: The average time complexity of the singly-linked list search algorithm by value is O(n), and the average time complexity of the singly-linked list insertion algorithm is O(n);
just wonder...

2-1-4
If a linear list is represented by a linked list, the addresses of the elements in the memory must be consecutive.

Translation: If the linear linked list is represented by the linked list, the addresses of the elements in the memory must be continuous.

T F

If a linear list is represented by a linked list, then the address of the elements in the memory: may be continuous, or may not be continuous;
Textbook P29: Linear list chain storage structure is characterized by: a set of arbitrary storage units to store the linear table Data elements of (this group of storage units can be continuous or discontinuous);
Textbook P32: In a singly linked list, the storage location of each element is arbitrary;

2-1-5
organize and store N data in a singly linked list in ascending order. If a binary search is used, the average time complexity of the search is O(logN).

T F

The average complexity of the binary search of the array is O(logN). There is nothing wrong with it, but the binary search cannot be stored in a linked list. This is determined by the characteristics of the linked list. The linked list is a typical sequential access structure. The position of the data in the linked list can only be retrieved from the beginning to the end. Even if it is ordered, one of the data must be operated from the beginning. This is fundamentally different from arrays. The elements in the array are determined by the subscript. As long as you know the subscript, you can directly store the entire element, such as a[5], which is straightforward. The linked list does not have this, so the binary search can only be performed on the array.
Textbook P32: A singly linked list is a non-random access storage structure. To obtain the i-th data element, it must be searched in the chain from the head pointer, which is also called a sequential access structure;


Multiple choice

2-2-1
For a linear table of length N stored sequentially, the time complexity of accessing nodes and adding nodes is:

A : O (1), O (1)
B:O(1), O(N)
C : O (N), O (1)
D : O (N), O (N)

Sequential storage can achieve "random access", so the time complexity of accessing nodes is O(1), while inserting and deleting nodes involves a large number of moving elements, so the time complexity is O(n);
Textbook P26 -28: The average time complexity of the sequence table value algorithm is O(1), the average time complexity of the sequence table search algorithm by value is O(n), and the average time complexity of the sequence table insertion algorithm is O(n) ;
Access node == value?


The features that 2-2-2 linked list does not have are:

A: Insert and delete do not need to move elements
B: To facilitate random access to any element
C: No need to estimate the storage space in advance
D: The required space is proportional to the linear length

2-2-3
Under what circumstances is the linear table L suitable for implementation using a chain structure?

A: Need to continue to delete and insert L
B: Need to frequently modify the node value in
L C: L contains a large number of nodes
D: The node structure in L is complex

2-2-4
If the linear table adopts the chain storage structure, the address of the available storage unit in the memory is required

A: It must be continuous
B: It can be continuous or discontinuous
C: Part of the address must be continuous
D: must be discontinuous

2-2-5
In the sequence table of N nodes, the time complexity of the algorithm is O(1) operation is:

A: Visit the i-th node (1≤i≤N) and find the immediate predecessor of the i-th node (2≤i≤N)
B: Insert a new node after the i-th node (1≤i≤N)
C: Delete the i-th node (1≤i≤N)
D: Sort N nodes from small to large

A. Assuming the sequence table L, length n, find the i-th node L[i], the direct predecessor L[i-1], so it is O(1);
B needs to move ni nodes, so it is O(n) ;
C also needs to move ni nodes;
D is the slowest O(n^2) and the fastest O(nlogn) depending on the sorting method;

2-2-6
Connect the linear tables La and Lb head-to-tail, requiring a time complexity of O(1) and occupying as little auxiliary space as possible. Which structure should be used?

A: Single linked list
B: Single circular linked list
C: Single circular linked list with tail pointer
D: Double circular linked list with leading node

The time complexity of options C and D are both O(1), but C has fewer pointers and occupies less auxiliary space;
Textbook P38:
Circular singly linked list:
In some cases, if the tail pointer is set up in the circular linked list Do not set up a head pointer, which can simplify some operations;

2-2-7
adopts the non-zero necklace storage representation of polynomials. If the non-zero terms of the two polynomials are N1
​​ and N​2, and the highest term index is M​1 and M2, then two The time complexity of polynomial addition is:

A:O(N​1+N​2)
B:O(M​1+M​2​​)
C:O(N​1×N2)
D:O(M​1×M​2)

Textbook P49: Assuming that the number of terms of the two polynomials are m and n, respectively, the time complexity of the polynomial addition algorithm is O(m+n), and the space complexity is O(1);
there is no test site here;

2-2-8
In a singly linked list, if the node pointed by p is not the last node, then to insert a node pointed by s after p, we must do:

Translation: In a singly linked list, if the node pointed to by p is not the last node, then to insert a node pointed to by s after p, you must do:

A:s->next=p; p->next=s;
B:s->next=p->next; p=s;
C:s->next=p->next; p->next=s;
D:p->next=s; s->next=p;

Textbook P37: Insert the new node *s after the node *p;

2-2-9
For a non-empty singly linked circular list, with h and p pointing to its head and tail nodes, respectively, the TRUE statement is:

Translation: For a non-empty single-chain circular linked list, where h and p point to its head node and tail node respectively, then the true proposition is:

A:p->next == h
B:p->next == NULL
C:p == NULL
D:p == h

Unsure of the answer?

2-2-10
The following table shows how a linked list is stored in memory space with the head node c:

Translation: The following table shows how the c for the first node of the linked list is stored in memory space:
Insert picture description here
Now fIS Stored AT 1014Hand IS inserted The INTO The linked List BETWEEN aand e. The Then The "Link" Fields of a, e, and fare __, respectively .

Translation: Now f is stored in 1014H and inserted into the linked list between a and e. Then, the "link" fields of a, e and f are __.

A:1010H, 1014H, 1004H
B:1010H, 1004H, 1014H
C:1014H, 1010H, 1004H
D:1014H, 1004H, 1010H
Insert picture description here

Programming questions

2-7-1 The establishment and traversal of sequence table (20 points)
Insert picture description here

#include <stdio.h>
#include <stdlib.h>
#include<algorithm>
using namespace std;
#define MAXSIZE 100
typedef int ElemType;/*宏定义int型*/
typedef struct
{
    
    
    ElemType *elem;
    int length;
} SqList;
int n;
void InitList(SqList &L,int n)
{
    
    
    L.elem=(ElemType*)malloc(sizeof(MAXSIZE));/*开辟空间:(数据类型*)malloc(长度)*/
    L.length=0;
    ElemType q;
    for(int i=0; i<n; i++)
    {
    
    
        scanf("%d",&q);
        L.elem[L.length++]=q;
    }
    for(int i=0; i<L.length; i++)
        printf("%d%c",L.elem[i],i==L.length-1?'\n':' ');
    return;
}
int main()
{
    
    
    scanf("%d",&n);
    SqList L;
    InitList(L,n);
    return 0;
}

2-7-2 Creation and traversal of singly linked list (20 points)
Insert picture description here

#include<iostream>
using namespace std;

#define OK 1
#define ERROR 0

typedef int Status;
typedef int ElemType;

typedef struct LNode{
    
    
	ElemType data; //数据域
	struct LNode *next; //指针域
} LNode, *LinkList;

Status InitList(LinkList &L); //函数声明
void CreateList( LinkList &L,int n); //函数声明 
void DisplayList( LinkList ); //函数声明 
void DestroyList( LinkList ); //函数声明

int main()
{
    
    
	LinkList L; //值传递
	InitList( L ); //调用函数InitList
	int n;
	cin >> n;
	CreateList( L, n );
	DisplayList( L );
	DestroyList( L );
	return 0;
}

Status InitList(LinkList &L)
{
    
    	//构造一个空的单链表L 
	L = new LNode ;		//生成新结点作为头结点,用头指针L指向头结点 
	L->next = NULL ;	//头结点的指针域置空 
	return OK;
}

void CreateList( LinkList &L,int n )
//尾插法建立带头结点的有n个元素的链表 
{
    
    
	LNode *p, *r; //定义p与r指针 

	r = L ; //尾指针r指向头结点

	for( int i=0; i<n; ++i ){
    
     
		p = new LNode ; 	//生成新结点*p
		cin >> p->data; 	//输入元素值赋给新结点*p的数据域 
		p->next= NULL; 
		r->next = p ; 		//将新结点*p插人尾结点*r之后
		r = p; 				//r指向新的尾结点*p
	} 
}

void DisplayList( LinkList L )
//遍历带头结点的单链表 
{
    
    
	LNode *p =L->next ;
	while ( p ){
    
     
		if(p->next==NULL){
    
    
			cout<<p->data;
		}else{
    
    
			cout <<p->data<<" ";
			}
	p=p->next;
	}
}

void DestroyList( LinkList L )
{
    
    //回收L的所有结点的空间 
	LNode *p = L, *q; 
	while ( p ){
    
     		//当p指向结点不为空 
		q = p->next;	//q指向p的下一结点 
		delete p; 		//回收p指向的结点空间 
		p = q; 			//p指向q指向的结点 
	}
}

2-7-3 Array rotate left (20 points)
Insert picture description here

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

typedef struct Node *NodePtr;
struct Node
{
    
    
	int Val;
	NodePtr Next;
};
int main()
{
    
    
	int n,m;
	int i;
	NodePtr Head,Rear,Tmp,Pre;
	Head=Rear=(NodePtr)malloc(sizeof(struct Node));
	Head->Next=NULL;
	scanf("%d %d",&n,&m);
	for(i=0;i<n;i++)
	{
    
    
		Pre=(NodePtr)malloc(sizeof(struct Node));
		Pre->Next=NULL;
		scanf("%d",&Pre->Val);
		Rear->Next=Pre;
		Rear=Pre;
	}
	for(i=0;i<m;i++)
	{
    
    
		Tmp=(NodePtr)malloc(sizeof(struct Node));
		Tmp->Val=Head->Next->Val;
		Tmp->Next=NULL;
		Rear->Next=Tmp;
		Rear=Tmp;
		Head=Head->Next;
	}
	Tmp=Head->Next;
	printf("%d",Tmp->Val);
	for(Tmp=Tmp->Next;Tmp!=NULL;Tmp=Tmp->Next)
		printf(" %d",Tmp->Val);
	return 0;
}

Guess you like

Origin blog.csdn.net/Jessieeeeeee/article/details/105329628