线性表

顺序表

#include<iostream>
using namespace std;
const int MAXSIZE = 1000 + 10;
typedef struct node{
	int* elem;
	int length;
}List;
//--------初始化------------
void initList(List &L){
	L.elem = new int[MAXSIZE];
	L.length = 0;
}
//--------插入---------------
void insert(List &L, int e){
	L.elem[L.length++] = e;
}
//-------删除第pos个元素--------
void Delete(List &L, int pos){
	for(int i = pos; i < L.length; i++)
		L.elem[i - 1] = L.elem[i];
	--L.length;
}
//-------查找-----------
int location(List &L, int e){
	for(int i = 0; i < L.length; i++)
		if(L.elem[i] == e) return i+1;  //返回第几个位置
	return 0;   //不存在该元素
}
//--------修改第pos个元素------
void update(List &L, int pos,int e){
	L.elem[pos - 1] = e;
}
//-----------打印--------------
void print(List &L){
	for(int i = 0; i < L.length; i++)
		cout<<L.elem[i]<<' ';
	cout<<endl;
}
//-----------主程序-------------
int main(){
	List L;
	initList(L);
	cout<<"初始链表: ";
	for(int i = 1; i <= 10; i++)
		insert(L, i);
	print(L);

	Delete(L,4);
	cout<<"删除第4个元素: ";
	print(L);

	cout<<"将第8个元素的值改为4: ";
	update(L,8,4);
	print(L);
	
	return 0;
}

有头节点单链表

#include<iostream>
using namespace std;
typedef struct node{
	int data;
	struct node *next;
}List,*pList;
//-------初始化---------
void initList(pList &head){
	head = new List;
	head -> next = NULL;
}
//-------插入------------
void insert(pList &head, int e){
	pList temp = new List;
	temp -> data = e;
	//头插
	temp -> next = head -> next;
	head -> next = temp;
}
//---------查找----------
int location(pList &head, int e){
	pList p = head -> next;
	if(p == NULL) return -1; //空表
	int loc = 1;
	while(p != NULL){
		if(p -> data == e)
			return loc;
		loc++;
		p = p -> next;
	}
	return 0;   //没有找到
}
//----------删除-------------
int Delete(pList &head, int pos){
	pList p = head;
	if(p == NULL) return -1; //空表
	while(p != NULL){
		if(pos == 1){
			pList temp = p -> next;
			p -> next = temp -> next;
			delete temp;
		}
		pos--;
		p = p -> next;
	}
	return 0;  //没有该元素
}
//---------打印----------------
void print(pList &head){
	pList p = head -> next;
	while(p != NULL){
		cout<<p -> data<<' ';
		p = p -> next;
	}
	cout<<endl;
}
//----------主程序---------------
int main()
{
	pList head;
	cout<<"创建一个链表:";
	initList(head);
	for(int i = 1; i <= 10; i++)
		insert(head,i);
	print(head);
	cout<<"查找元素为4的位置: ";
	cout<<location(head, 4)<<endl;
	cout<<"删除第5个元素 ";
	Delete(head,5);
	print(head);
	return 0;
}

无头节点单链表

#include<iostream>
using namespace std;
typedef struct node {
	int data;
	struct node *next;
}List,*pList;
//--------初始化---------
void initList(pList &head){
	head = NULL;
}
//---------插入----------
void insert(pList &head,int e){
	pList temp = new List;
	temp -> data = e;
	temp -> next = head;
	head = temp;
}
//---------删除----------
int Delete(pList &head, int pos){
	pList p = head;
	if(head == NULL) return -1;  //空表
	if(pos == 1){
		head = p -> next;
		delete p;
		return 1;  //删除成功
	}
	-- pos;
	while(p != NULL){
		if(pos == 1){
			pList temp = p -> next;
			p -> next = temp -> next;
			delete temp;
			return 1;  //删除成功
		}
		pos--;
		p = p -> next;
	}
	return 0;  //没有该元素
}
//----------查找--------
int location(pList &head, int e){
	pList p = head;
	if(p == NULL) return -1; //空表
	int loc = 1;
	while(p != NULL){
		if(p -> data == e)
			return loc;
		loc++;
		p = p -> next;
	}
	return 0;   //没有找到
}
//---------打印----------
void print(pList &head){
	pList p = head;
	while(p != NULL){
		cout<<p -> data<<' ';
		p = p -> next;
	}
	cout<<endl;
}
//---------主程序--------
int main()
{
	pList head;
	initList(head);
	cout<<"创建一个链表: ";
	for(int i = 1; i <= 10; i++)
		insert(head, i);
	print(head);
	cout<<"删除第4个元素: ";
	Delete(head,4);
	print(head);
	cout<<"元素为5的位置: ";
	cout<<location(head, 5)<<endl;
	return 0;
}

