C++结构体链表 / 创建链表实现输入、查找、插入、排序、求长度

程序结构(函数调用关系)

 struct Node{};                                                     //建立链表的结点
 Node* FileIn();                                                      //文件输入数据
 Node * CreateList(int n);                                        //直接输入建立链表

 void search_cout(Node *h,char d);            //查找函数(查找数据并输出该数据位置)
 Node * search(Node *h,char d);            //查找函数(查找函数并返回该函数的结点)
 Node * search_Last(Node *h,char d);      //查找函数(查找函数并返回该函数前一结点)

 void PrintList(Node *h,int count);                                     //输出该链表
 void Fileout(Node * h);                                              //文件输出数据

 void insert(Node *p,int& count);                                         //插入数据
 void del(Node *x,Node *y,int& count);                                    //删除数据

 int len(Node * h);                                                     //求链表长度
 Node * BubbleSort(Node *h,int count);                                    //冒泡排序

 void menu(int count,Node *p);                                                //菜单
 void menu2();

实现:

#include<iostream>
using namespace std;
#include<fstream>
#include<stdlib.h>

//
struct Node
{
	char data;
	struct Node* next;
};
//---------------------

//
Node* FileIn()
{
	//--------------------------------Node
	Node *p,*h,*c;
	h = new Node;
	if(h==NULL)
	{
		cout<<"Unable to allocate h memory space."<<endl;
		exit(0);
	}
	//
	h->next=NULL;
	p=h;
	
	//----------------------------------File
	fstream infile;
	infile.open("实验1.dat",ios::in);
	if(!infile)
	{
		cout<<"实验1.dat can't open."<<endl;
		abort();
	}

	//----------------------------------Read
	char ch;
	while(infile.get(ch))
	{
		c = new Node;
		if(c==NULL)
		{
			cout<<"Unable to allocate c memory space."<<endl;
			exit(0);
		}
		p->next=c;
		cout<<"ch= "<<ch<<endl;
		c->data=ch;
		cout<<"c->data= "<<c->data<<endl;
		c->next=NULL;
		p=c;
	}
	infile.close();
	return h;
}
//


Node * CreateList(int n)
{
	Node *p,*h,*c;
	//
	h = new Node;
	if(h==NULL)
	{
		cout<<"Unable to allocate memory space."<<endl;
		exit(0);
	}
	//
	h->next=NULL;
	p=h;

	//
	for(int i=0;i<n;i++)
	{
		c = new Node;
		if(c==NULL)
		{
			cout<<"Unable to allocate memory space."<<endl;
			exit(0);
		}
		p->next=c;
		cout<<"Please enter characters:";
		cin>>c->data;
		c->next=NULL;
		p=c;
	}
	return h;
}
//

void search_cout(Node *h,char d)
{
	Node* p;
	int i=1;
	p=h->next;
	while(p!=NULL)
	{
		if(p->data==d)
		{
			cout<<d<<"位于"<<"第"<<i<<"个"<<endl;
			i++;
			p=p->next;
		}
		else
		{
			i++;
			p=p->next;
		}
	}
}

//
Node * search(Node *h,char d)
{
	Node *p;
	p=h->next;
	while(p!=NULL)
	{
		if(p->data==d)
			return p;
		else
			p=p->next;
	}
	//-------
	if(p==NULL)
		cout<<"Cannot search it."<<endl;
	exit(0);              /*---------------------------*/
}
//


//
Node * search_Last(Node *h,char d)
{
	Node *p;
	Node *s;

	p=h->next;
	s=h;

	while(p!=NULL)
	{
		if(p->data==d)
			return s;
		else
		{
			s=s->next;
			p=p->next;
		}
	}
	//--------
	if(p==NULL)
		cout<<"Cannot search it."<<endl;
	exit(0);     
}

//
void PrintList(Node *h,int count)
{
	if (count==0)
	{
		cout<<"无数据"<<endl;
		return;
	}
	Node *p;
	p=h->next;
	int i=1;
	while(p!=NULL)
	{
		if(i%4!=0)
			cout<<p->data<<" ";
		else
		{
			cout<<p->data<<endl;
		}
		p=p->next;
		i++;
	}
}
//

void Fileout(Node * h)
{
	Node* p;
	p=h->next;
	fstream outfile;
	outfile.open("实验1--输出.dat",ios::out);
	if(!outfile)
	{
		cout<<"实验1--输出.dat can't open."<<endl;
		abort();
	}
	while(p!=NULL)
	{
		outfile.put(p->data);
		p=p->next;
	}
	outfile.close();
	cout<<endl<<"已经输出到文件中"<<endl;
}

//
void insert(Node *p,int& count)
{
	Node *s;
	s = new Node;
	if(s==NULL)
	{
		cout<<"Unable to allocate memory space."<<endl;
		exit(0);
	}
	cout<<"输入你要插入的字符"<<endl;
	cin>>s->data;
	s->next=p->next;
	p->next=s;
	count++;
}
//

