//记录一下做lab的心路历程 期末复习的时候用
链表: 一种线性的ADT (此处为Node的英译)
链表分为数据域和指针域,数据域可以为任何自定义数据类型,也可以是char double这些基本数据类型。指针域恆指向下一个链表
(头链表不会被Node里的next指向,所以一般使用时,只保存head node的指针域,剩下的都依靠方法去遍历。)
definetion of Node
typedef struct node{
double data;
struct node* next;
} Node;
(the type of variable data can be changed in whatever you want)
0. implementation of Link (usually in main() for C)
int main()
{
Node *head = 0;
for(int i=0; i<5; i++)
InsertNode(&head, i, i);
}
//题目要求
bool IsEmpty(Node* head);
/*
function:
tests whether a list is empty
input:
head - pointer to the first node
output:
true if the list is empty and false otherwise
*/
Node* InsertNode(Node** phead, int index, double x);
/*
function:
inserts a new node with certain data after a certain position
input:
phead - pointer to the pointer to the first node
index - the new node is inerted after position index
insert a new node as the head if index=0
x - data of the new node
output:
a pointer to the new node if insertion is successful and NULL otherwise
*/
int FindNode(Node* head, double x);
/*
function:
finds node with certain data
input:
head - pointer to the first node
x - the first node whose data=x is returned
output:
returns the position of the first node whose data=x
returns 0 if no such node exists
*/
int DeleteNode(Node** phead, double x);
/*
function:
deletes a node with certain data
input:
phead - pointer to the pointer to the first node
x - the first node whose data=x is deleted
output:
returns the position of the deleted node
returns 0 if no such node exists
*/
void DisplayList(Node* head);
/*
function:
prints all the nodes in the list
input:
head - pointer to the first node
*/
void DestroyList(Node** phead);
/*
function:
deletes all the nodes in the list and frees the memory occupied by them
input:
phead - pointer to the head
*/
1. IsEmpty
return true if the Link is empty
return false if not
//check the condition of Node though head pointer.
bool IsEmpty(Node* head)
{
if(head==NULL)
return true;
else
return false;
}
2.Insert Node
When index = 0, the new node will replace previous head node.
Node* InsertNode(Node** phead, int index, double x)
// Node**head means the address of node pointer?
//within phead in the insert function, user can change the head of node
{
if (index<0)
return 0;
int currentindex = 1;//the index number of node
Node* currentNode = *phead;//此处实际上是对Node**phead的一次解引用
while(currentNode&& index>currentindex)
{
currentNode = currentNode->next;// let pointer point next node
currentindex++;
}
if(index>0&¤tNode==0) return 0;
//dealing with index bigger than biggest index of node
Node *newNode = (Node*)malloc(sizeof(node));//create new node
newNode->data = x;// the data of new node depends on input
if(index==0)//插入头节点
{
newNode->next = *phead;
//when index =0 , the added node will be new node
//but before added, lined previous head node as the next of new node
*phead = newNode;
}
else{
newNode->next = currentNode->next;
currentNode->next = newNode;
}
return newNode;
}
3.Display
/*
function:
prints all the nodes in the list
input:
head - pointer to the first node
*/
void DisplayList(Node* head)
{
if(head==NULL)
printf("The node is empty! No data inside!\n");
else
{
int currentIndex = 1;
Node *cUrrentNode = head;
//do head node have data or not?
while(cUrrentNode!=NULL)
{
printf("Here is the NO.%d node, and the data is: %.1f.\n", currentIndex++, cUrrentNode->data);
cUrrentNode = cUrrentNode->next;//point next node
}
printf("DISPLAY ENDING\n\n");
}
}
4. Find
/*
function:
finds node with certain data
input:
head - pointer to the first node
x - the first node whose data=x is returned
output:
returns the position of the first node whose data=x
returns 0 if no such node exists
*/
int FindNode(Node* head, double x)
{
Node *currentNode = head;
int currentIndex = 1;
//链表结构本身并没有index的设计
//想要用index 一是在遍历中计数,二是可以设计在自定义data里
//(但二的index会因链表结构删改而失去意义)
bool answer = false;
while (currentNode!=NULL)//while循环用于遍历Node
{
if(currentNode->data==x)
//如果 想要靠index来定向查找 在此处换成 currentIndex== index(输入参数) 即可
{
answer = true;
break;
}
currentIndex++;
currentNode = currentNode->next;
//指针指向下一个Node(不能直接+1)
//因为在Insertfunction里可以看到
//Node的指针是malloc()出来的 不是连续内存
}
if(answer)
return currentIndex;
else
return 0;
}
5.Delete
/*
function:
deletes a node with certain data
input:
phead - pointer to the pointer to the first node
x - the first node whose data=x is deleted
output:
returns the position of the deleted node
returns 0 if no such node exists
*/
int DeleteNode(Node** phead, double x)
{
Node *currentNode = *phead;
Node *lastNode = NULL;
bool result = false;
int currentIndex = 1;
while (currentNode!=NULL)
{
if(currentNode->data==x)// when the value match,start deleting
{
if(currentIndex==1)//delete the head node
{
currentNode = (*phead)->next;
free(*phead);
*phead = currentNode;
}
else{
lastNode->next = currentNode->next;
}
result = true;//update condition of operation:sucessful or not
break;
}
currentIndex++;
lastNode = currentNode;
currentNode = currentNode->next;
}
lastNode=NULL;
currentNode=NULL;
if(result)
return 0;
else
return currentIndex;
}
6. Destroy
/*
function:
deletes all the nodes in the list and frees the memory occupied by them
input:
phead - pointer to the head
*/
void DestroyList(Node** phead)
{
Node *tempNode = *phead;
while(*phead!=NULL)
{
tempNode = (*phead)->next;//save the address of next node
free(*phead);//free the address of *phead - change the value of currentNode
*phead = tempNode;//let the pointer of Node 's address point (previous next node)
}
tempNode=NULL;//防止野指针越权
//free(whole phead at the end of main fuction,
//wherther we will lose way to check Node is Empty or not)
}