[data structure] (Yan Weimin version) implementation and code of relevant functions of sequence table

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define MAXSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
using namespace std;
typedef int Status;
typedef int ElemType;
typedef struct{
	ElemType *elem;
	int length;
}SqList;


//Linear table initialization
Status Init_List(SqList &L){
	L.elem=new ElemType[MAXSIZE];
	memset(L.elem,0,sizeof(L));
	L.length=0;
	return 0;
}

//menu
void menu(){
	printf("*********1,establish            2. Print***********\n");
	printf("*********3,insert            4. Delete***********\n");
	printf("*********5,lookup            6. Invert***********\n");
	printf("*********7,empty            8. Quit***********\n");
}



//establish
bool CreateList(SqList &L,int n){
	if(n<0||n>MAXSIZE)
	false;
	int i;
	printf("Please enter n number\n");
	for(i=0;i<n;i++){
		scanf("%d",&L.elem[i]);
		L.length++;
	}
	return true;
}


//Print
Status PrintList(SqList L){
	int i;
	printf("Print all elements in the current sequence table:"); 
	for(i=0;i<L.length;i++){
		printf("%d ",L.elem[i]);
	}
	printf("\n");
	return 0;
}


//insert
Status InsertList(SqList &L){
	int m,n,i;
	printf("Please enter the position and value to insert in the linear table:\n");
	cin>>m>>n;
	if(m<1||m>L.length+1){
		printf("Unqualified insertion position"); 
		return ERROR;
		
	}
	for(i=L.length-1;i>=m-1;i--){
		L.elem[i+1]=L.elem[i];
	}
	L.elem[m-1]=n;
	L.length++;
	return OK;
}


//delete
Status DeleteList(SqList &L){
	int m,i;
	printf("Please enter the number of elements to delete\n");
	cin>>m;
	for(i=m-1;i<=L.length-2;i++){
		L.elem[i]=L.elem[i+1];
	} 
	L.length--;
	return OK;
	
}


//lookup
Status SearchList(SqList L){
	int m;
	printf("Please enter the value you want to find:\n");
	cin>>m;
	int i;
	for(i=0;i<=L.length-1;i++){
		if(L.elem[i]==m){
			printf("The location of the deleted element is:%d \n",i+1);
			break;
		}
	}
	return OK;
}



//inversion
Status ReverseList(SqList L){
	sort(L.elem,L.elem+L.length,greater<int>());
	return OK;
}



/*Status SplitSort(SqList L){
	    int i,j;
	    i=0;
	    j=2;
		while(j>=L.length){
			if(L.elem[i]>L.elem[j]){
				int t;
				t=L.elem[j];
				L.elem[j]=L.elem[i];
				L.elem[i]=t;
			    i=i+2;
			    j=j+2;
		    }
		}
		int m,n;
		m=1;
		n=3;
		while(n>=L.length){
		    if(L.elem[m]>L.elem[n]){
			    int temp;
		     	temp=L.elem[m];
		    	L.elem[m]=L.elem[n];
		     	L.elem[n]=temp;
		     	m=m+2;
		     	n=n+2;
		    }
		}
	return OK;
}*/


//empty
void ClearList(SqList &L){
	L.length=0;
}


int main(){
	SqList L;
	Init_List(L);
	int choice;
	while(1){
		menu();
		printf("Please enter the menu serial number:\n");
		cin>>choice;
		if(choice==8) break;
	    switch(choice){
		    case 1:
	            int n;
	            printf("Please enter the length of the sequence table you want to create\n");
       	        cin>>n;
	            CreateList(L,n);
	            printf("Created successfully!!!\n");
	            //PrintList(L);
	            break;
	        case 2:
	            //InsertList(L);
	            PrintList(L);
	            break;
	        case 3:
	    	    InsertList(L);
	         	break;
	        case 4:	
	            DeleteList(L);
            //	PrintList(L);
                break;
            case 5:	
	            SearchList(L);
	            break;
	        case 6:
	            ReverseList(L);
	            //PrintList(L);
	            break;
	        case 7:
	    	    ClearList(L);
	    	    break;
	        default:
	    	    printf("Input error!!!\n");
        }
    }
	//SplitSort(L);
	//PrintList(L);
		
	return 0;
}

Experience in writing code

1. Header file

#Include < cstdio > is c + + to reference the scanf and printf function libraries #include < stdio h> , in order to distinguish from the function library of c + +, it is removed h. Add 'c' before. Similar ones are #include < CString >, #include < cmath >, #include < cstdlib > (which encapsulates many functions, such as free, malloc and other dynamic memory allocation functions)

The CIN > > and cout < < in c + + need to be included in the #include < iostream > header file.

