struct node
{
int num;
struct node *p;
} ;
#include <stdlib.h> /*含ma l l o c ( ) 的头文件*/
#include <stdio.h>
//①定义链表数据结构
struct node
{
int num;
struct node *next;
};
//函数声明
struct node *creat();
void print();
main( )
{
struct node *head;
head=NULL; //②建一个空表
head=creat(head);/*创建单链表*/
print(head);/*打印单链表*/
}
/******************************************/
struct node*creat(struct node *head)/*返回的是与节点相同类型的指针*/
{
struct node*p1,*p2;
int i=1;
//③利用malloc ( )函数向系统申请分配一个节点
p1=p2=(struct node*)malloc(sizeof(struct node));/*新节点*/
printf("请输入值,值小于等于0结束,值存放地址为:p1_ADDR= %d\n",p1);
scanf("%d",&p1->num);/*输入节点的值*/
p1->next=NULL;/*将新节点的指针置为空*/
while(p1->num>0)/*输入节点的数值大于0*/
{
//④将新节点的指针成员赋值为空。若是空表,将新节点连接到表头;若是非空表,将新节点接到表尾;
if(head==NULL)
head=p1;/*空表,接入表头*/
else
p2->next=p1;/*非空表,接到表尾*/
p2=p1;
p1=(struct node*)malloc(sizeof(struct node));/*下一个新节点*/
i=i+1;
printf("请输入值,值小于等于0结束,值存放地址为:p%d_ADDR= %d\n",i,p2);
scanf("%d",&p1->num);/*输入节点的值*/
//⑤判断一下是否有后续节点要接入链表,若有转到3 ),否则结束;
}
//==============原来程序更正部分:(多谢@daling_datou提醒)================================
free(p1); //申请到的没录入,所以释放掉
p1=NULL; //使指向空
p2->next = NULL; //到表尾了,指向空
printf("链表输入结束(END)\n");
//==============================================
return head;/*返回链表的头指针*/
}
/*******************************************/
void print(struct node*head)/*出以head为头的链表各节点的值*/
{
struct node *temp;
temp=head;/*取得链表的头指针*/
printf("\n\n\n链表存入的值为:\n");
while(temp!=NULL)/*只要是非空表*/
{
printf("%6d\n",temp->num);/*输出链表节点的值*/
temp=temp->next;/*跟踪链表增长*/
}
printf("链表打印结束!!");
}
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(NODE)
typedef struct _NODE//节点声明
{
int val;
struct _NODE* next;
} NODE, *PNODE;
void print(PNODE head){//打印所有节点
while (head)
{
printf("%3d",head->val);
head = head->next;
}
printf("\n");
}
void insertHead(PNODE *pHead, int val){//头插法
PNODE n = (PNODE)malloc(LEN);
n->val = val;
n->next = *pHead;
*pHead = n;
}
void insertTail(PNODE *pHead, int val){//尾插法
PNODE t = *pHead;
PNODE n = (PNODE)malloc(LEN);
n->val = val;
n->next = NULL;
if (*pHead == NULL)
{
n->next = *pHead;
*pHead = n;
}else{
while (t->next)
{
t = t->next;
}
t->next = n;
}
}
void deleteHead(PNODE *pHead){//删除头
if (*pHead == NULL)
{
return;
}
else
{
PNODE t = *pHead;
*pHead = (*pHead)->next;
free(t);
}
}
void deleteTail(PNODE *pHead){//删除尾
PNODE t = *pHead;
if (t == NULL)
{
return;
}
else if (t->next == NULL)
{
free(t);
*pHead = NULL;
}
else{
while (t->next->next != NULL)
{
t = t->next;
}
free(t->next);
t->next = NULL;
}
}
PNODE findByVal(PNODE head, int val){//根据值找到满足条件的第一个节点
while (head != NULL && head->val != val)
{
head = head->next;
}
return head;
}
PNODE findByIndex(PNODE head, int index){//根据索引找节点
if (index == 1)
{
return head;
}
else
{
int c = 1;
while (head != NULL && index != c)
{
head = head->next;
c++;
}
}
return head;
}
void insertByIndex(PNODE *pHead, int index, int val){//根据索引插入节点
if (index == 1)
{
insertHead(pHead, val);
}
else
{
PNODE t = findByIndex(*pHead,index - 1);
if (t == NULL)
{
return;
}
else
{
PNODE n = t->next;
t->next = (PNODE)malloc(LEN);
t->next->next = n;
t->next->val = val;
}
}
}
void deleteByIndex(PNODE *pHead, int index)//根据结点索引删除结点
{
if (index == 1)
{
deleteHead(pHead);
}
else
{
PNODE t= findByIndex(*pHead, index - 1);
if (t == NULL || t->next == NULL)
{
return;
}else{
PNODE n = t->next->next;
free(t->next);
t->next = n;
}
}
}
void deleteByVal(PNODE *pHead, int val)//根据值删掉第一个满足条件的
{
if (*pHead == NULL)//如果空表退出
{
return;
}
else
{
if ((*pHead)->val == val)//如果第一个就是,删头
{
deleteHead(pHead);
}
else
{
PNODE t = *pHead;
while (t->next != NULL && t->next->val != val)//遍历,若t的next为空或者next是要找的节点则退出
{
t = t->next;
}
if (t->next)//如果t指向要找的结点的上一个节点
{
PNODE n = t->next->next;//删除要找的结点
free(t->next);
t->next = n;
}
}
}
}
void clear(PNODE *pHead)//清除链表
{
while ((*pHead) != NULL)
{
deleteHead(pHead);//从头删除
}
}
void main()
{
PNODE head = NULL;
insertTail(&head,1);
deleteHead(&head);
insertTail(&head,2);
insertTail(&head,3);
insertTail(&head,4);
insertTail(&head,5);
insertTail(&head,6);
print(head);
insertByIndex(&head, 6, 9);
print(head);
//deleteByIndex(&head,3);
deleteByVal(&head, 2);
print(head);
clear(&head);
print(head);
insertByIndex(&head,1,12);
print(head);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有