第一次写C语言链表的增删改查

2023-10-31 09:06:24
<span style="font-size:14px;color:#990000;"><strong>这是我第一次写链表的增加删除,学习数据结构,学习算法,是一个程序员必经之路,所以我来了,虽然还有很多的不足,但是这个代码就是一个基础,不仅会使我对php的理解更加深刻,而是我对程序有了另外一种看法,一切皆数据结构的原理。</strong></span>
<span style="font-size:14px;color:#990000;"><strong>#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//¶¨Òå½á¹¹ÌåÖ¸Õë
struct node
{	
	int key;
	int num;
	struct node *next ;
} ;

typedef struct node node_type ;
node_type *create()
{
	node_type *head=NULL,*tail=NULL,*p=NULL ;
	int key,num ;
	int count=0;
	while(count<1000)
	{	
		p = (struct node *)malloc(sizeof(struct node)) ;
		if(head == NULL)
		{
			head = tail = p ;
		}
		else
		{
			tail->next = p ;
			tail = p ;
		}
		key = count ;
		num = 100 + count ;
		p->key = key ;
		p->num = num ;
		p->next = NULL;
		count++ ;
	}

	return head ;
}	


node_type *search_list(node_type *head,int search_num)	//²éÕÒ·µ»Ø²éÕÒµ½µÄ½Úµã£¬×¢ÒâÕâÀïÈç¹ûÓжà¸öÔªËØ·ûºÏ²éÕÒÌõ¼þ£¬ÕâÀï¾Í²»¿¼ÂÇÁË£¬Ö»ÊÇÔÚÕâÀïÁ·Ï°Ð´´úÂë¶øÒÑ
{
	node_type *p ;
	p = head ;

	while(p != NULL)
	{
		if(search_num == p->num)
			return p ;
		p = p->next ;
	}
	return NULL;
}

//寻找上一个节点
node_type *find_previous(node_type *head,int search_num)
{
	node_type *p ;
	p = head ;
	while(p->next !=NULL && p->next->num != search_num)
		p = p->next ;

	return p ;

}

node_type *delete_list(node_type *head,int num)	//Á´±íµÄɾ³ýÂß¼­¾ÍÊÇ°ÑÇ°ÃæÒ»¸öµÄÖ¸ÕëÈƹý²éÕÒµ½µÄ½Úµã
{	
	//这里做参数的检验
	node_type *tmp_node ;
	node_type *p ;
	p = find_previous(head,num) ;
	if(p == NULL)
		return ;
	tmp_node = p->next ;
	p->next = tmp_node->next ;
	free(tmp_node) ;
	return head ;
}

node_type *update_list(node_type *head,int key,int num)
{
	node_type *p ;
	p = head ;
	while(p!=NULL)
	{
		if(p->key == key)
			p->num = num ;
	}
	return head ;
}

node_type *add_list(node_type *head,int num)  //在指针的尾部插入一个节点
{
	node_type *p ;
	node_type *new_node ;
	p = head ;
	while(p->next != NULL)
	{
		p = p->next ;
	}
	//出来的就是p->next= NULL的指针节点
	new_node = (struct node *)malloc(sizeof(struct node)) ;
	if(new_node == NULL)
		return NULL; 
	p->next = new_node ;
	new_node->key = p->key +1 ;
	new_node->num = num ;
	new_node->next = NULL ;
	return head ;
}


print_list(node_type *head) //´òÓ¡Á´±í
{
	node_type *p ;
	p = head;
	while(p != NULL)
	{
		printf("%d=>%d \n",p->key,p->num);
		p = p->next ;
	}
}
void main()
{	
	node_type * head;
	node_type *search;
	node_type *del ;
	int num ;
	head = create();
	print_list(head);

	printf("please input a num to search \n") ;
	scanf("%d",&num) ;
	search = search_list(head,num) ;
	printf("search->key=%d,search->num=%d \n",search->key,search->num) ;

	printf("please input delete num \n");
	scanf("%d",&num);

	del = delete_list(head,num) ;
	print_list(del) ;

	printf("please input a num to add \n");
	scanf("%d",&num);
	del = add_list(del,num) ;

	print_list(del) ;
}</strong></span>
下面是一个双向链表的例子,珍惜现在机会,好好学习,为自己的事业奋斗,虽然现在只是一个程序员,要能吃苦.


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


typedef struct node *node_type;
struct node //这里是一个双向链表的数据结构
{
<span style="white-space:pre">	</span>int num ;
<span style="white-space:pre">	</span>node_type prev ; //指向头的节点指针
<span style="white-space:pre">	</span>node_type next ; //指向下一个节点的指针
} ;




print_list(node_type head) //´òÓ¡Á´±í
{
<span style="white-space:pre">	</span>node_type p ;
<span style="white-space:pre">	</span>p = head;
<span style="white-space:pre">	</span>while(p != NULL)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>printf("p->num=%d \n",p->num);
<span style="white-space:pre">		</span>p = p->next ;
<span style="white-space:pre">	</span>}
}


node_type create()
{
<span style="white-space:pre">	</span>node_type head ;
<span style="white-space:pre">	</span>node_type tail;
<span style="white-space:pre">	</span>node_type p ;
<span style="white-space:pre">	</span>int count = 1 ;
<span style="white-space:pre">	</span>head =tail=p= (node_type)malloc(sizeof(struct node)) ;
<span style="white-space:pre">	</span>if(head == NULL)
<span style="white-space:pre">		</span>return NULL ;
<span style="white-space:pre">	</span>while(count<=10)
<span style="white-space:pre">	</span>{<span style="white-space:pre">	</span>
<span style="white-space:pre">		</span>if(count == 1)
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>head->num = 100 ;
<span style="white-space:pre">			</span>head->prev = head->next = NULL ;
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>else
<span style="white-space:pre">		</span>{
<span style="white-space:pre">			</span>p = (node_type)malloc(sizeof(struct node)) ;
<span style="white-space:pre">			</span>if(p == NULL)
<span style="white-space:pre">				</span>continue ;


<span style="white-space:pre">			</span>p->num = 10*count;
<span style="white-space:pre">			</span>tail->next = p;
<span style="white-space:pre">			</span>p->next = NULL;
<span style="white-space:pre">			</span>p->prev =  head;
<span style="white-space:pre">			</span>head->prev = p ;
<span style="white-space:pre">			</span>tail = p ;
<span style="white-space:pre">		</span>}


<span style="white-space:pre">	</span>
<span style="white-space:pre">		</span>count++ ;
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>return head ;
}




void main()
{<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>node_type head;
<span style="white-space:pre">	</span>head  = create() ;
<span style="white-space:pre">	</span>print_list(head);
}

  • 作者:白小狮
  • 原文链接:https://blog.csdn.net/baixiaoshi/article/details/46780211
    更新时间:2023-10-31 09:06:24