#Include < CString > common functions contained in header file

1. strcpy function

  • Prototype: char* strcpy (char *s1, const char *s2);
  • Function: copy string 2 to character array 1
  • explain:
    • The length of character array 1 should not be less than the length of string 2
    • "Character array 1" must be written as an array name, and "string 2" can be a character array name or a string constant
    • When no initial value is assigned to character array 1, the string in "string 2" and the subsequent "/ 0" are copied into character array 1 to replace the first n+1 characters, and the following characters are the original characters of "character array 1"

2. strncpy function

  • Prototype: char* strncpy (char *s1, const char *s2, size_t len);
  • Function: copy the first len characters of s2 to the address specified in s1 without '\ 0'

5. strxfrm function

  • Prototype: size_t strxfrm (char *s1, const char *s1, size_t len);
  • Function: according to the current area option of the program, copy the first len characters (bytes) of s2 to the address specified in s1 without '\ 0'

6. strcat function

  • Prototype: char* strcat (char *s1, const char *s2);
  • Function: connect string 2 to string 1 (string 1 should be large enough)
  • Note: the first two strings of the connection have '/ 0'. When connecting, the '/ 0' after the string 1 will be discarded, and only '/ 0' will be retained after the new string

7. strncat function

  • Prototype: char* strncat (char *s1, const char *s2, size_t len);
  • Function: connect the first len characters of string s2 to the end of s1 without '\ 0'

8. strcmp function

  • Prototype: int strcmp (const char *s1, const char *s2); ① Function: compare string 1 and string 2
  • Rule: two strings are compared character by character from left to right (compared according to the size of ASCII code value) until different characters appear or "/ 0" is encountered. If all characters are the same, they are considered equal. If different characters appear, the first different character shall prevail
  • Guidelines:
    • If string 1 = string 2, the return value of the function is 0
    • If string 1 > string 2, the function returns a positive number
    • If string 1 < string 2, the return value of the function is negative

9. Function strncmp

  • Prototype: int strncmp (const char *s1, const char *s2, size_t len);
  • Function: compare the first len characters of s1 and s2

10. Function memcmp

  • Prototype: int memcmp (const void *s1, const void *s2, size_t len);
  • Function: compare the first len bytes of s1 and s2

11. Function strcoll

  • Prototype: int strcoll (const char *s1, const char *s2);
  • Function: according to the LC in the current area option of the program_ Collate, compare strings s1 and s2

12. Function strchr

  • Prototype: char* strchr (const char *s, int ch);
  • Function: find the position of the first occurrence of the given character ch in s

13. Function memchr

  • Prototype: void* memchr (const void *s, int ch, size_t len);
  • Function: find the position of the last character 'Ch' in the string. If the character ch exists in s, return the pointer of the position where ch appears; Otherwise, NULL is returned.

14. Function strrchr

  • Prototype: char* strrchr (const char *s, int ch);
  • Function: find the position of the last occurrence of the given character ch in the string s, and r means starting from the end of the string

15. Function str

  • Prototype: char * str (const char * S1, const char * S2);
  • Function: find the position of the first occurrence of the specified string s2 in the string s1

20. Function strlen

  • Prototype: size_t strlen (const char *s);
  • Function: it is a function to test the length of the string. The value of the function is the actual length of the string (excluding "/ 0")

21. Function memset

  • Prototype: void* memset (void *s, int val, size_t len);
  • Function: set len bytes from s to val

23. Function_ strlwr

  • Prototype: char*_ strlwr( char *string );
  • Function: change the uppercase letters in the string into lowercase letters

24. Function_ strupr

  • Prototype: char*_ strupr( char *string );
  • Function: replace lowercase letters in the string with uppercase letters

#Include < cmath > common functions contained in header file

1,double pow(double x,double y); Calculate the y-power of X

2,double sqrt (double); Open square

3,(1). ceil() round up

      (2). round() round

      (3). floor() rounded down

4. Absolute value

      (1). int abs(int ); Find the absolute value of an integer

      (2). double fabs (double); Absolute value of realistic type

      (3). double cabs(complex); Find the absolute value of complex number

#Include < algorithm > header file

The use of sort function needs to add header file #include < algorithm > and using namespace std;

2. Dynamic allocation and static allocation of linear table

typedef struct{
    int data[MaxSize];
    int length;        //Current length
}SqList;            //Static definition of sequence table
typedef struct{
    int *data;
    int MaxSize;    //Maximum capacity
    int length;        //Current length
}SeqList;

Keywords: C++ Algorithm data structure

Added by parboy on Mon, 14 Feb 2022 13:59:50 +0200