C语言——带头节点单链表常见操作

#include <stdio.h>
#include <stdlib.h>
typedef struct stu
{
	int data;  //存放数据
	struct stu* next; //下一个节点地址
}Node;
typedef struct
{
	int count;  // 节点个数
	Node* head; // 头结点
}List;


//创建新节点功能
Node* Create_node(int data)
{
	Node* pnew = (Node*)malloc(sizeof(Node));//为新节点开辟空间
	pnew->next = NULL;     //新节点指向null;
	pnew->data = data;
	return pnew;
}


//头部插入数据
void push_head(List* p, int data)
{
	//创建一个新的节点
	Node*pn = Create_node(data);
	//插入新节点到表头
	pn->next = p->head;
	p->head = pn;
	p->count++;
}


//尾插
void push_tail(List* p, int data)
{
	//找到最后一个节点
	Node* pn = p->head;
	while (pn->next != NULL)
	{
		pn = pn->next;
	}
	//插入元素
	Node*pm = Create_node(data);
	pn->next = pm;
	p->count++;
}


//头删节点
void del_head(List* p)
{
	Node* pn = p->head;//把要删除的节点保存起来
	p->head = p->head->next;//连接它的前节点与后节点
	free(pn);//释放并置为空
	pn = NULL;
	p->count--;
}


//尾删节点
void del_tail(List* p)
{
	Node *pn = p->head;//第一个节点
	while (pn->next->next != NULL)
	{
		pn = pn->next;
	}
	Node*pm = pn->next->next;
	pn->next = NULL;
	free(pm);
	pm = NULL;
	p->count--;
}


//任一位置节点插入
void push_arb(List* p, int data, int pos)
{
	//判断插入位置是否合法
	if (pos<0 || pos>p->count)
	{
		printf("插入位置不合法,插入到尾部\n");
		pos = p->count;
	}
	//头插
	if (pos == 0)
	{
		Node*pn = Create_node(data);
		pn->next = p->head;
		p->head = pn;
		p->count++;
		return;
	}
	//中间任意位置插入
	Node*pn = Create_node(data);
	Node*pm = p->head;
	for (int i = 1; i < pos; i++)
	{
		pm = pm->next;
	}
	pn->next = pm->next;
	pm->next = pn;
	p->count++;
}


//任一节点删除
void del_arb(List* p, int pos)
{
	//判断删除位置是否合法
	if (pos<0 || pos >= p->count)//删除的是当前位置后面的一个节点
	{
		printf("删除位置不合法,删除失败\n");
		return;
	}
	Node*pn = p->head;
	if (pos == 0)//头删
	{
		p->head = p->head->next;
		free(pn);
		p->count--;
		return;
	}
	//任意位置删除
	for (int i = 1; i < pos; i++)
	{
		pn = pn->next;
	}
	Node*pm = pn->next;
	pn->next = pn->next->next;
	free(pm);
	pm = NULL;
	p->count--;
}


//从尾到头打印链表递归调用
void tail_head(Node*head)//递归方法
{
	if (head == NULL)
	{
		return;
	}
    tail_head(head->next);
	printf("%d  ", head->data);
}

//链表长度
void long_list(List* p)
{
	printf("%d\n", p->count);
}


//清空链表
void clear_list(List* p)//从头开始清空
{
	printf("清空所有的节点\n");
	//第一个数据

	while (p->head != NULL)
	{
		Node*pn = p->head;
		p->head = p->head->next;
		free(pn);
		pn = NULL;
		p->count--;
	}

}


//遍历链表
void travel_list(List* p)
{
	Node* ptemp = p->head;
	if (ptemp == NULL)
	{
		printf("链表已空\n");
		return;
	}
	printf("链表中元素有: ");
	while (ptemp != NULL)
	{
		printf("%d   ", ptemp->data);
		ptemp = ptemp->next;
	}
	printf("\n");
}
int main()
{
	List list;
	list.head = NULL;
	list.count = 0;
	push_head(&list, 1);
	push_head(&list, 2);
	push_head(&list, 3);
	travel_list(&list);
	push_tail(&list, 4);
	travel_list(&list);
	del_head(&list);
	travel_list(&list);
	del_tail(&list);
	travel_list(&list);
	long_list(&list);
	clear_list(&list);
	long_list(&list);
	push_arb(&list, 6, 9);
	push_arb(&list, 6, 1);
	travel_list(&list);
	tail_head(list.head);
	travel_list(&list);
	return 0;
}

这里写图片描述

  • 3
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
/*带头结点头文件 hlinklist.h*/ #include <stdio.h> typedef int datatype; typedef struct link_node { datatype data; struct link_node *next; }node; /*初始化链表*/ node *init() { node *head; head=(node *)malloc(sizeof(node)); head->next=0; return head; } /*尾插法创建一个带头结点链表*/ node *creat(node *head) { node *r,*s; int x; r=head; printf("在新链表中输入数据以0结束:"); scanf("%d",&x); while(x) { s=(node*)malloc(sizeof(node)); s->data=x; r->next=s; r=s; scanf("%d",&x); } r->next=0; return head; } /*打印链表的结点值*/ void print(node *head) { node *p; p=head->next; if(!p) printf("链表内容为空!"); else while(p) { printf("%5d",p->data); p=p->next; } printf("\n"); } /*在单链表中查找第i个结点的地址*/ node *find(node *head,int i) { node *p=head; int j=0; if(i<0) {printf("不存在!");return 0;} if(i==0) return head; while(p&&i!=j) { p=p->next; j++; } return p; } /*在带头结点的单链表第i个位置后插入一个数*/ node *insert(node *head,int i,datatype x) { node *p,*q; q=find(head,i); if(!q) { printf("插入的位置不存在!\n");return head;} else { p=(node *)malloc(sizeof(node)); p->data=x; p->next=q->next; q->next=p; } return head; } /*在带头结点的单链表中删除一个为x的值*/ node *dele(node *head,datatype x) { node *pre=head,*p; p=head; while(p&&p->data!=x) { pre=p;p=p->next; } if(p) { pre->next=p->next; free(p); } return head; } /*把带头结点的单链表倒置(以结点形式 )*/ node *Dao_zhi(node *head) { node *p,*s; p=head->next; head->next=NULL; while(p) { s=p; p=p->next; s->next=head->next; head->next=s; } return head; } /*删除链表中重复的结点 */ node *dele1(node *head) { node *pre,*p,*q; if(head->next==0||!head->next->next) { printf("链表为空!"); return head; } //pre=head->next; q=head->next; while(q) { pre=q; p=q->next; while(p) { while(p&&q->data!=p->data) { pre=p;p=p->next; } if(p) { pre->next=p->next; free(p); } p=pre->next; } q=q->next; } return head; }
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值