单链表的基础操作(头插法、尾插法、插入和删除)

一、链表的建立(头插法和尾插法)

1、头插法:把后建立的结点插在头部。用这种方法建立起来的链表的实际顺序与输入顺序刚好向反,输出时为倒序!
下面附上代码:

struct node *headcreat()
{
 struct node *p,*q,*head;
 head = (struct node*)malloc(sizeof(struct node));
 p = (struct node*)malloc(sizeof(struct node));
 p->next = NULL;                       //先让最后一个节点p指向空
 printf("请输入数字,以0结尾:\n");
 do{
  q = (struct node*)malloc(sizeof(struct node));
  scanf("%d",&q->data);
  if(q->data == 0)break;               //输入数字为0时跳出循环,停止操作(0不在链表内)
  L++;                                           //L为全局变量,用于计算链表长度,对于实现功能没有影响
  q->next = p->next;
  p->next = q;
 }while(true);
 head = p;				     //头节点head为空
 return (head); 
}

2、尾插法:将后建立的结点插在链表尾部,这种方法建立起来的链表的实际顺序与输入顺序相同,在对链表顺序有严格要求时,建议使用尾插法!
代码如下:

在这里插入代码片struct node *tailcreat()
{
 struct node *p,*q,*head;
 int n = 0;
 head = (struct node*)malloc(sizeof(struct node));
 p = (struct node*)malloc(sizeof(struct node));
 printf("请输入数字,以0结尾:\n");
 do{
  q = (struct node*)malloc(sizeof(struct node));
  scanf("%d",&q->data);L++;
  if(q->data == 0)break;
  if(n == 0)                                    //当输入第一个数字时,将这个节点赋值给头节点的指针域
  {
   head->next = q;
   p->next = q;
   p = q;
   n++;
  }
  p->next = q;
  p = q;
 }while(true); 
 p->next = NULL;                          //容易忽略,后果就是在display时疯狂输出!!
 return (head);
}

二、链表的输出

这个不用多说啦~代码如下:

void display(struct node *head)
{
 while(head->next!=NULL)             //此处不能为head!=NULL,因为下面的printf输出的是当前位置的下一个结点的数据,若写成head->NULL,则指针指到NULL前个结点时,(head->next)->data无法输出
 {
  printf("%d ",(head->next)->data);
  head = head->next;
 }
}

三、链表的插入

由于新建的链表是有头结点的链表,固不必讨论插入的位置是在头部还是在中间或尾部,若没有头结点,则要分为插在头部和插在中间(尾部)两种情况讨论,因为插在头部需要改变head的值,而插在中间或尾部就不用改变head。这也是我建议建立有头指针的链表的原因!
为了便于理解,可以画张图~
在这里插入图片描述
代码如下:

struct node *insert(struct node *head)
{
 struct node *r,*p;
 int i,j;
 p = head;
 r = (struct node*)malloc(sizeof(struct node));
 printf("请输入你要插入的位置:\n");
 scanf("%d",&i);
 printf("请输入你要插入的数字:\n");
 scanf("%d",&r->data);
 for(j=1;j<i;j++)                    //将指针移动到要插入新结点的位置
  p = p->next;
 r->next = p->next;
 p->next = r;
 L++;
 return(head);
}
 

四、结点的删除

同样不需要考虑要删除的位置啦~原因同上
在这里插入图片描述

struct node *del(struct node *head)
{
 struct node *p;
 int i;
 p = head;
 printf("请输入你要删除的数字:\n");
 scanf("%d",&i);
 while(i!=(p->next)->data)               //当要指针移动到要删除的结点的前一个结点时
  p = p->next;
 p->next = (p->next)->next;
 L--;
 return(head);
}

以上就是单链表的基本操作我对于链表的j理解还不是很深刻!欢迎指正交流下面附上完整代码:

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
	int data;
	struct node *next;
}node;

int L;

struct node *tailcreat()
{
	struct node *p,*q,*head;
	int n = 0;
	head = (struct node*)malloc(sizeof(struct node));
	p = (struct node*)malloc(sizeof(struct node));
	printf("请输入数字,以0结尾:\n");
	do{
		q = (struct node*)malloc(sizeof(struct node));
		scanf("%d",&q->data);L++;
		if(q->data == 0)break;
		if(n == 0)
		{
			head->next = q;
			p->next = q;
			p = q;
			n++;
		}
		p->next = q;
		p = q;
	}while(true); 
	p->next = NULL;
	return (head);
}

struct node *headcreat()
{
	struct node *p,*q,*head;
	head = (struct node*)malloc(sizeof(struct node));
	p = (struct node*)malloc(sizeof(struct node));
	p->next = NULL;
	printf("请输入数字,以0结尾:\n");
	do{
		q = (struct node*)malloc(sizeof(struct node));
		scanf("%d",&q->data);
		if(q->data == 0)break;
		L++;
		q->next = p->next;
		p->next = q;
	}while(true);
	head = p;
	return (head);	
}

struct node *insert(struct node *head)
{
	struct node *r,*p;
	int i,j;
	p = head;
	r = (struct node*)malloc(sizeof(struct node));
	printf("请输入你要插入的位置:\n");
	scanf("%d",&i);
	printf("请输入你要插入的数字:\n");
	scanf("%d",&r->data);
	for(j=1;j<i;j++)
		p = p->next;
	r->next = p->next;
	p->next = r;
	L++;
	return(head);
}

struct node *del(struct node *head)
{
	struct node *p;
	int i;
	p = head;
	printf("请输入你要删除的数字:\n");
	scanf("%d",&i);
	while(i!=(p->next)->data)
		p = p->next;
	p->next = (p->next)->next;
	L--;
	return(head);
}

void display(struct node *head)
{
	while(head->next!=NULL)
	{
		printf("%d ",(head->next)->data);
		head = head->next;
	}
}

int main()
{
	struct node *head;
	head = tailcreat();
	display(head);
	head = insert(head);
	display(head);
	head = del(head);
	display(head);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43260290/article/details/82843347