数据结构课收获(第二周第一次课)

其中单链表的特点:单链表不要求逻辑上相邻的两个元素在物理位置上也相邻,因此不需要连续的存储空间.

心得体会:在上周第二节数据结构课上,我们进一步的学习了链式表的相关性质及其增添,删除等基本操作。同时具体学习了在基本操作过程的图标过程和数据过程。

具体知识点:了解到了链表是由n个节点由指针链组成的一个链表,其中链表是顺序存储的(这点与顺序表的随机存取不同),而每一个节点则是由数据域和指针域组成(这点符合结构体的性质,于是我们常常用结构体来定义节点)。

其中单链表的特点:单链表不要求逻辑上相邻的两个元素在物理位置上也相邻,因此不需要连续的存储空间.而顺序表则是随机储存的。

二、学习了空表的相应知识及其存在的合理性。

而我通过模仿的代码如下:

#include<stdio.h>
#include<malloc.h>
 
/**
 *Linked list of characters.The key is data.
 */
typedef struct LinkNode{
	char data;
	struct LinkNode *next;
}LNode,*LinkList,*NodePtr;
 
/**
 *Initialize the list with a header.
 *return the pointer to the header;
 */
LinkList initLinkList(){
	NodePtr tempheader = (NodePtr)malloc(sizeof(LNode));
	tempheader->data = '\0';
	tempheader->next = NULL;
	return tempheader;
}//Off initLinkList.
 
/**
 *Print the list.
 *param paraheader the header of the list.
 */
void printList(NodePtr paraheader){
	NodePtr p = paraheader->next;
	while(p != NULL){
		printf("%c",p->data);
		p = p->next;
	}//Off while.
	printf("\r\n");
}
 
/**
 *Add an element to the tail
 *param paraheader the header of the list.
 *param parachar the given char.
 */
void appendElement(NodePtr paraheader,char parachar){
	NodePtr p,q;
	
	//Step 1.Construct a new node.
	q = (NodePtr)malloc(sizeof(LNode));
	q->data = parachar;
	q->next = NULL;
	
	//Step 2.Search to the tail.
	p = paraheader;
	while(p->next != NULL){
		p = p->next;
	}//Off while.
	
	//Step 3.Now add.
	p->next = q;
}//Off appendElement.
 
/**
 *Insert an element to the given position.
 *param paraheader the header of the list.
 *param parachar the given char.
 *param paraposition the given position.
 */
void insertElement(NodePtr paraheader, char parachar, int paraposition){
	NodePtr p,q;
	
	//Step 1.research the position.
	p = paraheader;
	for(int i = 0; i < paraposition; i++){
		p = p->next;
		if (p == NULL){
			printf("The position %d is beyond the scope of the list.\n",paraposition);
			return;
		}//Off if.
	}//Off for i.
	
	//Step 2.Construct a new Node.
	q = (NodePtr)malloc(sizeof(LNode));
	q->data = parachar;
	
	//Step 3.Now add.
	printf("linking\r\n");
	q->next = p->next;
	p->next = q;
}//Off insertElement.
 
/**Delete an element from the list.
 *param paraheader the header of the list.
 *param parachar the given char.
 */
void deleteElement(NodePtr paraheader, char parachar){
	NodePtr p,q;
	p = paraheader;
	while((p->next != NULL) && (p->next->data != parachar)){
		p = p->next;
	}//Off while.
	
	if (p->next == NULL){
		printf("Cannot delete %c\r\n",parachar);
		return;
	}//Off if.
	
	q = p->next;
	p->next = p->next->next;
	free(q);
}//Off deleteELement.
 
/**
 *Unit test.
 */
void appendInsertDeleteTest(){
	//Step 1.Initialize an empty list.
	LinkList tempList = initLinkList();
	printList(tempList);
	
	//Step 2.Add some characters.
	appendElement(tempList,'H');
	appendElement(tempList,'e');
	appendElement(tempList,'l');
	appendElement(tempList,'l');
	appendElement(tempList,'o');
	appendElement(tempList,'!');
	printList(tempList);
	
	//Step 3.Delete some characters (the first occurrence)
	deleteElement(tempList,'e');
	deleteElement(tempList,'a');
	deleteElement(tempList,'o');
	printList(tempList);
	
	//Step 4. Insert to a given position.
	insertElement(tempList,'o',1);
	printList(tempList);
}//Off appendInsertDeleteTest.
 
/**
 *Address test: beyond the book.
*/
void basicAddressTest(){
	LNode tempNode1,tempNode2;
	
	tempNode1.data = 5;
	tempNode1.next = NULL;
	
	tempNode2.data = 7;
	tempNode2.next = NULL;
	
	printf("The first node: %p, %p, %p\r\n",&tempNode1,&tempNode1.data,&tempNode1.next);
	printf("The second node: %p, %p, %p\r\n",&tempNode2,&tempNode2.data,&tempNode2.next);
	
	tempNode1.next = &tempNode2;	
}//Off basicAddressTest.
 
//the entrance.
 
int main(){
	appendInsertDeleteTest();
	basicAddressTest(); 
}

其中的运行结果如下:

Hello!
Cannot delete a
Hll!
linking
Holl!
The first node: 000000000062FDE0, 000000000062FDE0, 000000000062FDE8
The second node: 000000000062FDD0, 000000000062FDD0, 000000000062FDD8

通过对增添append和删除delete函数的学习,更加深刻的了解到了单链表顺序存储的原理及其相关的操作,让我明白了敲代码前研究代码原理的重要性。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值