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

📄 插和删.cpp

📁 数据结构实验内容
💻 CPP
字号:
/* note:Your choice is c idE */
#include "stdio.h"
#include "iostream.h"
#include "malloc.h"
#define null 0
#define error 0
#define ok 1

typedef struct lnode{
	int    data;
	struct lnode *next;
}lnode,*linklist;

 linklist createlist_l(int n){
    linklist l,p;
	l=(linklist)malloc(sizeof(lnode));
	l->next=null;
	cout<<"please input the list reserves:"<<endl;
	for(int i=n;i>0;--i){
		p=(linklist)malloc(sizeof(lnode));
		cin>>p->data;
		p->next=l->next;l->next=p;
	}
	return(l);
}

 void printlist_l(linklist l){
 	linklist p;
	p=l->next;
	while(p){
		cout<<p->data<<endl;
		p=p->next;
	}
 }
 
int listinsert_l(linklist &l,int i,int e){
	linklist p,s;
	int j;
	p=l;j=0;
	while(p&&j<i-1){p=p->next;++j;}
	if(!p||j>i-1)return error;
	s=(linklist)malloc(sizeof(lnode));
	s->data=e;
	s->next=p->next;
	p->next=s;
	return ok;
}

int listdelete_l(linklist &l,int i){
    linklist p,q;int j=0;
	p=l;
	while(p->next&&j<i-1)
	{p=p->next;  ++j;}
	if(!(p->next)||j>i-1) return error;
	q=p->next;p->next=q->next;
	  free(q);
	return ok;
}
void main()
{
 int n,i,j,e;
 linklist l;
 cout<<"How long is the linklist:"<<endl;
 cin>>n;
 l=createlist_l(n);
 printlist_l(l);
 cout<<"Now begin to do the insert"<< endl;
 cout<<"what's the position:";
 cin>>i;
 while(i>n+1||i<0)
 {
 cout<<"This is a wrong position,please input the right position:";
 cin>>i;
 }
 cout<<"The insert number:";
 cin>>e;
 listinsert_l(l,i,e);
 printlist_l(l);
 n=n+1;
 cout<<"Now begin to do the delete"<<endl;
 cout<<"what's the position:";
 cin>>j;
 while(j<0||j>n)
 {
 cout<<"This is a wrong position,please input the right position:";
 cin>>j;
 }
 listdelete_l(l,j);
 printlist_l(l);
 n=n-1;
}




⌨️ 快捷键说明

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