[postgraduate entrance examination] sequence list and linked list

Posted by no_one on Thu, 30 Sep 2021 21:55:44 +0200

Sequential table structure

Static allocation of sequential storage

#define MaxSize 50 / / define the maximum length of a linear table
typedef int Elemtype//Assume that the element type in the table is int
typedef struct{
	ElemType data[MaxSize];//Elements of sequential table (array)
	int length ;//Current length of sequence table
}

Dynamic allocation of sequential storage

typedef int Elemtype
typedef struct 
{
	ElemType *data ;//Pointer indicating dynamically allocated array 
	int MaxSize , length ;//Maximum capacity and current number of arrays
};

/* c Dynamic allocation statement of language */
#define InitSize 100
SeqList L;
L.data=(ElemType*)malloc(sizeof(ElemType)*InitSzie);

Linked list

Node structure

Single linked list

typedef struct LNode{
	ElemType data ;//Data domain 
	struct LNode *next; //Pointer field		
}LNode , *LinkList;

Double linked list

typedef struct DNode{
	ElemType data;
	struct DNode *prior , *next;
}DNode , *DinkList;

Static linked list

#define MaxSize 50 	// Defines the maximum length of the linked list	
typedef struct{		//Definition of static linked list structure type
	ElemType data;	//Storage data structure
	int next;		//Array subscript for storing elements
}SlinkList[MaxSize];

Values in the output linked list:

printf(p->next->data);

statement

Declaration linked list: LinkList &A
 Declaration node/Pointer: LNode *p

Creation of linked list

Head insertion

LinkList List_HeadList(LinkList &L){
	LNode *p , int x;
	L = (LinkList *)malloc(sizeof(LinkList));	//Application space
    L->next = NULL;			//The pointer is NULL
    scanf("%d" , &x);		//Enter x value
    while(x != 999){
    	p = (LinkList*)malloc(sizeof(LinkList));
    	p->data = x;
    	p->next = L->next;
    	L->next = p;
    	scanf("%d" , &x);
    }
	return L;
}

Tail interpolation

