c链表联系

每天写一点点,记录脑残的那些日子

#include 
   
   
    
    
#include 
    
    
     
     

struct student{
	char* name;
	struct stutent* next;
};

typedef struct student Student;

/**
 * create chain
 */
Student* create();
/**
 * print chain
 */
void show(Student* head);
/**
 * delete chain
 */
Student* delete(Student* head,int index);
/**
 * insert element
 */
Student* insert(Student* head,Student* s,int index);
/**
 * add element
 */
Student* add(Student* head,Student* s);
/**
 * clear chain
 */
Student* clear(Student* head);
int main(){
	//test create
	Student* head = create();
	show(head);

	//test add
	Student* s1 = (Student*)malloc(sizeof(Student));
	if(!s1)return 0;
	s1->name = "add";
	s1->next = NULL;
	head = add(head,s1);
	show(head);

	//test insert
	Student* s2 = (Student*)malloc(sizeof(Student));
	if(!s2)return 0;
	s2->name = "insert 1";
	s2->next = NULL;
	head = insert(head,s2,2);
	Student* s3 = (Student*)malloc(sizeof(Student));
	if(!s3)return 0;
	s3->name = "insert 2";
	s3->next = NULL;
	head = insert(head,s3,0);
	Student* s4 = (Student*)malloc(sizeof(Student));
	if(!s4)return 0;
	s4->name = "insert 3";
	s4->next = NULL;
	head = insert(head,s4,12);
	show(head);

	Student* s5 = (Student*)malloc(sizeof(Student));
	if(!s5)return 0;
	s5->name = "insert at the end!";
	s5->next = NULL;
	head = insert(head,s5,13);
	show(head);

	Student* s6 = (Student*)malloc(sizeof(Student));
	if(!s6)return 0;
	s6->name = "insert out of rang!";
	s6->next = NULL;
	head = insert(head,s6,20);
	show(head);


	//test delete
	head = delete(head,0);
	head = delete(head,1);
	head = delete(head,10);
	head = delete(head,20);
	show(head);

	//test clear
	head = clear(head);
	show(head);

	return 0;
}

void show(Student* head){
	printf("-----------------enter show method-----------------------\n");
	if (head==NULL){
		printf("there is no element in the chain!\n");
		return;
	}
	Student* current = head;
	Student* next = current->next;
	int index = 0;
	while(1){
		printf("index:%d, name:%s\n",index,current->name);
		if (next!=NULL){
			current = next;
			next = current->next;
			index++;
		}else
			break;
	}
}

Student* clear(Student* head){
	printf("-----------------enter clear method-----------------------\n");
	if(head==NULL){
		printf("null point\n");
		return head;
	}
	Student* current = head;
	Student* next = current->next;
	while(1){
		free(current);
		if(next!=NULL){
			current = next;
			next = current->next;
		}
		else{
			printf("clear success\n");
			head = NULL;
			break;
		}
	}
	return head;
}
Student* delete(Student* head,int index){
	printf("----------------enter delete method------------------\n");
	if(head==NULL){
		printf("null point\n");
		return NULL;
	}
	Student* current = head;
	Student* next = current->next;
	Student* last = NULL;
	int position = 0;
	while(1){
		if(next!=NULL){
			if (position==index){
				if(last==NULL){
					head = next;
					free(current);
				}else{
					last->next = next;
					free(current);
				}
				break;
			}
			last = current;
			current = next;
			next = current->next;
			position++;
		}
		else{
			if (position==index){
				if(last==NULL){
					head = next;
					free(current);
				}else{
					last->next = next;
					free(current);
				}
				break;
			}else
				printf("index out of rang\n");
			break;
		}
	}
	return head;
}

Student* insert(Student* head,Student* s,int index){
	printf("----------------enter insert method------------------\n");
	if(head==NULL){
		if (index==0)
			head = s;
		else
			printf("index out of rang\n");
		return head;
	}
	Student* current = head;
	Student* next = current->next;
	Student* last = NULL;
	int position = 0;
	while(1){
		if(next!=NULL){
			if(position==index){
				if(last==NULL){
					s->next = current;
					head = s;
				}else{
					last->next = s;
					s->next = current;
				}
				break;
			}
			last = current;
			current = next;
			next = current->next;
			position++;
		}
		else{
			if(position==index){
				if(last==NULL){
					s->next = current;
					head = s;
				}else{
					last->next = s;
					s->next = current;
				}
			}else
				printf("index out of rang\n");
			break;
		}
	}
	return head;
}

Student* add(Student* head,Student* s){
	printf("----------------enter add method------------------\n");
	if (head==NULL){
		head = s;
		return head;
	}
	Student* current = head;
	Student* next = current->next;
	while(1){

		if(next==NULL){
			current->next = s;
			break;
		}else{
			current = next;
			next = current->next;
		}
	}
	return head;
}

Student* create(){
	printf("----------------enter create method------------------\n");
	Student* head = (Student*)malloc(sizeof(Student));
	if(!head)return NULL;
	Student* tail;
	head->name = (char*)malloc(1024);
	printf("please enter name:");
	if(!head->name)return NULL;
	scanf("%s",head->name);
	head->next = NULL;
	tail = head;
	int i=1;
	while(i<10){
		printf("please enter name:");
		Student* temp = (Student*)malloc(sizeof(Student));
		if(!temp)return NULL;
		temp->name = (char*)malloc(1024);
		temp->next = NULL;
		scanf("%s",temp->name);
		tail->next = temp;
		tail = temp;
		i++;
	}
	return head;
}

    
    
   
   

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值