#include <stdio.h>
#include <malloc.h>
typedef struct LinkNode{
char data;
struct LinkNode *next;
} LNode, *LinkList, *NodePtr;
LinkList initLinkList(){
NodePtr tempHeader = (NodePtr)malloc(sizeof(LNode));
tempHeader->data = '\0';
tempHeader->next = NULL;
return tempHeader;
}
void printList(NodePtr paraHeader){
NodePtr p = paraHeader->next;
while (p != NULL) {
printf("%c", p->data);
p = p->next;
}
printf("\r\n");
}
void appendElement(NodePtr paraHeader, char paraChar){
NodePtr p, q;
q = (NodePtr)malloc(sizeof(LNode));
q->data = paraChar;
q->next = NULL;
p = paraHeader;
while (p->next != NULL) {
p = p->next;
}
p->next = q;
}
void insertElement(NodePtr paraHeader, char paraChar, int paraPosition){
NodePtr p, q;
p = paraHeader;
int i;
for (i = 0; i < paraPosition; i ++) {
p = p->next;
if (p == NULL) {
printf("The position %d is beyond the scope of the list.", paraPosition);
return;
}
}
q = (NodePtr)malloc(sizeof(LNode));
q->data = paraChar;
printf("linking\r\n");
q->next = p->next;
p->next = q;
}
void deleteElement(NodePtr paraHeader, char paraChar){
NodePtr p, q;
p = paraHeader;
while ((p->next != NULL) && (p->next->data != paraChar)){
p = p->next;
}
if (p->next == NULL) {
printf("Cannot delete %c\r\n", paraChar);
return;
}
q = p->next;
p->next = p->next->next;
free(q);
}
void appendInsertDeleteTest(){
LinkList 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 tempNode1, tempNode2;
tempNode1.data = 4;
tempNode1.next = NULL;
tempNode2.data = 6;
tempNode2.next = NULL;
printf("The first node: %d, %d, %d\r\n",
&tempNode1, &tempNode1.data, &tempNode1.next);
printf("The second node: %d, %d, %d\r\n",
&tempNode2, &tempNode2.data, &tempNode2.next);
tempNode1.next = &tempNode2;
}
int main(){
appendInsertDeleteTest();
}
首先,坦白从宽,抗拒从严,不得不承认,这一节我没有学懂。上节课上课不知道这么的,竟然睡着了,我不想睡觉,但睡着了,然后我就什么也没有学到,所以我抄代码就真的只是在抄代码,不理解其中的奥秘。还有另外一点也不得不说,我好像不是特别喜欢计算机这门专业,所以上学期学习c语言的时候也只是应付,只学到了数组,字符串,指针等我自己都没有学习。所以从基础上就已经落后别人太多了。但我又不是很喜欢这门专业,不知道怎么样才能去提起学习的兴趣。究其本质,还是我有一颗摆烂的心没有调整,爬山不走最陡峭的以最快速度上山,也不去弯弯曲曲的绕路上山,而是选择不上山。