近期了解C语言链表的心得

最近了解C语言的链表,也算是有点心得,至此分享给大家

初学时,我做的单向链表实现代码如下:

#include<stdio.h>
#include<stdlib.h>
typedef struct student{
	int num;
	struct student* next;
} node;
int main(){
	int i;
	node *head = NULL;
	while(1){
		scanf("%d",&i);
		if( i == -1 )//链表创建退出条件 
				break;
		node* p = (node*)malloc(sizeof(node));
		p->num = i;
		p->next = NULL;
		node *last = head;//在此定义一个临时结点,用来寻找链表上的最后一个节点  
		if( last ){
			while(last->next){
				last = last->next ;//如果链表不为空,那么将尾结点指向刚才创建的新节点 
			}
			last->next = p;
		}else {
			head = p;//如果链表为空,那么让刚才创建的结点尾头结点 
		}		
	}
	return 0; 
} 
双向链表如下:

#include<stdio.h>
#include<stdlib.h>
typedef struct student{
	int num;
	struct student* lnext;
	struct student* rnext;
}node;
int main(){
	int i;
	node *head = NULL,*real = NULL; 
	while(1){
		scanf("%d",&i);
		if(i==-1)		//链表创建退出条件 
				break;
		node *pp = (node*)malloc(sizeof(node));
		pp->num = i;
		pp->lnext = NULL;
		pp->rnext = NULL;
		node* temp = head;
		if(temp){  //如果链表不为空, 
			while(temp->rnext){//找出尾结点 
				temp = temp->rnext;
			}
			temp->rnext = pp; //将新节点连接 
			pp->lnext = temp;
		}else {  //如果链表为空,则让刚创建的结点成为头结点 
			head = pp;
		}
		real = pp;
	} 
	return 0;
}
在进一步学习双向,单向循环链表时,了解到了尾结点的妙用,于是单向链表可以写成这样:

#include<stdio.h>
#include<stdlib.h>
typedef struct list{
	int num;
	struct list* next;
}node;
int main(){
	int i;
	node* head = NULL,*real = NULL;//额外定义一个尾结点 
	while(1){
		scanf("%d",&i);
		node* p1 = (node*)malloc(sizeof(node));
		if(i==-1)
				break;
		p1->num = i;
		p1->next = NULL;
		node* pp = head;
		if(pp){ 
			real->next = p1;//直接用尾结点相连,是不是方便多啦 
		}else {
			head = p1;
		}
		real = p1;//每次创建的新链表都作为尾结点保存,
			  //这样每次就不用再苦苦寻找尾结点啦 
	}
	return 0;
}
至于循环链表   只需要在那个while循环出来得时候将尾结点指向头节点即可。

还有一个就是交换链表结点的东西,以下是比较正规的实现方法:

传入的参数为头结点和需要交换的相邻结点的前一个结点的标志:

void swap(node* head,int i){
	node* temp1,*p = head,*pp;
	while(p){
		if(p->num == i){
			temp1 = p;//保存需要交换的前一个节点 
			pp->next = p->next;//使指向前一个结点的结点指向后一个节点 
			temp1->next = p->next->next;//需要交换的前一个结点指向
						//需要交换的后一个节点的后一个节点 
			pp->next->next = temp1;//使需要交换的后一个节点指向需要交换的前一个结点 
			break;
		}
		pp = p;
		p = p->next;
	}
}
个人在开始的时候对交换结点得方式有点迷惑,于是当时想出了一种笨方法,将需要交换的两个结点和这两个结点的前后结点都保存在变量中,接下来要做的就只是互相指来指去达到交换的效果了,虽然用到的变量比较多,但也不失为一种简单易懂的方法,有兴趣的小伙伴可以试试。


本人编程新手入门,各方大佬多多指教,不喜勿喷,有相同爱好的小伙伴可以相互关注下,常交流。谢谢






  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
三种不同的方法,挺不错的! #include<stdio.h> #include<stdlib.h> #include<string.h> #define N 100 typedef struct SList { char data[N]; //字符数组 struct SList *next; //定义链表头指针 }SList,*ListPointer; /*typedef struct List { SList *head; }List,* ListPointer; */ void initList(ListPointer &lp) { lp=(SList *)malloc(sizeof(SList));//初始化链表 lp->next=lp; //链表的头指针指向本身,实现链表循环 } void output(ListPointer lp) // 自定义输出函数 { SList *ep; ep=lp; while(ep->next!=lp) //判定条件 当指针重新指向头指针输出结束 { ep=ep->next; printf("%s ",ep->data); } } void revert(ListPointer lp) // 链表的逆置 { SList *p,*q; p=lp;q=NULL;lp=NULL; while(p) { q=p->next; p->next=lp; lp=p; p=q; } /*方法二 SList *p,*q,*end; p=lp->next; q=p->next; end=p; while(q!=lp) { lp->next=q; q=q->next; lp->next->next=p; p=lp->next; } end->next=lp; */ } void add_char(char *p,ListPointer lp) //将输入的字符传递给链表 { SList * ep; ep=lp; while(ep->next!=lp) //判定条件 当指针重新指向头指针输出结束 { ep=ep->next; } ep->next=(SList *)malloc(sizeof(SList)); //开辟空间存储 strcpy(ep->next->data,p); //字符的传递 ep->next->next=lp; } void main() { ListPointer L; char str[N]; initList(L); printf("输入#以结束\n");//确定输入终止条件 while(1) { scanf("%s",str); if(*str=='#') //判定条件 { break; } add_char(str,L); } printf("初始序列为:"); output(L); printf("\n"); revert(L); printf("逆置后为:"); output(L); printf("\n"); }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值