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

📄 list1.cpp

📁 this program is link list that writted with c++. this program create a link list with object.
💻 CPP
字号:
//in barname yek linlliste ra ejra mikonad
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class list;
class node
{
	friend class list;
	node(int x){data=x;}
	node(){}
private:
	int data;
	node *next;
	node *prev;
};
//****************************************************************
class list
{
public:
	list();
	void delet();
	void add();
	void show();
private:
	node *first;
	node *last;
};
//****************************************************************
list::list()
{
	first=last=0;
}
//****************************************************************
void list::add()
{
	int x;
	cout<<"Enter a number: "<<endl;
    cin>>x;
	node *help1=new node;
	help1->next=NULL;
	help1->data=x;
    if(first==0)
	{
		first=last=help1;
		
	}
	else
	{
		last->next=help1;
		help1->prev=last;
		last=help1;
	}
	

}
//****************************************************************
void list::delet()
{
	int item;
	cout<<"Enter number: "<<endl;
	cin>>item;
	node *thisptr=first;
	node *nextptr=first;
	while(thisptr)
	{
		if(thisptr->data==item)
		{
			if(thisptr==first)
			{
				nextptr=first->next;
				delete first;
				first=nextptr;
				break;
			
			}
			else if(thisptr==last)
			{
				nextptr=last->prev;
				delete last;
				last=nextptr;
				last->next=NULL;
				break;
			}
			else
			{
				thisptr->prev->next=thisptr->next;
				thisptr->next->prev=thisptr->prev;
				delete thisptr;
				break;
			}
		}
		thisptr=thisptr->next;
	}
}
//****************************************************************
void list::show()
{
	node *help;
	help=first;
	while(help )
	{

		cout<<help->data<<"  ";
		help=help->next;
	}
	cout<<endl;
}
//****************************************************************
int menu();
int main()
{
	list li;
	for(;;)
	{
		switch(menu())
		{
		case 1:li.add();break;
		case 2:li.delet();break;
        case 3:li.show();break;
        case 4:exit(1);
		}
	}
	getch();
	return 0;

}
//****************************************************************
int menu()
{
	int choice;
	cout<<"1.Enter a student: "<<endl
		<<"2.Delete a student: "<<endl
		<<"3.Display list: "<<endl
		<<"4.End of program: "<<endl;
	cout<<"Enter yoUr select(1-4): ";
	cin>>choice;
	return choice;
}

⌨️ 快捷键说明

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