Experimental data structure topics

1, a known single list L (lead node) is an increasing order table, the test preparation of an algorithm, the table is deleted node is greater than min and less than max (if there is such a node table), while the release point truncated space, where min and max are two given parameters. Analyze the time complexity of the algorithm.

#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct LNode{
	int a;
	LNode *next;
};
LNode *init_List(int n){              //初始化链表 
	LNode *L;
    L=(LNode *)malloc(sizeof(LNode));
    L->next=NULL;
	LNode *p;
	LNode *r = L; 
	if(n!=0){
		int x;
	    while(n--){
		    scanf("%d",&x);
	    	p=(LNode *)malloc(sizeof(LNode));
	    	p->a = x;
	    	p->next = NULL; 
	    	r->next = p;              //使用尾插法 
	    	r = p;
		}	
	}
	return L;	
}
void print_List(LNode *L){            //输出链表内容 
	LNode *p=L->next;
	if(L->next==NULL){
		printf("-1\n");
		return;
	}
    while(p->next != NULL){
		printf("%d ",p->a); p = p->next;
	}
	printf("%d\n",p->a);
} 

LNode *delete2(LNode *L,int min,int max){       //删除指定区域内的节点 
	LNode* p=L->next,*q=L,*r;
	if(L->next==NULL){
		return L;
	}
	while(p!=NULL){
		if(p->a > min && p->a < max){
			r=p;
			q->next=p->next;
			p=q->next;
        	free(r);                           //释放被删除节点的空间 
		}
		else{
			p=p->next;
		    q=q->next;
		}	
	}
	return L;
}
int main(){
	int T,len,q,type,min,max;
	LNode * L;
	scanf("%d",&T);
	while(T--){
		scanf("%d",&len);
		L=init_List(len);
		scanf("%d",&q);
		    while(q--){
		    	scanf("%d",&type);
		    	if(type==1){
		    		print_List(L);
				}
				else if(type==2){
					scanf("%d%d",&min,&max);
					L=delete2(L,min,max);
				}
			}	
	}
	return 0;
}

2, Josephus problem

1) Problem Description: There are numbered 1, 2 ... n n individuals in a clockwise direction around a circle, each holding a positive integer password. Given a positive integer starts m, clockwise from a start number of packets from the first person, who report the m columns, no longer participate in the number of packets, then the column's password as shown m, from the column by the next person in a clockwise direction to start again from the beginning of the number 1 newspaper. And so on until everyone is out of the line. Test design algorithm, a sequence column's output.

2) Experimental requirements: sequential chain and two storage structure to achieve

#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct Node{
	int m;                  //m记录密码 
	int num;                //记录初始的位置 
	Node *next;             //下一个节点 
}; 
int count;
Node* init_List(int n){
	int num=1;
	Node* L,*r,*p;
	int m;
	L = (Node*)malloc(sizeof(Node));
	L->next = NULL;
	r=L;                   //尾插法 
	if(n==0) return L;
	else{
		count=0;
		while(n--){
			p = (Node*)malloc(sizeof(Node));
			scanf("%d",&m);
			count++;       //记录节点数 
			p->m = m;
			p->num = num++; 
			p->next = NULL;
			r->next = p;
			r = p;
		}
	}
	r->next = L->next;  
	return L;
}
void xunhuan_List(Node* L,int m){
	Node *p=L->next,*r=L,*q1;
	int m1=m;
	while(count != 1 ){          //原表中剩一节点则结束 
		m1-=1;                   //第一次从本身开始
	    while(m1--){
		    r=r->next;
		    p=p->next;
	    }
	    printf("%d ",p->num); 	    
	    count--; 
	    if(p->m!=0) m1=p->m;
	    else m1=m;
	    r->next = p->next;       //改变原来的链接关系 
	    p = r->next;
	}
	printf("%d\n",p->num);
}

int main(){
	int a,n,m;
	scanf("%d",&a);
	while(a--){
		Node *L;
	    scanf("%d",&n);	
		L = init_List(n);
		scanf("%d",&m);             //初始密码 
		xunhuan_List(L,m);          //循环 
	} 
	return 0;
}

