数据结构之链表

静态创建链表

练习1:实现查找目标节点和统计链表节点个数功能

#include <stdio.h>

typedef struct link
{
	int data;
	struct link *next;
}LINK;
//函数声明
void printLink(LINK *head);//打印链表
int getNodeNum(LINK *head);//统计链表节点总个数
void findNode(LINK *head,int num);//查找目标节点

int main()
{
    //静态创建链表
	LINK t1 = {1,NULL};
	LINK t2 = {2,NULL};
	LINK t3 = {3,NULL};
	LINK *head = &t1;
	t1.next = &t2;
	t2.next = &t3;
	//打印链表
	printLink(head);
	//统计链表个数
	int ret = getNodeNum(head);
	printf("链表中节点的个数为%d\n",ret);
	//查找节点
	findNode(head,3);
	return 0;
}
//打印链表
void printLink(LINK *head)
{
	while(head != NULL)
	{
		printf("%d ",head->data);
		head = head->next;
	}
	putchar('\n');
}
//统计链表节点总个数
int getNodeNum(LINK *head)
{
	int count = 0;//计算节点个数
	while(head != NULL)
	{
		count++;
		head = head->next;
	}
	return count;
}
//查找目标节点
void findNode(LINK *head,int num)
{
	while(head != NULL)
	{
		if(head->data == num)
		{
			printf("你要找的小%d找到了\n",head->data);
		}
		head = head->next;
	}
}

练习2:静态创建链表实现增删改查功能

#include <stdio.h>


typedef struct link
{
	int data;
	struct link *next;
}LINK;
//函数声明
void printLink(LINK *head);//打印链表
int getNodeNum(LINK *head);//统计节点个数

LINK* addBehindNode(LINK *head,LINK* new,int data);//从节点后添加新节点
LINK* addAheadNode(LINK *head,LINK* new,int data);//从节点前添加新节点

LINK* deleteNode(LINK*head,int delete_data);//删除节点
LINK* modifyNode(LINK *head,int data ,int alterdata);//修改节点
LINK* findNode(LINK *head);//查找节点

int main()
{
    //静态创建链表
	LINK t1 = {1,NULL};
	LINK t2 = {2,NULL};
	LINK t3 = {3,NULL};
	LINK new = {4,NULL};
	LINK new2 = {0,NULL};
	LINK *head = &t1;
	t1.next = &t2;
	t2.next = &t3;

	//打印链表
	puts("初始链表:");
	printLink(head);
	//增加节点
	puts("在节点之后增新加节点的链表:");
	head = addBehindNode(head,&new,3);
	printLink(head);
	puts("在节点之前增加新节点的链表:");
	head = addAheadNode(head,&new2,1);
	printLink(head);
	//删除节点
   	puts("删除指定节点后的链表:");
  	head = deleteNode(head,0);
    	printLink(head);

	//修改节点
    puts("修改指定节点后的链表:");
   	head = modifyNode(head,1,666);
   	printLink(head);

	//查找节点
	findNode(head);

	return 0;
}
//打印链表
void printLink(LINK *head)
{
	LINK* p = head;
	while(p != NULL)
	{
		printf("%d ",p->data);
		p = p->next;
	}
	putchar('\n');
}
//统计节点个数
int getNodeNum(LINK *head)
{
	int count = 0;//计算节点个数
	while(head != NULL)
	{
		count++;
		head = head->next;

	}
	return count;
}
//从节点后添加新节点
LINK *addBehindNode(LINK *head,LINK* new,int data)
{
	LINK *phead = head;
	while(phead != NULL)
	{
		if(phead->data == data)
		{
			new->next = phead->next;先连接新节点的后面
			phead->next = new;//再连新节点的前面
			return head;
		}
		phead = phead->next;
	}
}
//从节点前添加新节点
LINK* addAheadNode(LINK*head,LINK*new,int data)
{
        LINK*phead = head;
        if( head->data ==data ){
                new->next = head;    //先连接新节点的后面
                head = new;          //再连新节点的前面
                return head;
        }
        while(phead->next != NULL){
                if(data == phead->next->data){
                        new->next = phead->next;//先连新节点的后面
                        phead->next = new;      //再连新节点的前面
                        return head;
                }
                phead = phead->next;//让链表遍历起来
        }
}
//删除节点
LINK * deleteNode(LINK* head,int delete_data)
{
	LINK* p = head;
	if( head->data == delete_data)
	{
		head = head->next;//删除首节点,头指针需要指向下一个节点
		return head;
	}
	while( p->next != NULL)
	{
		if(p->next->data == delete_data)//遍历找到目标节点
		{
			p->next = p->next->next;
			return head;
		}
		p = p->next;
	}

}
//修改节点
LINK* modifyNode(LINK *head,int data ,int alterdata)
{
	LINK *phead = head;

	if(head->data == data)
	{
		head->data = alterdata;
		return head;
	}
	while(phead->next != NULL)
	{
		if(phead->next->data == data)
		{
			phead->next->data = alterdata;
			return head;
		}
		phead = phead->next;

	}

}
LINK* findNode(LINK *head)
{

	printf("输入你要查找的节点:");
	int num=0;
	scanf("%d",&num);

	LINK* phead = head;
	while(phead != NULL)
	{
		if(phead->data == num)
		{
			printf("你要找的小%d找到了\n",phead->data);
			return head;
		}
		phead = phead->next;

	}
	printf("没有你要找的节点\n");
}

动态创建链表

头插法&尾插法

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

typedef struct link
{
	int data;
	struct link *next;
}LINK;

void printLink(LINK *head)
{
	LINK* p = head;
	while(p != NULL)
	{
		printf("%d ",p->data);
		p = p->next;
	}
	putchar('\n');
}
//头插法
LINK * insertFromHead(LINK *head,LINK* new)
{

	if(head == NULL){
		head = new;
		return head;
	}else{
		new->next = head;
		head = new;
		return head;
	}
}
//尾插法
LINK * insertFromTail(LINK *head,LINK* new)
{

	LINK *phead = head;
	if(head == NULL){
		head = new;
		return head;
	}
	while( phead->next != NULL )
	{
		phead = phead->next;//遍历链表找到尾巴!!!
	}
	phead->next = new;//保证new节点是尾节点!!
	return head;
}
//头插法动态创建链表
LINK * create_Link(LINK *head,int flag)
{
	LINK* new;
	while(1)
	{
		new = (LINK *)malloc(sizeof(LINK));
		printf("请输入新节点,退出请输入888\n");
		scanf("%d",&new->data);
		if(new->data == 888)
		{
			free(new);
			new = NULL;
			return head;            
		}
		if(flag == 1)
		{
			head = insertFromHead(head,new); 
		}else if(flag == 2){
			head = insertFromTail(head,new); 
		}
	}
}
int main()
{
	LINK *head = NULL;
	//动态创建链表,第二个参数1为头插法,2尾插法
	head = create_Link(head,2);
	//打印链表
	printf("创建链表如下:");
	printLink(head);
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值