C语言实现带头结点单链表

废话不多说,直接上代码:
先是.h文件,用来定义成员方法和成员变量:

#pragma once
#define TRUE 1
#define FALSE 0  //C语言里没有bool 故用宏定义实现

 typedef int ElemType;

 typedef union Data
 {
   
	 ElemType val;
	 int length;
 }Data;

 typedef struct Node
 {
   
	 Data data;//数据域
	 struct Node *next;//指针域
 }Node;

 typedef struct HList
 {
   
	 Node head;
 }HList,*pHList;

 //初始化
 void InitHList(pHList list);

 //显示元素
 void ShowHList(pHList list);

 //按位置插入  头插  尾插
 int InsertHListPos(pHList list,ElemType val,int pos);
 int InsertHListHead(pHList list,ElemType val);
 int InsertHListTail(pHList list,ElemType val);

 //按位置删  头删  尾删
 int DeleteHListPos(pHList list,int pos);
 int DeleteHListHead(pHList list);
 int DeleteHListTail(pHList list);

 //按值删
 int DeleteHListVal(pHList list,ElemType val);

//去重
 int DeleteHListRepeatVal(pHList list);

//清空
 void ClearHList(pHList list);

//销毁
 void DestroyHList(pHList list);

之后是.c文件,用来编写实现的方法:

#include "HList.h"
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

//判空操作
static void DeterPointIsNull(pHList list)
{
   
	assert(list != NULL);
	if(list 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/*头结点头文件 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; }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值