3. one yuan sparse polynomial simple calculator

1) Description of the problem: yuan sparse polynomial represents a linear table, a design univariate polynomial arithmetic.

2) Experimental requirements: single linked list storage structure one yuan sparse polynomial, the input and the establishment of a polynomial, polynomial output achieve polynomial addition, subtraction operations.

 

#include<iostream>           //多项式问题 
#include<cstdlib>
using namespace std;
typedef struct Node{
	int x;
	int y;
	Node* next;
}; 
Node* init_List(int n){
	Node *L,*p,*r;
	int x,y;
	L=(Node*)malloc(sizeof(Node));
	L->next = NULL;
	r=L;
	if(n==0){                 //使用尾插法 
		return L;
	}
	else{
		while(n--){
			scanf("%d%d",&x,&y);
			p=(Node*)malloc(sizeof(Node));
			p->x=x;
			p->y=y;
			p->next = NULL;
			r->next = p;
			r=p;
		}
	}
	return L;
}
void print_List(Node *L){            //输出链表内容 
	Node *p=L->next;
	if(L->next==NULL){
		printf("-1\n");
		return;
	}
	if(p->x>0){
		printf("%dX^%d",p->x,p->y);
		p=p->next; 
	}
	if(p==NULL){
		printf("\n");
		return;
	}
	else{
	    while(p->next != NULL){
    	if(p->x>0){
    		printf("+%dX^%d",p->x,p->y); 
		    p = p->next;
		}
		else if(p->x<0){
			printf("%dX^%d",p->x,p->y); 
		    p = p->next;
		}
		else if(p->x == 0){
			printf("");
			p=p->next;
		}
	}
	if(p->x>0){
		printf("+%dX^%d\n",p->x,p->y); 
	}
	else if(p->x<0){
		printf("%dX^%d\n",p->x,p->y); 
	}
	else if(p->x == 0){
		printf("\n");
	}	 
	}
 
} 
Node* jian_List(Node* L1,Node* L2){
	Node *p1=L1,*p2=L2->next,*r;
	int flag;
	while(p2!=NULL){
		p1=L1->next;
		flag=0;
	    while(p1!=NULL){
	    	if(p1->y==p2->y){
	    		flag=1;
	    		p1->x =p1->x - p2->x;
			}
			p1=p1->next;
		}
		if(flag==0 && p1==NULL){
			Node* p;
			r=L1->next;
			while(r->next!=NULL) r=r->next;
			p=(Node*)malloc(sizeof(Node));
			p->x = -(p2->x);
			p->y = p2->y; 
			p->next = NULL;
			r->next = p;
			r=p;	
		}
		p2=p2->next;
	} 
	return L1;
}
Node* add_List(Node* L1,Node* L2){
	Node *p1=L1,*p2=L2->next,*r;
	int flag;
	while(p2!=NULL){
		p1=L1->next;
		flag=0;
	    while(p1!=NULL){
	    	if(p1->y == p2->y){
	    		flag=1;
	    		p1->x = p1->x + p2->x;
			}
			p1=p1->next;
		}
		if(flag==0 && p1==NULL){
			Node* p;
			r=L1->next;
			while(r->next!=NULL) r=r->next;
			p=(Node*)malloc(sizeof(Node));
			p->x = p2->x;
			p->y = p2->y; 
			p->next = NULL;
			r->next = p;
			r=p;	
		}
		p2=p2->next;
	}
	return L1;
}
int main(){
	int T,m,n,p,type;
	scanf("%d",&T);
	while(T--){
		Node *L1,*L2,*L21,*L22;
	    scanf("%d%d",&m,&n);
		L1=init_List(m);             //初始化多项式1 
		L2=init_List(n);             //初始化多项式2 
		scanf("%d",&p);
		while(p--){
			scanf("%d",&type);
			if(type==1){
				print_List(L1);
				print_List(L2);
			}
			else if(type==2){        //加运算 
				L1 = add_List(L1,L2);
			}
			else if(type==3){        //减运算 
				L1 = jian_List(L1,L2);
			}
		}	
	}
	return 0;
}

