typedefstruct node{int val;struct node* next;} MyLinkedList;/** Initialize your data structure here. */
MyLinkedList*myLinkedListCreate(){
MyLinkedList* H;
H=(MyLinkedList*)malloc(sizeof(MyLinkedList));
H->next=NULL;return H;}/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */intmyLinkedListGet(MyLinkedList* obj,int index){int flag,count,res;
flag=count=0;
MyLinkedList* p=obj;while(p->next!=NULL){
p=p->next;if(count==index){
res=p->val;
flag=1;}
count++;}if(!flag){
res=-1;}return res;}/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */voidmyLinkedListAddAtHead(MyLinkedList* obj,int val){
MyLinkedList* p;
p=(MyLinkedList*)malloc(sizeof(MyLinkedList));
p->val=val;
p->next=obj->next;
obj->next=p;}/** Append a node of value val to the last element of the linked list. */voidmyLinkedListAddAtTail(MyLinkedList* obj,int val){
MyLinkedList* p,*q;
q=(MyLinkedList*)malloc(sizeof(MyLinkedList));
p=obj;while(p->next!=NULL){
p=p->next;}
p->next=q;
q->val=val;
q->next=NULL;}/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */voidmyLinkedListAddAtIndex(MyLinkedList* obj,int index,int val){
MyLinkedList* p,*q;
q=(MyLinkedList*)malloc(sizeof(MyLinkedList));
p=obj;int count,length;
count=length=0;while(p->next){
length++;
p=p->next;}
p=obj;if(index<0){myLinkedListAddAtHead(obj,val);}elseif(length==index){myLinkedListAddAtTail(obj,val);}else{while(p->next!=NULL){if(count==index){
q->next=p->next;
q->val=val;
p->next=q;break;}
p=p->next;
count++;}}}/** Delete the index-th node in the linked list, if the index is valid. */voidmyLinkedListDeleteAtIndex(MyLinkedList* obj,int index){
MyLinkedList* p,*q;
p=obj;int count=0;while(p->next!=NULL){if(count==index){
q=p->next;
p->next=q->next;free(q);break;}
p=p->next;
count++;}}voidmyLinkedListFree(MyLinkedList* obj){
MyLinkedList* p,*q;
p=obj;
q=p->next;while(q){free(p);
p=q;
q=q->next;}free(p);}
采用双链表形式,设计一个链表:
typedefstruct node{int val;struct node* next;struct node* pre;} MyLinkedList;/** Initialize your data structure here. */
MyLinkedList*myLinkedListCreate(){
MyLinkedList* H;
H=(MyLinkedList*)malloc(sizeof(MyLinkedList));
H->next=NULL;return H;}/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */intmyLinkedListGet(MyLinkedList* obj,int index){int flag,count,res;
flag=count=0;
MyLinkedList* p=obj;while(p->next!=NULL){
p=p->next;if(count==index){
res=p->val;
flag=1;}
count++;}if(!flag){
res=-1;}return res;}/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */voidmyLinkedListAddAtHead(MyLinkedList* obj,int val){
MyLinkedList* p;
p=(MyLinkedList*)malloc(sizeof(MyLinkedList));
p->val=val;
p->next=obj->next;
p->pre=obj;if(obj->next){
obj->next->pre=p;}
obj->next=p;}/** Append a node of value val to the last element of the linked list. */voidmyLinkedListAddAtTail(MyLinkedList* obj,int val){
MyLinkedList* p,*q;
q=(MyLinkedList*)malloc(sizeof(MyLinkedList));
p=obj;while(p->next!=NULL){
p=p->next;}
p->next=q;
q->val=val;
q->next=NULL;
q->pre=p;}/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */voidmyLinkedListAddAtIndex(MyLinkedList* obj,int index,int val){
MyLinkedList* p,*q;
q=(MyLinkedList*)malloc(sizeof(MyLinkedList));
p=obj;int count,length;
count=length=0;while(p->next){
length++;
p=p->next;}
p=obj;if(index<0){myLinkedListAddAtHead(obj,val);}elseif(length==index){myLinkedListAddAtTail(obj,val);}else{while(p->next!=NULL){if(count==index){
q->next=p->next;
q->pre=p;
q->val=val;
p->next->pre=q;
p->next=q;break;}
p=p->next;
count++;}}}/** Delete the index-th node in the linked list, if the index is valid. */voidmyLinkedListDeleteAtIndex(MyLinkedList* obj,int index){
MyLinkedList* p,*q;
p=obj;int count=0;while(p->next!=NULL){if(count==index){
q=p->next;
p->next=q->next;if(q->next){
q->next->pre=p;}free(q);break;}
p=p->next;
count++;}}voidmyLinkedListFree(MyLinkedList* obj){
MyLinkedList* p,*q;
p=obj;
q=p->next;while(q){free(p);
p=q;
q=q->next;}free(p);}