每天写一点点,记录脑残的那些日子
#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;
}