链表

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct AdvTeacher
{
	char name[64];
	int age;
	struct AdvTeacher *next;
};
//静态链表
void main()
{
	struct AdvTeacher t1,t2,t3;
	struct AdvTeacher *p=NULL;
	t1.age=10;
	t2.age=20;
	t3.age=30;
	t1.next=&t2;
	t2.next=&t3;
	t3.next=NULL;
	p=&t1;
	//遍历链表,准备环境 需要引入一个辅助指针变量p 
	while (p)
	{
		printf("age:%d",p->age);
		p=p->next;
	}
	system("pause");
}





静态列表有局限性。



#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct Node 
{
   int data;
   struct Node* next;
}SLIST;
SLIST *Create_SList()
{
	//1,建立带有头的节点
	int data=0;
	SLIST*pHead=NULL,*pM=NULL,*pCur;
	pHead=(SLIST*)malloc(sizeof(SLIST));
	pHead->data=0;
	pHead->next=NULL;
	//2,循环创建节点,节点数据从键盘输入
	//-1 为结束标志
	printf("please enter the data of node (-1:quit)");
	scanf("%d",&data);
	pCur=pHead;
	while (data!=-1)
	{
		  //一: 不断的malloc 节点
		pM=(SLIST*)malloc(sizeof(SLIST));
		pM->data=data;
		pM->next=NULL;
		   //二: 不断的让新pM节点入链表
		pCur->next=pM;//
		pCur=pM;
	}
	//x我们需要不断的记录下来最后一个节点的位置pCur
	   
	  

	//3,malloc 新节点
}
int SList_Print(SLIST *pHead);
//在节点数值为x 的前面插入y
int Slist_NodeInsert(SLIST,int x,int y);
//删除节点为y的链表节点
int SList_NodeDel(SLIST*pHead,int y);
int SList_Destory(SLIST* pHead);

void main()
{

}
遍历操作

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct Node 
{
   int data;
   struct Node* next;
}SLIST;
SLIST *Create_SList()
{
	//1,建立带有头的节点
	int data=0;
	SLIST*pHead=NULL,*pM=NULL,*pCur;
	pHead=(SLIST*)malloc(sizeof(SLIST));
	pHead->data=0;
	pHead->next=NULL;
	//2,循环创建节点,节点数据从键盘输入
	//-1 为结束标志
	printf("please enter the data of node (-1:quit)");
	scanf("%d",&data);
	pCur=pHead;
	while (data!=-1)
	{
		  //一: 不断的malloc 节点
		pM=(SLIST*)malloc(sizeof(SLIST));
		pM->data=data;
		pM->next=NULL;
		   //二: 不断的让新pM节点入链表
		pCur->next=pM;//
		pCur=pM;
	}
	//x我们需要不断的记录下来最后一个节点的位置pCur
	   
	  

	return pHead;
}
int SList_Print(SLIST *pHead)
{
	SLIST *p=NULL;
	if(pHead==NULL)
	{
	  return -1;
	}
	p=pHead->next;
	printf("链表开始打印\n");
	while(p)
	{
		printf("%d\n",p->data);
		p=p->next;
	}
}
//在节点数值为x 的前面插入y
int Slist_NodeInsert(SLIST,int x,int y);
//删除节点为y的链表节点
int SList_NodeDel(SLIST*pHead,int y);
int SList_Destory(SLIST* pHead);

void main()
{

}


源码教师

#include "stdlib.h"
#include "stdio.h"
#include "string.h"

typedef struct Node
{
	int data;
	struct Node *next;
}SLIST;

SLIST *Creat_SList();
int SList_Print(SLIST *pHead);
//在结点数值为x的前面插入y
int SList_NodeInsert(SLIST *pHead, int x, int y);
//删除结点为y的链表结点
int SList_NodeDel(SLIST *pHead, int y);
int SList_Destory(SLIST *pHead);

SLIST *Creat_SList()
{
	//1 创建头结点并初始化 
	int data = 0;
	SLIST *pHead = NULL, *pM = NULL, *pCur;
	pHead = (SLIST *)malloc(sizeof(SLIST));
	pHead->data = 0;
	pHead->next = NULL;

	//2循环创建结点,结点数据域中的数值从键盘输入,
	//以-1作为输入结束标志
	printf("\nPlease enter the data of node(-1:quit) ");
	scanf("%d", &data);

	//准备环境 让pCur指向pHead
	pCur = pHead;
	while(data != -1)
	{
		//不断的malloc新节点 并且数据域 赋值
		pM = (SLIST *)malloc(sizeof(SLIST));
		pM->data = data;
		pM->next = NULL;

		//1新节点入链表
		pCur->next = pM;

		//2 当前结点下移(新结点变成当前结点)
		pCur = pM; // (pCur = pCur->next)

		printf("\nPlease enter the data of node(-1:quit) ");
		scanf("%d", &data);
	}
	return pHead;
}

int SList_Print(SLIST *pHead)
{
	SLIST *p = NULL;

	if (pHead == NULL)
	{
		return -1;
	}
	//准备环境
	p = pHead->next;
	printf("\nBegin ");
	while(p)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("End ");
}

void main()
{
	SLIST *pHead = NULL;
	pHead = Creat_SList();
	 SList_Print(pHead);
	system("pause");
}

链表的反转如上图

//alt+f8 格式化
int Slist_revse(SLIST*pHead)
{
	SLIST *p=NULL,*q=NULL,*t=NULL;
	if(pHead==NULL)
	{
	   return -1;
	}
	if(pHead->next==NULL||pHead->next->next==NULL)
	{
	  return 0;
	}
	p=pHead->next;
	q=pHead->next->next;
	//准备环境
	while (q!=NULL)
	{
		//1,在逆置之前需要做一个缓存
		t=q->next;
		//2 开始逆置
		q->next=p;
		//3 让p q 分别后移
		p=q;
		q=t;
	} 
	pHead->next->next=NULL;//
	pHead->next=p;//这里为p  因为q 最后已经为NULL了
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值