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

📄 sortcpp.cpp

📁 数据结构课程程序
💻 CPP
字号:
/******Don't forget to download*****
*****GRAPHICAL DATA FILE STRUCTURE*****
*****A approach to learn DFS Graphically*****
Only @  http://www.vivekpatel.cjb.net        */

//LinkList Sorting

#include <iostream.h>
#include <conio.h>

struct node{
    int data;
    struct node *next;
};

class link{
    node *head;
    public:
     void create();
     void display();
     void sort();
};


void link :: create(){
    node *newl=NULL,*end=newl;
    int data;
    head=NULL;
    cout<<"\n**Create List**\n";
    cout<<"\nEnter -999 to terminate\n";
    while(1){
       cout<<"Enter Data Value : ";
       cin>>data;
       if(data==-999)
	 break;
       else{
	   newl = new node;
	   newl->data=data;
	   if(head==NULL)
	      head=newl;
	   else
	      end->next=newl;
	   end=newl;
	   end->next=NULL;
       }
    }
    cout<<"\nList is Created\n";
    getch();
}

void link :: display(){
	node *start;
	start=head;
	cout<<"\n**Display**\n";
	while(start!=NULL){
	   cout<<start->data;
	   if(start->next!=NULL)
	       cout<<"==>";
	   start=start->next;
	}
	cout<<"\nDisplay's End\n";
	getch();
}

void link :: sort(){
	cout<<"\n**Linear Sort**\n";
	node *i, *j;
	int temp;
	for(i=head;i!=NULL;i=i->next){
	   for(j=i->next;j!=NULL;j=j->next){
	       if(i->data > j->data){
		  temp    =  i->data;
		  i->data = j->data;
		  j->data = temp;
	       }
	   }
	}
	cout<<"\nData are Sorted\n";
	getch();
}

void main(){
     int choice;
     link obj;
     while(1){
	clrscr();
	cout<<"****LinkList Sorting Demo****"<<endl;
	cout<<"1) Create List\n";
	cout<<"2) Display List\n";
	cout<<"3) Sort List\n";
	cout<<"4) Exit\n";
	cout<<"Enter your choice : ";
	cin>>choice;
	switch(choice){
	     case 1 :  obj.create();
		       break;
	     case 2 :  obj.display();
			break;
	     case 3 : obj.sort();
			break;
	     case 4 : goto out;
	     default : cout<<"\nInvalid Choice\n";
	}
     }
out:
}

⌨️ 快捷键说明

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