데이터 구조 단일 체인 표 첨삭 검사

20000 단어 데이터 구조
#include
#include
using namespace std;
typedef struct Node{
	int num;
	Node *next;
}Node;//    
void init(Node *head,int number){//       
	Node *head2=head;
	for(int i=0;i<number;i++){//   
		Node *node=(Node *)malloc(sizeof(Node));//      ,   malloc    ,                   
		node->num=i;//     
		head2->next=node;//               next,    
		head2=node;//      ,         ,      
	}
	head2->next=NULL;//      ,next    NULL

}
/*
	     
*/
void print(Node *head){
	head=head->next;//       ,           
	while(head!=NULL){
		cout<<head->num<<" ";
		head=head->next;//        
	}
	cout<<endl;
}


/*
	add(   ,    (0  ),    )
	    1
	       
*/
int add(Node *head,int pos,int number){
	//number,      
	int i=0;
	Node *head1=head,*head2=NULL;
	if(head->next!=NULL)head2=head->next;
	do{
		if(i==pos){
			Node *node=(Node *)malloc(sizeof(Node)),*a;
			node->num=number;
			a=head1->next;
			head1->next=node;
			node->next=a;
			return 1;//ok
		}
		if(head2==NULL)return -2;
		head1=head2;
		head2=head2->next;

	}while(i++<pos&&head1!=NULL&&head2!=NULL);// 0  
	return -1;
}
/*
	deletes(   ,     );      0   
	    1
	       
*/
int deletes(Node *head,int pos){
	int i=0;
	Node *head1=head,*head2=NULL;
	if(head->next!=NULL)head2=head->next;
	else return -3;//     
	do{
		if(i==pos){
			head1->next=head2->next;
			free(head2);
			return 1;//ok
		}
		// cout<
		if(head2==NULL)return -2;
		head1=head2;
		head2=head2->next;

	}while(i++<pos&&head1!=NULL&&head2!=NULL);// 0  
	return -1;
}
/*
	change(   ,     (0  ),     )
	    1
	       
*/
int change(Node *head,int pos,int number){
	int i=0;
	if(head->next!=NULL)head=head->next;
	do{
		if(i==pos){
			head->num=number;
			return 1;
		}
		head=head->next;
	}while(i++<pos&&head!=NULL);// 0  
	return -1;//    :pos  
}
/*
	check(   ,     (0  ))
	        
	    -99999
*/
int check(Node *head,int pos){
	int i=0;
	if(head->next!=NULL)head=head->next;
	do{
		if(i==pos){
			return head->num;
		}
		head=head->next;
	}while(i++<pos&&head!=NULL);// 0  
	return -99999;//    :pos  
}

int main(int argc, char const *argv[])
{
	Node *head=(Node *)malloc(sizeof(Node));
	head->next=NULL;

	init(head,5);
	print(head);

	// cout<
	// cout<
	// cout<



	// cout<
	// print(head);

	// cout<
	// print(head);

	cout<<change(head,2,12)<<endl;//       
	print(head);

	return 0;
}

좋은 웹페이지 즐겨찾기