void del(Node *x,Node *y,int& count)
{
	Node *s;
	s=y;
	x->next=y->next;
	free(s);
	count--;
}
//

int len(Node * h)
{
	Node* p;
	p=h;
	int n=0;
		while(p!=NULL)
		{
			n++;
			p=p->next;
		}
		return n;
}

Node * BubbleSort(Node *h,int count)
{
	int len=count; 
	Node * t;
	Node * x;
	t=h->next;
	x=h->next;
	x=x->next; 
	while(t->next!=NULL)
	{
		while(x!=NULL)
		{
			if(t->data>x->data)
			{
				int e;
				e=t->data;
				t->data=x->data;
				x->data=e; 
			}
			x=x->next; 
		}
		
		t=t->next;
		x=t->next;
	}
	return h;
}



//----------------------

void menu(int count,Node *p)
{
	system("cls");
	cout<<"\t\t\t\tMENU\t\t"<<endl;
	cout<<"\t\t\t|--------------------|\t\t"<<endl;
	cout<<endl;
	cout<<"\t\t\t|  [1] 建 立 数 据   |"<<endl;
	cout<<"\t\t\t|  [2] 查 找 数 据   |"<<endl;
	cout<<"\t\t\t|  [3] 插 入 数 据   |"<<endl;
	cout<<"\t\t\t|  [4] 删 除 数 据   |"<<endl;
	cout<<"\t\t\t|  [5] 打 印 数 据   |"<<endl;
	cout<<"\t\t\t|  [6] 排 序 数 据   |"<<endl;
	cout<<"\t\t\t|  [7]   退   出     |"<<endl;
	cout<<endl;
	cout<<"\t\t\t|--------------------|\t\t"<<endl;
	if(count!=0)
	{
		cout<<"已存在数据为:"<<endl;
		PrintList(p,count);
		cout<<endl;
		cout<<endl;
		cout<<"输入你需要的操作:";
	}
}
//
void menu2()
{
	system("cls");
	cout<<"\t\t\t\tMENU\t\t"<<endl;
	cout<<"\t\t\t|--------------------------|\t\t"<<endl;
	cout<<endl;
	cout<<"\t\t\t|  [1]  输 入 建 立 数 据  |"<<endl;
	cout<<"\t\t\t|  [2]  文 件 输 入 数 据  |"<<endl;
	cout<<"\t\t\t|  [3]      返     回      |"<<endl;
	cout<<endl;
	cout<<"\t\t\t|--------------------------|\t\t"<<endl;
}

//------------------------
int main()
{
	int choose;
	int choose_two;
	int count=0;
	int n;
	char w;
	Node *head;
	Node *searchpoint;
	Node *searchpoint_Last;


	while(1)
	{
		menu(count,head);
		cin>>choose;
		switch(choose)
		{
		case 1:
			menu2();
			cin>>choose_two;
			switch(choose_two)
			{
			case 1:
				cout<<"Number of input characters:";
				cin>>n;
				count=n;
				head=CreateList(n);
				system("pause");
				break;
			case 2:
				head=FileIn();
				count=len(head);
				system("pause");
				break;
			case 3:
				system("pause");
				break;
			}

			break;
//

		case 2:
			if(count==0)
			{
				cout<<"目前无数据:"<<endl;
				system("pause");
				break;
			}
			cout<<"查找的数?";
			cin>>w;
			search_cout(head,w);
			//searchpoint=search(head,w);
			//cout<<"已查找到,为:"<<searchpoint->data<<endl;
			cout<<endl;
			system("pause");
			break;
		case 3:
			if(count==0)
			{
				cout<<"目前无数据:"<<endl;
				system("pause");
				break;
			}
			cout<<"在哪个字符后插入:";
			cin>>w;
			searchpoint=search(head,w);
			insert(searchpoint,count);
			cout<<endl;
			system("pause");
			break;
		case 4:
			if(count==0)
			{
				cout<<"目前无数据:"<<endl;
				system("pause");
				break;
			}
			cout<<"需要删除的字符:";
			cin>>w;
			searchpoint=search(head,w);
			searchpoint_Last=search_Last(head,w);
			del(searchpoint_Last,searchpoint,count);
			cout<<endl;
			system("pause");
			break;
		case 5:
			if(count==0)
			{
				cout<<"目前无数据:"<<endl;
				system("pause");
				break;
			}
			PrintList(head,count);
			Fileout(head);
			cout<<endl;
			system("pause");
			break;
		case 7:
			exit(0);
			cout<<endl;
			system("pause");
			break;
			
		case 6:
			if(count==0)
			{
				cout<<"目前无数据:"<<endl;
				system("pause");
				break;
			}
			else
			head=BubbleSort(head,count);
			system("pause");
			break;
			
			
		default:
			cout<<"你使用了非法字符!"<<endl;
			cout<<endl;
			system("pause");
			break;
		}
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值