算法笔记-Linklist

This question is to implement the data structure and methods of linkedlist

ADT(Abstract data type):Linkedlist

typedef struct node{
	double data;
	struct node* next;
}Node;

Methods declaration

bool IsEmpty(Node* head);
Node* InsertNode(Node** phead, int index, double x);
int FindNode(Node* head, double x);
int DeleteNode(Node** phead, double x);
void DisplayList(Node* head);
Void DestroyList(Node** phead)

Code implementation

#include <stdio.h> 
#include <stdlib.h> 

//function:	tests whether a list is empty
bool IsEmpty(Node *head) 
{	
	if (!head) 
		return true;//if list is empty return true 
	else
		return false;//else return true 
}

// function: 	inserts a new node with certain data after a certain position
Node* InsertNode(Node** phead, int index, double x) { 
	if (index < 0) return 0; // if it is a invalid index , return 0

	int currIndex = 1; //set a currIndex to count the index
	Node* currNode = *phead; // set a currNode to move head pointer
	while (currNode && index > currIndex) {// use while to move the pointer
		currNode = currNode->next; 
		currIndex ++;
	} 
	if (index > 0 && currNode == 0) return 0;// if don have that index return 0

	Node* newNode = (Node*)malloc(sizeof(Node)); 
	newNode->data = x; //create a new node and give the value x to it
	if (index == 0) { // if it is the first node, insert it 
		newNode->next = *phead; 
		*phead = newNode; 
	} else {//  if it is not the first node, insert it 
		newNode->next = currNode->next; 
		currNode->next = newNode;
	} 
	return newNode;// return a new node if insertion is successful
}

// function: finds node with certain data
int FindNode(Node* head, double x) {
	int currIndex = 1;//set a currIndex to count the index
	while( head && head->data != x) {// use while loop to move the pointer and until reach x
		head = head->next;
		currIndex ++;
	}
	if(!head)// if list is not exist return 0
		return 0;
	else// else return currIndex
		return currIndex;
}

//function:	deletes a node with certain data
int DeleteNode(Node** phead, double x) {
	int currIndex = 1;//set a currIndex to count the index
	Node* currNode = *phead;// set a currNode to move head pointer
	Node* lastNode;// set a lastNode to remember last node

	if (currNode->data == x) {// if the first node is the one, delete it
		*phead = currNode->next;
	} else {// if it is not first, check the following
		while (currNode && currNode->data != x) {
			lastNode = currNode;
			currNode = currNode->next;
			currIndex ++;
		} 
		if(!currNode) return 0;// if x is not exist, return 0 
		else lastNode ->next = currNode->next;// if x exists, delete it
	}
		free(currNode);// free the currNode 
		return currIndex;// return the index of x
}

//function: 	prints all the nodes in the list
void DisplayList(Node* head) {
	if(!head)// if list is not exist print worning
		printf("The list is not exist!\n");
	while(head) {// if list exist print out the result 
		printf("%f\n", head->data);
		head = head->next;
	}
}

//function: 	deletes all the nodes in the list and frees the memory occupied by them
void DestroyList(Node** phead) {
	Node* currNode = *phead;
	Node* nextNode;
	if(!currNode) {// if list is not exist, print worning
		printf("This list is not exist!\n");
	} else {// else print out exist message and delete
		printf("This list is exist!\n");
		while (currNode) {
			nextNode = currNode->next;
			free(currNode);
			currNode = nextNode;
		}
		if(!currNode)//check if delete is complete and print out message
			printf("Delete complete!\n");
		else
			printf("Delete not complete!\n");
		*phead = NULL;// the head pointer be set to null
	}
}


int main() { 
	Node *head = 0; // create a empty list

	//insert 5 double number from 0 to 4 in index i and display list
	for(int i=0; i<5; i++)
		InsertNode(&head, i, i);
	DisplayList(head);
	printf("\n");

	//insert 5 double number from 0 to 4 in index 0 and display list
	for(int i=0; i<5; i++)
		InsertNode(&head, 0, i);
	DisplayList(head);
	printf("\n");

	//find out if 0,2,4,6 is in the list and print out
	for(int i=0; i<7; i+=2){
		int idx = FindNode(head, i);
		if(idx>0)
			printf("%d is at position %d.\n", i, idx);
		else
			printf("%d is not in the list.\n", i);
	}
	printf("\n");

	//delete specific unmber in the list and display list
	DeleteNode(&head, 0);
	DisplayList(head);
	printf("\n");

	// destory the list and print to check
	DestroyList(&head);
	DisplayList(head);
}  

代码详解

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值