#include<stdio.h>
#include<malloc.h>
typedef struct linkNode
{
char data;
struct linkNode*next;
} LNode,*LinkList,*NodePtr;
LinkList initLinkList(){
NodePtr p=(NodePtr)malloc(sizeof(LNode));
p->data='\0';
p->next=NULL;
return p;
}
void printList(NodePtr pH){
NodePtr q=pH->next;
while(q!=NULL){
printf("%c",q->data);
q=q->next;
}
printf("\r\n");
}
void appendElement(NodePtr pH,char pC){
NodePtr p,q;
q=(NodePtr)malloc(sizeof(LNode));
q->data=pC;
q->next=NULL;
p=pH;
while(p->next!=NULL){
p=p->next;
}
p->next=q;
}
void InsertElement(NodePtr pH,char pC,int Position){
NodePtr p,q;
p=pH;
for(int i=0;i<Position;i++){
p=p->next;
if(p==NULL){
printf("The position %d is beyond the scope of the list.",Position);
return;
}
}
q=(NodePtr)malloc(sizeof(LNode));
q->data=pC;
printf("linking\r\n");
q->next=p->next;
p->next=q;
}
void deleteElement(NodePtr pH,char pC){
NodePtr p,q;
p=pH;
while((p->next!=NULL)&&(p->next->data!=pC)){
p=p->next;
}
if(p->next==NULL){
printf("Cannot delete %c\r\n",pC);
return;
}
q=p->next;
p->next=q->next;
free(q);
}
void appendInsertDeleteTest(){
NodePtr tempList=initLinkList();
printList(tempList);
appendElement(tempList,'H');
appendElement(tempList,'e');
appendElement(tempList,'l');
appendElement(tempList,'l');
appendElement(tempList,'o');
appendElement(tempList,'!');
printList(tempList);
deleteElement(tempList,'e');
deleteElement(tempList,'a');
deleteElement(tempList,'o');
printList(tempList);
InsertElement(tempList,'o',1);
printList(tempList);
}
void basicAddressTest(){
LNode N1,N2;
N1.data=4;
N1.next=NULL;
N2.data=6;
N2.next=NULL;
printf("The first node:%d,%d,%d\r\n",&N1,&N1.data,&N1.next);
printf("The second node:%d,%d,%d\r\n",&N2,&N2.data,&N1.next);
N1.next=&N2;
}
int main(){
appendInsertDeleteTest();
return 0;
}