循环双链表

#include<iostream>
using namespace std;
typedef struct node{
	int data;
	struct node *pre,*next;
}*pList,List;
//----------初始化---------
void initList(pList &head){
	head = new List;
	head -> next = head;
	head -> pre = head;
}
//----------插入-----------
void insert(pList &head, int e){
	pList temp = new List;
	temp -> data = e;
	//尾插
	temp -> pre = head -> pre;
	temp -> next = head;

	temp -> next -> pre = temp;
	temp -> pre -> next = temp;
}
//----------打印--------------
void print(pList &head){
	pList p = head -> next;
	while(p != head){
		cout<<p->data<<' ';
		p = p -> next;
	}
	cout<<endl;
}
//---------删除多个---------
void Delect(pList &head,int e){
	pList p = head -> next, temp;
	while(p != head){
		if(p -> data == e){
			temp = p -> next;
			p -> next -> pre =p -> pre;
			p -> pre -> next = p ->next;
			free(p);
			p = temp;
		}
		else
			p = p -> next;
	}
}
//-----------查找--------------
int location(pList &head, int e){
	pList p = head -> next;
	int cnt = 0;
	while(p != head){
		cnt++;
		if(p -> data == e){
			return cnt;
		}
		p = p -> next;
	}
	return 0;  //不存在该元素
}
//-----------主程序---------------
int main()
{
	pList head;
	initList(head);
	cout<<"创建一个链表: ";
	for(int i = 1; i <= 10; i++)
		insert(head, i);
	print(head);
	cout<<"删除元素为5的节点: ";
	Delect(head,5);
	print(head);
	cout<<"元素为4所在的位置: ";
	cout<<location(head,4)<<endl;
	return 0;
}

将两个非递减链表合并成非递增链表

#include<iostream>
using namespace std;
typedef struct node{
	int data;
	struct node *next;
}List,*pList;
void initList(pList &head){
	head = new List;
	head -> next = NULL;
}
void insert(pList &head, int e){
	pList p = new List;
	p -> data = e;
	p -> next = head -> next;
	head -> next = p;
}
void print(pList &head){
	pList p = head -> next;
	while(p != NULL){
		cout<<p -> data<<' ';
		p = p -> next;
	}
	cout<<endl;
}
void mergelist(pList &la,pList &lb,pList &l)
{
    /*
    pre是中间指针
    pre代替一下
    pa/pb头插进去
    pa/pb“++”
    */
	pList pa,pb,pre;
	pa=la->next;
	pb=lb->next;
 
	l=la;
	la->next=NULL;
 
	while(pa&&pb)
	{
 
		if(pa->data<=pb->data)
		{
			pre=pa->next;
			pa->next=la->next;
			la->next=pa;
		    pa=pre;
		}
		else
		{
			pre=pb->next;
			pb->next=la->next;
			la->next=pb;
			pb=pre;
		}
	}
 
	while(pa)
	{
		pre=pa->next;
        pa->next=la->next;
		la->next=pa;
		pa=pre;
	}
 
	while(pb)
	{
        pre=pb->next;
	    pb->next=la->next;
		la->next=pb;
		pb=pre;
	}
 
 
	free(lb);
}