4. decimal number to binary conversion of N

1) Problem Description: Converts a decimal number from the keyboard input is N (such as binary, octal, hexadecimal) hexadecimal data.

2) Experimental requirements: Stack used sequential number conversion implemented problem

3) to achieve prompt:

  • Conversion method using Euclidean algorithm;
  • N-nary number converted by the low to high sequentially generated, and the normal output from high to low, the calculation is just the opposite, so each of the conversion process to give an N-ary number is saved into the stack, the conversion turn the stack after completion of the conversion result is just.

4) attention to the problem:

  • When the stack, the stack

       Algorithm termination condition

#include<iostream>
using namespace std;
#include<cstdlib>
#include<cstring>
#define Max_Size 100
typedef struct SeqStack {
	char data[Max_Size];
	int top;
};
SeqStack *initStack() {        //初始化栈 
	SeqStack *s;
	s = (SeqStack *)malloc(sizeof(SeqStack));
	if (s != NULL)
		s->top = -1;
	return s;
}

void Push(SeqStack *s, int x) {  //入栈 
	if (s->top == Max_Size) {
		return;
	}
	else {
		s->top++;
		s->data[s->top] = x;
	}
	return;
}

int Pop(SeqStack *s) {          //出栈 
	char temp;
	if (s->top == -1) {
		return 0;
	}
	else {
		temp = s->data[s->top];
		s->top--;
	}
	printf("%c",temp);
}
int main(){
	SeqStack *s;
	s = initStack();           //2<=n<=36 
    int x,n;
    scanf("%d%d",&x,&n);
    int count=0;
    if(x == 0){ 
    	Push(s,0);
    }
    int r;
    while(x != 0){            //中间利用辗转相除法 
        r = x % n; 
        if(r >= 10 && r<=35){ //大于10就转化为相应的字母 
            Push(s,r+55);     //根据ASCALL码转换
		}
        else{                 //小于10就转化为相应数字 
            Push(s,r+'0');
        }
        count++;
        x /= n;
    }
    while(s->top != -1) {  //循环依次输出栈中的内容 
        Pop(s);	
	}
    return 0;
}

5. Analyzing "palindrome" Problem

1) Problem Description: The so-called palindrome refers to the front along the back read from back to front and back read all the same string.

For example, did; pop; I was able elba saw I and the like.

2) Experimental requirements: programming, using the stack frame is determined whether a character string is "palindrome."

3) to achieve prompt:

From left to right character encountered, and if the top element relatively, if not equal, character stack, if equal, the stack. This continues, if the stack is empty, the string is "palindrome", otherwise not.

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std; 
typedef struct Node{          //栈 
	int data[200];
	int top;
};
Node *init_List(){           //建立栈 
	Node *L;
	L = (Node*)malloc(sizeof(Node));
	L->top=-1;
	return L;
} 

Node* push(Node* L,char x){   //入栈 
	L->top++;
	L->data[L->top] = x; 
	return L;
} 

Node* pop(Node* L){         //出栈 
	L->top--;
	return L;
}

int main(){
	char a[200],b;
	int i=0;
    Node* L;
	L = init_List();
	int n=0; 
	
	while((b=getchar()) && b!='\n'){
		a[i++] = b;
		n++;
	} 
	
	if(n%2==1){
		for(i=(n-1)/2;a[i]!='\0';i++){    //中间数字之后的前移 
			a[i] = a[i+1];
		}
		n=n-1;
	}                                     //end 全部变成偶数个字符 
	
	for(i=0;i<n/2;i++){                  //前n/2个入栈 
		L = push(L,a[i]);
	}                                
	
	for(i=n/2;L->top!=-1;i++){           //从第n/2个开始逐个与栈内的元素对比 
		if(a[i] != L->data[L->top]){
			printf("NO"); 
			break;
		}
		L = pop(L);
	}
	if(L->top == -1){
		printf("YES");
	} 
	return 0;
}

 

 

Guess you like

Origin blog.csdn.net/qq_41106517/article/details/94717461