⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linklist.cpp

📁 用c语言实现的链表的各项操作的源代码
💻 CPP
字号:
#include "stdio.h"
#include "iostream"
#include "string.h"
typedef int Elemtype;
typedef struct Lnode
{ 
	Elemtype data;
	struct Lnode *next;
}Lnode,*Linklist;
void CreateList(Linklist &L,int n)
{
	Lnode *p;
	L=(Lnode *)malloc(sizeof(Lnode));
	L->next=NULL;
	for(int i=1;i<=n;i++)
	{
		p=(Lnode *)malloc(sizeof(Lnode));
		printf("please input the %d number:\n",i);
		scanf("%d",&p->data);
		p->next=L->next;
		L->next=p;
	}

}
void ShowList(Linklist &L)
{
	Linklist p;
	p=(Lnode *)malloc(sizeof(Lnode));
	p=L->next;
	while(p!=NULL)
	{
		printf("%-5d",p->data);
		p=p->next;
	}
	printf("\n");
}
void Listlen(Linklist &L)
{
	int i=0;Linklist p;
	p=(Linklist)malloc(sizeof(Lnode));
	p=L->next;
	while(p!=NULL)
	{  
		i++;
		p=p->next;
	}
	printf("the length of list is:%d\n",i);
}
void Getelem(Linklist &L,int n,int e)
{
	Linklist p;
	p=(Linklist)malloc(sizeof(Lnode));
	p=L;int i=0;
	while(i<n&&p!=NULL)
	{ i++;
	 p=p->next;
	}
	if(p==NULL)
	printf("n=%d is over the length\n",n);
	else
	{e=p->data;
	printf("the %d elem is:%d\n",n,e);}
}
void Locateelem(Linklist &L,int e)
{
	Linklist p;int i=0;
	p=(Linklist)malloc(sizeof(Lnode));
	p=L;
	while(p!=NULL&&p->data!=e)
	{p=p->next;
	 i++;
	}
	if (p==NULL)
	printf("not exists %d\n",e);
	else printf("%d is located in the %d place\n",e,i);
}
void Listinsert(Linklist &L,int n,int e)
{   
	Linklist p,q;int i=1;
	p=(Linklist)malloc(sizeof(Lnode));
	p=L;
	while(p!=NULL&&i<n)
	{ i++;p=p->next;}
	if (p==NULL) printf("n is over the list's length\n");
	else
	{ q=(Linklist)malloc(sizeof(Lnode));
	  q->data=e;
	  q->next=p->next;
	  p->next=q;
	}
}
void Listdelete(Linklist &L,int n)
{
	Linklist p;int i=1;
	p=(Linklist)malloc(sizeof(Lnode));
	p=L;
	while(p!=NULL&&i<n)
	{i++;p=p->next;}
	if (p==NULL)
	printf("del:n is over the length of the List\n");
	else
	p->next=p->next->next;
}
void Destorylist(Linklist &L)
{  
	Linklist p=L,q;
	q=p->next;
	while(p!=NULL)
	{ free(p);
	  p=q;
	  q=p->next;
	}
	free(p);
}
	
	


void main()
{
	Linklist L;int e;
	CreateList(L,5);
	ShowList(L);
	Listlen(L);
	Getelem(L,3,e);
	Locateelem(L,8);
	Listinsert(L,1,5);
	ShowList(L);
	Listdelete(L,2);
	ShowList(L);
	Destorylist(L);
	ShowList(L);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -