链表基本用法(添加节点、删除节点、清空节点、显示所有节点)

 

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

typedef struct stNode{
	int data;
	struct stNode * next;
}Node;
Node *addNode(Node *head);
void clearList(Node *head);
void displyNode(Node *head);
int deleteNode(Node *head, int n);
Node * ReverseList(Node * pHead) ;//链表反转
int main()
{
	
	int cntNode = 0;
	char c;
	Node *g_head = NULL; //头指针
	int n = 0;
	int data;
	if(g_head == NULL){
		//头结点不放数据,在创建个节点
		g_head = (Node *)malloc(sizeof(Node));
		if(g_head == NULL){
			printf("NO enough momery to allocate!\n");
			exit(0);
		}
		g_head->next = NULL;
	}
	while(1){
		printf("0:add node\n");
		printf("1:delet node\n");
		printf("2:display node\n");
		printf("3:clear node\n");
		printf("4:add node\n");
		int opt;
		scanf(" %d",&opt);
		switch (opt){
			case 0:
				//printf("add node please input Y or N?");		
				//scanf(" %c",&c);				
				//if('Y'== c || 'y' == c)
				{
					g_head = addNode(g_head);
					cntNode++;			
					//printf("add node please input Y or N?");		
					//scanf(" %c",&c);							
				}
				break;
			case 1:
				printf("input delet node num\n");
				scanf(" %d",&n);
				if(0 == deleteNode(g_head,n))
					cntNode--;
				break;
			case 2:
					printf("cnt=%d:\n",cntNode);
					displyNode(g_head);
				break;
			case 3:
				printf("clear cntNode = %d\n",cntNode);
				clearList(g_head);
				cntNode = 0;
				break;		
			case 4:
				break;
			default:
				break;
		}		
	}
	
	
}
Node *addNode(Node * head){
	int data;
	Node *pr = head;
	Node * pNode = NULL;
	
	pNode = (Node *)malloc(sizeof(Node));
	if(pNode == NULL){
		printf("NO enough momery to allocate!\n");
		exit(0);
	}
	while(pr->next != NULL){
		pr = pr->next;		//查找到尾结点
	}
	printf("input node data:");
	scanf(" %d",&data);
	pNode->data = data;
	pNode->next = NULL;
	pr->next = pNode;
	
	return head;
}

void clearList(Node *head){
	Node *pr = head;
	Node *tmp = NULL;
	if(head != NULL){
		pr = pr->next;
	}
	while(pr != NULL){
		tmp = pr;
		pr = pr->next;
		if(tmp != NULL){
			printf("free data = %d\n",tmp->data);
			free(tmp);
			tmp = NULL;	


		}	
	}
	head->next = NULL;//如果head 中不释放,去解析head->next野指针,而导致错误
	#if 0
	pr = head->next;
	while(pr != NULL){
		printf("have no free data = %d\n",pr->data);
		pr = pr->next;
		
	}	
	#endif 
}

void displyNode(Node *head){
	Node *pr = head;
	if(head != NULL){
		pr = pr->next;
	}
	int i = 1;
	while(pr != NULL){
		printf("%d data = %d\n",i,pr->data);
		pr = pr->next;		
		i++;
	}	
	
}

int deleteNode(Node *head, int n){
	int i = 0;
	Node *pr = head;
	Node *tmp = NULL;

	//找到删除的节点
	while(i < n && pr != NULL){
		tmp = pr;
		pr = pr->next;
		i++;
	}
	printf("delete node %d\n",i);
	if(i == n && pr != NULL){
		printf("node %d,data = %d have been delete\n",n,pr->data);
		tmp->next = pr->next;
		free(pr);
		pr = NULL;
		return 0;
    }else{
		printf("not find the delete node %d\n",n);
	}
	
	return -1;
}

Node * ReverseList(Node * pHead) 
{
if(pHead == NULL){return NULL;}

Node *pre = NULL;//暂存上一个节点
Node *tmp = NULL;//暂存下一个节点
while(pHead != NULL){
tmp = pHead->next;
pHead->next = pre;
pre = pHead;
pHead = tmp;
}
return pre;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值