int main()
{
	pList LA, LB, LC;
	initList(LA);
	initList(LB);
	for(int i = 10; i>=1;i--){
		insert(LA,i);
		insert(LB,i);
	}
	cout<<"A链表 :";
	print(LA);
	cout<<"B链表:";
	print(LB);
	cout<<"合并之后: ";
	mergelist(LA,LB,LC);
	print(LC);

	return 0;
}

简单的学生管理系统

基于双向循环链表实现

#include<iostream>
#include<string>
using namespace std;
typedef struct node{
	string name;
	int id;
	string age;
	string gender;
	struct node *pre,*next;
}*pList,List;
//----------初始化---------
void initList(pList &head){
	head = new List;
	head -> next = head;
	head -> pre = head;
}
//----------插入-----------
void insert(pList &head, pList &temp){
	//尾插
	temp -> pre = head -> pre;
	temp -> next = head;

	temp -> next -> pre = temp;
	temp -> pre -> next = temp;
}
//----------录入------------
void create(pList &head){
	int n;
	cout<<"请输入插入学生的数量: ";
	cin>>n;
	cout<<"请依次输入学生的学号、姓名、性别、年龄"<<endl;
	for(int i = 0;i < n; i++){
		pList p = new List;
		cin>>p->id>>p->name>>p->gender>>p->age;
		insert(head,p);
	}
}
//----------打印--------------
void print(pList &head){
	pList p = head -> next;
	cout<<"输出学生的信息,依次为学号、姓名、性别、年纪"<<endl;
	while(p != head){
		cout<<p->id<<' '<<p->name<<' '<<p->gender<<' '<<p->age<<endl;;
		p = p -> next;
	}
}
//------------删除------------
void Delect(pList &head){
	string name;
	cout<<"请输出要删除学生的姓名:";
	cin>>name;
	pList p = head -> next, temp;
	while(p != head){
		if(p -> name == name){
			temp = p -> next;
			p -> next -> pre =p -> pre;
			p -> pre -> next = p ->next;
			free(p);
			p = temp;
			cout<<"删除成功"<<endl;
			break;
		}
		else
			p = p -> next;
	}
}
//-----------查找--------------
void search(pList &head){
	string name;
	cout<<"请输入学生的姓名: ";
	cin>>name;
	pList p = head -> next;
	while(p != head){
		if(p -> name == name){
			cout<<p->id<<' '<<p->name<<' '<<p->gender<<" "<<p->age<<endl;
			break;
		}
		p = p -> next;
	}
	cout<<"不存在该同学"<<endl;
}
//-----------菜单----------------
void menu(){
	cout<<endl<<"    菜单     "<<endl;
	cout<<"0 :数据录入"<<endl;
	cout<<"1 :数据打印"<<endl;
	cout<<"2 :数据插入"<<endl;
	cout<<"3 :数据删除"<<endl;
	cout<<"4 :数据排序"<<endl;
	cout<<"5 :数据查找"<<endl;
	cout<<"6 :退出程序"<<endl;
}
//----------排序---------------
void sort(pList &head)
{
    pList tmp = head->next,p = NULL,q = NULL;
 
    //将链表置空
    head->next = head;
    head->pre = head;
 
    while(tmp!=head){
        //保存原链表中tmp的下一个节点
        q = tmp->next;
        
        //找到tmp插入的位置
        for(p=head->pre;p!=head&&p->id>tmp->id;p=p->pre);
        //将tmp插入到p之后
        tmp->pre = p;
        tmp->next = p->next;
        p->next->pre = tmp;
        p->next = tmp;
 
        //将tmp指向要插入的下一个节点
        tmp = q;
    }
    
}
//-----------主程序---------------
int main()
{
	pList head;
	initList(head);
	int choice;
	while(1){
		menu();
		cout<<"请选择功能:";
		cin>>choice;
		switch(choice){
			case 0:    	//数据录入
				create(head);
				break;
			case 1:     //打印
				print(head);
				break;
			case 2:   	//数据插入
				create(head);
				break;
			case 3:		//数据删除
				Delect(head);
				break;
			case 4:		//排序
				sort(head);
				print(head);
				break;
			case 5:		//查找
				search(head);
				break;
			case 6:     //退出
				exit(0);
				break;
		}
	}
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值