ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

单向链表的 增删

2019-07-24 17:41:04  阅读:148  来源: 互联网

标签:node head cur 单向 next 链表 score 增删 struct


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NAME_MAX 30
 
//typedef struct student_node *S_P;

typedef struct student_node
{
	int id;
	char name[NAME_MAX];
	float score;
	struct student_node *next;
}LinkNode;

 
struct student_node *create_node(int id, char *name, float score)
{
	struct student_node *p = malloc(sizeof(LinkNode));
	if(p != NULL)
	{
		p->id = id;
		strcpy(p->name,name); //字符串
		p->score = score;
		p->next = NULL;
	}
	return p;
}

//head = insert_head(head, id, name, score);

// Insert from head
struct student_node *insert_head(struct student_node *head, int id, char *name, float score)
{
	struct student_node *newp = create_node(id, name, score);
		
	if( head == NULL || newp == NULL)
	{
		if(head == NULL)
		{
			head = newp;
		}
		return head;
	}
	newp->next = head;
	head = newp;
	return head;
}
 
//Insert from tail
struct student_node *insert_tail(struct student_node *head, int id, char *name, float score)
{
	struct student_node *newp = create_node(id, name, score);
	struct student_node *tail;
 
 	if( head == NULL || newp == NULL)
	{
		if(head == NULL)
		{
			head = newp;
		}
		return head;
	}
	for(tail = head; tail->next; tail = tail->next)
	{
		
	}	
	//tail存放是是尾节点
	tail->next = newp;
	return head;
}
 
//按升序插入node
struct student_node *insert_sort_node(struct student_node *head, int id, char *name, float score)
{
	struct student_node *newp = create_node(id, name, score);
	struct student_node *pre, *cur;
 
	//判断head newp node是否创建成功
	if(!head || !newp)
	{ 
		if(!head)
		{
			head = newp;
		}
		return head;
	}
	for(pre = cur = head; cur && cur->score < score; pre = cur, cur = cur->next)
	{
	}
										
	if(cur == head)
	{
		newp->next = head;
		head = newp;
	}
	else
	{
		newp->next = cur;
		pre->next = newp;
	}
	return head;
}
 
void display_linklist(struct student_node *head)
{
	struct student_node *cur;
	for(cur = head; cur != NULL; cur = cur->next)//for(cur = head; cur; cur = cur->next)
	{
		printf("%d\t%s\t%.2f\n", cur->id, cur->name, cur->score);
	}
 
}
 
void delete_linklist(struct student_node *head)
{
	struct student_node *cur, *next;
	#if 0
	for(cur = head; cur; cur = next)
	{
		next = cur->next;
		free(cur);
	}
	#endif
 
	#if 1
	cur = head;
	while(cur != NULL)
	{
		next = cur->next;
		free(cur);
		cur = next;
	}
	#endif
}
 
struct student_node *delete_node(struct student_node *headp, float key)
{
	struct student_node *del, *pre, *cur;
	//没有head节点的情况	
	if(headp == NULL)
	{
		return NULL;
	}
	
	//删除的节点恰好是头结点
	if( (headp)->score == key )
	{
		del = headp;
		headp = (headp)->next;
		del->next = NULL;
		return headp;
		//return del;
	}
	
	for( pre = headp, cur = (headp)->next; cur; pre = pre->next, cur = cur->next)
	{
		if(cur->score == key )
		{
			del = cur;
			cur = cur->next;
			del->next = NULL;
			pre->next = cur;
			printf("[%s:%d]id:%d\tname:%s\tscore:%.2f\n", __FUNCTION__, __LINE__, del->id, del->name, del->score);
			return del;
		}			
	}
	return NULL;
}
 
int main(void)
{	
	int id;
	char name[30];
	float score;
	struct student_node *head = NULL, *del;
 
	while(1)
	{
		scanf("%d %s %f", &id, name, &score);
		getchar();
		if(id == 0 )
		{
			break;
		}
		//head = insert_sort_node(head, id, name, score);
		//要有返回值
		head = insert_head(head, id, name, score);
		//head = insert_tail(head, id, name,score);
	}
	printf("--------------------All node----------------------------\n");
	display_linklist(head);


	
	while( (del = delete_node(head, 3)) )
	{
		printf("del:%d %s %f\n", del->id, del->name, del->score);
		free(del);
	}
	printf("-----------------       Del score=3   ------------------------\n");
	display_linklist(head);
	
//	display_linklist(head);

	return 0;
}
 
/*
lbo@donglebuild2:~/lin/ms$ gcc linklist.c 
lbo@donglebuild2:~/lin/ms$ ./a.out        
1 a 11
2 b 22
3 c 33
4 d 44
5 e 3
6 f 55
0 0 0
--------------------All node----------------------------
6       f       55.00
5       e       3.00
4       d       44.00
3       c       33.00
2       b       22.00
1       a       11.00
[delete_node:164]id:5   name:e  score:3.00
del:5 e 3.000000
-----------------       Del score=3   ------------------------
6       f       55.00
4       d       44.00
3       c       33.00
2       b       22.00
1       a       11.00
lbo@donglebuild2:~/lin/ms$ 


*/


标签:node,head,cur,单向,next,链表,score,增删,struct
来源: https://blog.csdn.net/linbounconstraint/article/details/97144397

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有