单链表的插入与删除算法

1,单链表的插入算法

     算法描述:在一个带头结点的单链表中,第i个结点前插入结点e,输出插入后的单链表。这里注意的是,在第i个结点前插入,查找时,是要找到第i-1个结点处,也就是在第i-1个结点后插入。

    代码如下:

   

#include<stdio.h>
#include <malloc.h>
#define SIZE  100
#define  INCREMENT_SIZE 10
typedef struct  LNode
{
   int data;
   LNode *next;
}LNode,*LinkList;

//creat a LinkList
bool creatLinklist(LinkList&L,int n)
{
	LinkList p,q,t,s;
	L=(LNode*)malloc(sizeof(LNode));
	if(!L)
		return false;
	L->next=NULL;
	for(int i=n;i>0;i--)
	{
		p=(LNode*)malloc(sizeof(LNode));
		scanf("%d",&p->data);
		p->next=L->next;
		L->next=p;
	}
	p=L->next;//The first Node
	q=p->next;//The second Node
	p->next=NULL; 
	while(q)
	{
		t=q->next;// keep the third Node
		q->next=p;// the second Node points to the first Node in a loop
		p=q;      //p points to next Node
		q=t;      //q points to next Node
	}
	L->next=p;    //set head Node
	return true;
}

bool LinkListInsert(LinkList&L,int i,int e)
{
	int j=0;
	LinkList p,s;
	p=L->next;
	while(p)
	{
		j++;
		if(j<i-1)
			p=p->next;
		else
			break;
	}
	if(j>i-1||!p)
	return false;
	s=(LNode*)malloc(sizeof(LNode));
	s->data=e;
	s->next=p->next;
	p->next=s;
	return true;
}

bool LinklistDelete()
{
	return true;
}
void main()
{
	LinkList Llist,p;
	int k;
	int elemet;
	int position;
	printf("input the number of LinkList to be created:");
	scanf("%d",&k);
	creatLinklist(Llist,k);
	printf("\n");
	printf("input the position to insert in LinkList:");
	scanf("%d",&position);
	printf("\n");
	printf("input the element to insert in LinkList:");
	scanf("%d",&elemet);
	printf("\n");
	LinkListInsert(Llist,position,elemet);
	printf("out put the new LinkList:\n");
	p=Llist->next;
	while(p)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
	free(Llist);
}

运行结果如下:

《单链表的插入与删除算法》


2,单链表的删除算法

      算法描述:在一个带头结点的单链表中,删除表中第i个结点,返回删除结点的值,并输出删除后的单链表。

      代码如下:

   

#include<stdio.h>
#include <malloc.h>
#define SIZE  100
#define  INCREMENT_SIZE 10
typedef struct  LNode
{
   int data;
   LNode *next;
}LNode,*LinkList;

//creat a LinkList
bool creatLinklist(LinkList&L,int n)
{
	LinkList p,q,t,s;
	L=(LNode*)malloc(n*sizeof(LNode));
	if(!L)
		return false;
	L->next=NULL;
	for(int i=n;i>0;i--)
	{
		p=(LNode*)malloc(sizeof(LNode));
		scanf("%d",&p->data);
		p->next=L->next;
		L->next=p;
	}
	p=L->next;//The first Node
	q=p->next;//The second Node
	p->next=NULL; 
	while(q)
	{
		t=q->next;// keep the third Node
		q->next=p;// the second Node points to the first Node in a loop
		p=q;      //p points to next Node
		q=t;      //q points to next Node
	}
	L->next=p;    //set head Node
	return true;
}
//delete a Node
bool LinklistDelete(LinkList&L,int i,int &e)
{
	LinkList p,q;
	int j=0;
	p=L->next;
	while(p)
	{
		j++;
		if(j<i-1)
			p=p->next;
		else
			break;
	}
	if(j>i-1||!p)
		return false;
	q=p->next;
	p->next=q->next;
	e=q->data;
	free(q);
	return true;
}
void main()
{
	LinkList Llist,p;
	int k;
	int elemet;
	int position;
	printf("input the number of LinkList to be created:");
	scanf("%d",&k);
	creatLinklist(Llist,k);
	printf("\n");
	printf("input the position to delete in LinkList:");
	scanf("%d",&position);
	printf("\n");
	LinklistDelete(Llist,position,elemet);
	printf("output the deleted data of Node:%d\n",elemet);
	printf("\n");
	printf("output the new LinkList:\n");
	p=Llist->next;
	while(p)
	{
		printf("%d ",p->data);
		p=p->next;
	}
	printf("\n");
	free(Llist);
}

运行结果如下:

《单链表的插入与删除算法》


    原文作者:yychenxie21
    原文地址: https://blog.csdn.net/chenxieyy/article/details/49047199
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