LinkList CreatLinkList(LinkList &L){
	int x ;
	L=(LinkList)malloc(sizeof(LNode));
	LNode *s , *r = L; //r is the footer, and the pointer points to the footer 
	scanf("%d" , x) ; //Enter the value of the node 
	while(x != 9999){//Enter 9999 to end 
		s=(LNode*)malloc(sizeof(LNode)) ;
		s->data = x ;
		r->next = s ;
		r= s ;//r points to the new footer node 
		scanf("%d" , x) ;
	}
	r->next = NULL ;//Tail node pointer null 
	return L;

Sequential table algorithm

Remove minimum from sequence table

The function returns the position of the deleted element, and the empty position is filled by the last element

void DeleteElem_min( SqList &L , int minvalue , int k){
//Find the minimum value first 
	if(L.length == 0)
		return 1;
	minvalue = L.data[0];	//First set the minimum value to array 0
	int k=0;	//The initial position is 0 
	for( int i = 1 ; i < L.length ; i++){
		if( minvalue > L.data[i] ){
			minvalue = L.data[i];
			k = i;
		}
	} 
	L.data[k] = L.data[L.length-1];	
	L.length--;
	return k;
}  

Reverse order table L

/Sequence table L Inversion 
void Reverse(SqList &L){
	int temp;	//Auxiliary variable
	for( int i = 0 ; i < L.length/2 ; i++){
		temp = L.data[i];
		L.data[i] = L.data[L.length-i-1];
		L.data[L.length-i-1] = temp;
	} 
	return 0;
}

Delete all data elements with value x

void Delete_key( SqList &L ,int key ){
	int k = 0;
	for( int i = 0 ; i < L.length ; i++ ){
		if( L.data[k] != key){
			L.data[k] = L.data[i];
			k++; 
		}
	}
	L.length = k;
	return L.length;
}

Delete the data elements between [s,t] in the ordered table

Ordered table

bool Delete_rangekey(SqList &L , int key ,int s ,int t) {
//If L is an ordered table, use ij to find values greater than s and less than t	
	int i,j; 
	if( s >= t || L.length == 0)
		return false;
	for(i = 0 ; L.data[i] < s && i < L.length ; i++ );
	if( i >= L.length )
		return false;
	for( j=i ; L.data[j] <= t && j <= L.length ; j++ );
	for( ; j < L.length ; i++,j++);
		L.data[i] = L.data[j];
	L.length= i;
	return true;
}

Delete the data elements between [s,t] in the sequence table

bool Del_s_t(SqLsit &L , int s ,int t){
	int i , k =0;
	if( L.length == 0 || s>= t)
		return false;
	for( i = 0 ; i < L.length ; i++ ){
		if( L.data[i] >= s && L.data[i] <= t)
			k++;
		L.data[i-k] = L.data[i];	//Move k positions forward 
	}
	L.length -= k;
	return true;	
}

Delete duplicate values from ordered tables

bool Delete_data( SqList &L , int key ){
	int i ,j;
	if(L.length == 0 )
		return true;
	for(i = 0 j=1; j < L.length ; j++){
		if(L.data[i] != L.data[j])
			L.data[++i] = L.data[j];
	}
	L.length = i+1;
	return true; 		
} 

Merge two ordered tables

int MergeList_two(SqList &L1 , SqList &L2 , SqList &L3 ){
//Compare the ordered tables L1,L2 and the algorithm
	int i=0 , j=0 ,k=0;
	if( L1.length + L2.length > L3.length)
		return false;	
	while( i < L1.length && j < L2.length ){
		if(L1.data[i] > L2.data[j])
			L3.data[k] = L2.data[j];   
		else
			L3.data[k] = L1.data[i];
	}
	while( i < L1.length)
		L3.data[k++] = L1.data[i++];
	while( j < L2.length)
		L3.data[k++] = L2.data[i++];
	L3.length= k;
	return true; 
}

Permutation algorithm

//Permutation algorithm
//Train of thought (sort in half): three times 
//Swap an and bn, and then perform a half search inside each 
void Search_two( int A[] , int left ,int right , int arraySize ){	
//arraySize represents the length of A []. Make A mark to prove that right does not exceed the upper limit. The default is m+n 
	int temp;
	if(right >= left || right >= arraySiz)
		return;
	int mid = (left + right)/2;
	for(int i = 0 ; i <= mid-left ; i++){
		temp = A[left+i];
		A[left+i] = A[right-i];
		A[right-i] = temp;
	}
} 

void ExChange( int A[] , int m , int n , int mid , arraySize){
	Search_two(A , 0 , m+n-1 , arraySize);
	Search_two(A , 0 , n-1 , arraySize);
	Search_two(A , n , m+n-1 , arraySize);
}

The ab part of the array becomes the ba part

//Move the array p positions to the left 
//ab becomes ba, using the idea of inversion 
void Reverse_elem_p(int A[] , int left ,int right){
	if(left <= right )
		return;
	for( int i = 0 ; i < right-left ; i++){
		int temp = A[left+i];
		A[left+i] = A[right-i];
		A[right-i] = temp;
	}
}

void ALL_Reverse( int A[] , int int p , int n){
	Reverse_elem_p(A , 0 , n-1);
	Reverse_elem_p(A , 0 , P-1);
	Reverse_elem_p(a , p , n-1);
}

Array to find duplicate elements (primary elements)

//The primary element must have the largest number of repetitions, and x must be greater than n/2
int Majority( int A[] , int n){
	int i , c , count=1;
	c = A[0];
	for( i =1 ; i < n ; i++){
		if(A[i] == c)
			count++;
		else{
			if(count > 0)
				count--;
			else{		//Actual occurrences of replacement candidate primary elements 
				c = A[i];
				count =1;
			}
		}
	} 
	if(count > 0 ){
		for(i = count = 0 ; i < n ; i++){
			if(A[i] == c)
				count++;	//Counts the number of occurrences of the primary element 
		}
	}
	if(count > n/2)
		return c;	//But still candidate elements 
	else	
		return -1;	//No primary element exists 
}

Find the smallest positive integer

//The smallest positive integer, starting from 1, 2, 3 and 4, find the number that does not appear in the array
void findMissMId( int A[] , int n){
	int *B =(int *)malloc(n*sizeof(int ));
	memset(B , 0 , sizeof(int)*n);
	for( int i=0 ; i < n ;i++){
		if(A[i] > 0 && A[i] < n)
			B[A[i]-1] = 1;
	}
	for(int i = 0 ; i < n ; i++){
		if(B[i] == 0)
			breal;
	}
	return i+1;
	
}

Linked list algorithm

Delete the same x value recursively

//Recursive form, delete the single linked list without the leading node, and delete all the nodes with the value of x in L
void Delete_x(LinkList &L , int x){
	LNode*p;
	if(L == NULL)
		return;
	if(L->data == x){
		p = L;
		L=L->next;
		free(p);
		Delete_x(L,x);
	}
	else
		Delete_x(L->next , x);
} 

Delete x value from single linked list

//Find them one by one, delete them if found, and move on to the next if not found 
void Delete_LNode_x(LinkList &L , int x){
	LNode *p=L->next , *pre = L , *q;
	while( p != NULL){
		if( p->data == x ){
			q = p;		//q points to the node 
			p = p->next;	
			pre->next = p;	//Delete q node 
			free(q);		//Free * q node space 
		}
		else{	//Otherwise, pre and p move back 
			pre = pre->next;
			p = p->next;
		} 
	}
}

Single linked list deletes the node with the minimum value

//The single linked list of the leading node deletes the minimum node
LinkList Delete_Minnum( LinkList &L ){
	LNode *p = L->next , *pre = L;
	LNode *min = p , *minpre = pre;
	while( p != NULL){
		if( p->data < min->data ){
			min = p;	//Find a node smaller than the previous minimum 
			minpre = pre;
		}
		pre = p;		//Continue scanning for the next element 
		p = pre->next;
	}
	pre->next= min->next;	//Delete minimum 
	free(min);
	return L;
} 

Single linked list (local!) reverse

//First disconnect the initial node L, and then disconnect the subsequent nodes and reconnect the upper node 
LinkList replacementList (LinkList L){
	LNode *p = L->next , *pre = p->next, *r;
    L->next =NULL;
	while( p->next != NULL){
		r = p->next;
		p->next = r->next;
		L->next = p;
		p = r;
	}
	return L;
} 

Order elements incrementally

//Using the idea of insertion sort
void Sort(LinkList &L){
	LNode *p = L->next,*pre;
	LNode *r = p->next;		//Keep r after p to ensure continuous chain
	p->next = NULL;			//Divide the linked list into 2 items
	p = r;					
	while(p != NULL){
		r = p->next;
		pre = L;
		while(pre->next != NULL && pre->next->data < p->data)
			pre = pre->next;	//Find the precursor node * pre inserted into * p in the ordered table
		p->next = pre->next;	//Insert p after pre
		pre->next =p;			
		p = r;				//Scan the remaining nodes
	}
}

Deletes the element between the given X and Y values

void RangeDelete(LinkList &L){
	LNode *p=L , *pre = p->next;
	while(p->next != NULL){
        if( p->next->data >x && p->next->data < y ){
            p->next = pre->next;
            free(p);
            p = p->next;
            pre = p->next;
        }
        else{
            p = p->next;
            pre = p->next;
        }
	}
}

Find the common node of two linked lists

The next pointer of each node in the single linked list is unique, so when there are public nodes, all the subsequent nodes are the same, so the linked list is a "Y", not an "X" in our inertial thinking. According to this conclusion, if two linked lists have common nodes, their tail nodes must be the same.

//Find the common node of two linked lists
//The public nodes of the two linked lists are the same 
//First find out the length difference dist between the two linked lists, and then let it find the first node
LNode* Search_SamNode(LinkList L1,LinkList L2){
    //Get the length of two linked lists. dist represents the length difference
    int len1 = Length(L1),len2 = Length(L2),dist;
    LNode* longptr,shortptr;
    if(len1 > len2){
        dist = len1 - len2;
        longptr = L1;
        shortptr = L2;
    }else{
        dist = len2 - len1;
        longptr = L2;
        shortptr = L1;
    }
    //Move the long pointer back dist to ensure that the two pointers are synchronized
    while(dist--)
        longptr = longptr->next;
    while(longptr != NULL){		//Sync found common point
        if(longptr == shortptr)	//First public node found
            return longptr;
        else{
            longptr = longptr->next;
            shortptr = shortptr->next;
        }
    }
    return NULL;
}

Split into 2 linked lists

//The linked list of one leading node is decomposed into the linked list of two leading nodes according to the parity position
LinkList DepartList(LinkList &A){
    LinkList B;
    B = (LinkList)malloc(sizeof(LNode));
    int count = 1;
    //p is the working pointer, q points to the B head node, and y points to the A head node
    LNode *p = A->next,
          *q = B,
          *y = A;
    A->next = NULL;//Empty A linked list
    while(p != NULL){
        if(count%2 == 0){//Even positions are added to the tail of B
            q->next = p;
            q = p;
        }else{//Odd positions are added to the tail of A
            y->next = p;
            y = p;
        }
        p = p->next;//The working pointer moves back one bit
        count++;//Counter plus one
    }
    //Point the tail pointer to null
    q->next = NULL;
    y->next = NULL;
    return B;
}

Change table C into table AB locally

C: {a1,b1,a2,b2,a3,b3...an,bn};A:{a1,a2,a3...an};B:{b1,b2,b3...bn}

LinkList DisCreat(LinkList &A){
	LinkList &B;
	B = (LinkList)malloc(sizeof(LNode));
	B->next = NULL;
	LNode *p = A->next , *q;
	LNode *ra =A;
	while( p != NULL){
		ra->next = p;
		ra = p;
		p = p->next;
		if(p != NULL)
			q = p->next;
		p->next = B->next;
		B->next =p;
		p = q;
	}
	ra->next = NULL;
    return B;
}

Subtract the same value from the sequential linked list

void Del_Same(LinkList &L){
//L is the incremental linked list, minus the same value in the linked list
	LNode *p = L->next ,*pre = p->next;
	if(p == NULL){
		return;
	}
	while(p->next != NULL){
		pre = p->next;
		if(p->data == pre->data){
			p->next =pre->next;
			free(pre);
		}
		else
			p = p->next;
	}
}

Intersection of two ordered linked lists

void Union(LinkList &L , LinkList &B){
	LNode *p = L->next , *pre = B->next,*r = L, *s = B;
	while( p != NULL){
		if(p->data == pre->next){
			s->next = pre->next;			
			pre->next = p->next;
			p->next = pre;
			pre = s->next;
			r = p;
			p = pre->next;
		}
		else if( p->data < pre->data){
			r = p;
			p = p->next;
		}
		else{
			r->next = pre->next;
			free(pre);
			pre = r->next;
		}
	}
	while (p){	//A has not finished traversing 
		r = p;
		p = p->next;
	}
	while(pre){
		s = pre;
		pre = pre->next;
	}
	p->next = NULL;
	free(r);
	return;
}

Determine whether it is a continuous subsequence

int pattern (LinkList A , LinkList B){
    LNode* p  = A;         //p is the working node of A linked list. This problem assumes that both A and B have no head nodes
    LNode* pre  = A;    //Record the start node of A-linked list in each comparison
    LNode* q  = B;      //q is the work node of B linked list

    while( p && q ){
        if(p->data == q->data){
            p = p->next;
            q = q->next;
        }
        else{
            pre = pre->next ;   //A linked list starts from the last comparison node
            p = pre;
            q= B;
        }
    }
   
    if(q==NULL)  return 1;
    else return 0 ; 

Is the double cycle linked list symmetrical

void Symmetry (LinkList &L){
//Scan the circular double linked list from scratch to judge whether the linked list is symmetrical
	LNode *p=L->next; *pre = L->prior;//Two end working pointer
	while( p!= q && p->next != pre){
		if(p->data == pre->data){
			p = p->next;
			pre = pre->prior;
		}
		else
			return 0;
	}
	return 1;
}

Link two circular double linked lists

LinkList Link(LinkList &h1,LinkList &h2){
    //After the circular linked list h2 is linked to the circular linked list h1, it remains in the form of a circular linked list
    LNode *p, *q;  //Point to the tail nodes of the two linked lists respectively
    p=h1;
    while (p->next!=h1)  //Find the tail node of h1
        p=p->next;
    q=h2;
    while (q->next!=h2)  //Find the tail node of h2
        q=q->next;
    p->next=h2;    //After linking h2 to h1
    q->next=h1;    //Let the tail node of h2 point to h1
    return h1;
}

Double linked list finds the minimum value and deletes it

void Del_All(LinkList &l){
	LNode *p , *pre, *minp ,*minpre;
//Let minp point to the minimum value, minpre is its suffix, and P and pre are traversed 
	while(p! = L){
	//Because each element needs to be deleted, it needs to be traversed n times 
		p = L->next;
		pre = L;
		minp = p;
		minpre = pre;
	//Let p and pre traverse and compare with minp to find the minimum value 
		while(p!= L){
			if(p->data < minp->data){
				minp = p;
				minpre = pre; 
			}
			pre = p;
			p = p->next;
		}
		printf("%d" , minp->data)data;//Output minimum 
		minpre->next = minp->next;//Delete minimum 
		free(minp);//Free up space 
	}	
	free(L);
}

The values of freq in the access frequency domain are arranged in descending order

typedef struct LNode
{
    int data;
    struct LNode *pre;
    struct LNode *next;
    int freq;
}LNode,*LinkList;

void Locate(LinkList &L,int x)
{
    LNode* p = L->next , *q;
    while(p!=NULL){	//Start looking for elements equal to x
        if(p->data==x)
           break;
        p=p->next;
    }
    if(p==NULL){
        printf("Does not exist. The value is x Node of");
        return 0;
    }
    else{
        p->freq++;
        LNode* temp=p;
        temp->next->pre=temp->pre;
        temp->pre->next=temp->next;//Point p is removed

        LNode* q=L->next;
        while(q!=NULL&&q->freq<p->freq)
            q=q->next;
        p->pre=q->pre;
        q->pre->next=p;	//When inserting the p node, it must be the first one in the same frequency
        p->next=q;
        q->pre=p;
    }
    return L;
}

The linked list outputs the value of the reciprocal node

void Search_k(LinkList list , int k){
	LNode *p = list->link ,*q = list->link;
	int count= 0;	//Count with count 
	while( p != NULL){	//Traverse to the last node 
	//Because k is the reciprocal, just let p move the length of table length - k 
		if(count <k)	//Use count to reach k size to make p move the table - k times 
			count++;
		else
			p = p->link;
			q = q->link;
	}
	if(count > k)
		return 0;
	else{
		printf("%d" ,q->data);
		return 1;
	}
}

Determine whether the linked list has a ring

void *FindLoodStart(LinkList *head){
	LNode *p = head , *pre = head;
	//Let p go one step faster than pre every time, so that if there is a ring, p and pre will meet in the ring 
	while( p->next!= NULL && pre!= NULL){
		p = p->next;
		pre = pre->next->next;
		if(p == pre)
			break;
	}
	if( p->next ==NULL || pre == NULL)		//If it is NULL, there is no ring 
		return NULL;
	LNode *q = head ,*r = p; 		//Create two pointers to find the entry point of the ring 
	while( q!= NULL){
		q = q->next;
		r = r->next;
	}
	return q;		//Return to entry point 	
} 

Topics: C data structure linked list