Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct node
- {
- int info;
- struct node *link;
- } *start;
- void createlist(int data);
- void display();
- void addtobeg(int data);
- void addtoend(int data);
- void addtopos(int data,int pos);
- void finddata();
- void arrange(){
- struct node *ptr,*temp;
- ptr = new node;
- ptr = start;
- bool change = true;
- while(change==true)
- {
- ptr = start;
- while(ptr->link!=NULL){
- ptr = ptr->link;
- if((ptr->info)>(ptr->link->info)){
- temp = new node;
- temp->info = ptr->info;
- ptr->link->info = ptr->info;
- ptr->info = temp->info;
- delete temp;
- change = true;
- }
- else
- change = false;
- }
- }
- cout<<"\n\n--------------------Success---------------------\n\n";
- }
- void del();
- void newline();
- void delatpos(int pos);
- int main()
- {
- int choice, elem, i, num, addval,addvall, pos;
- start = NULL;
- restart:
- while (1) {
- system("cls");
- cout << "enter choice : \n1 - create link list \n2 - add at the beginning \n3 - add at the end \n4 - Add at certain position\n5 - Delete a value at position\n6 - delete at the end \n7 - find position of value\n8 - display \n9 - arrange values to ascending order\n20 - exit\n";
- cout<<"Choice: ";cin >> choice;cout<<endl;
- switch (choice)
- {
- case 1: cout << "Create link list \n";
- cout << "Enter the number of elements: \n";
- cin >> num;
- for (i = 0; i < num; i++)
- {
- cout << "Enter the elements \n";
- cin >> elem;
- createlist(elem);
- }
- break;
- case 2: if(start == NULL){
- cout<<"No link list created\nProgram will now exit.";
- system("pause"); exit(0);
- }
- else
- cout << "Add the beginning" << endl;
- cin >> addval;
- addtobeg(addval);
- break;
- case 3:if(start == NULL){
- cout<<"No link list created\nProgram will now exit.";
- system("pause"); exit(0);
- }
- else
- cout << "Add to the end" << endl;
- cin >> addval;
- addtoend(addval);
- break;
- case 4:if(start == NULL){
- cout<<"No link list created\nProgram will now exit.";
- system("pause"); exit(0);
- }
- else
- cout<<"Add at certain position\nNOTE: You can not add at the beginning. For that function please choose option [1] next time.\n";
- cout<<"Input value to be added: ";
- cin>>addvall;void newline();
- cout<<"Input position: ";
- cin>>pos;
- cout<<"Value : \""<<addvall<<"\""<<" will be added to the position: ["<<pos<<"]\n";
- system("pause"); addtopos(addvall,pos);
- break;
- case 5:if(start == NULL){
- cout<<"No link list created\nProgram will now exit.";
- system("pause"); exit(0);
- }
- else
- display();
- cout<<"\nDelete at position.\nInput position you want to delete: ";
- cin>>pos;
- delatpos(pos);
- break;
- case 6:if(start == NULL){
- cout<<"No link list created\nProgram will now exit.";
- system("pause");
- exit(0);
- }
- else
- del();
- break;
- case 7:
- finddata();
- break;
- case 8:
- if(start == NULL){
- cout<<"No link list created\nPlease make one first.";
- system("pause");
- goto restart;
- }
- else
- display();
- break;
- case 9:
- arrange();
- break;
- case 20:
- exit(0);
- default:
- cout<<"Choice not found\n";system("pause");
- goto restart;
- }
- }
- system("pause>0");
- return 0;
- }
- void createlist(int data)
- {
- struct node *tail, *ptr;
- ptr = new node;
- ptr->info = data;
- ptr->link = NULL;
- if (start == NULL)
- start = ptr;
- else
- {
- tail = start;
- while (tail->link != NULL)
- tail = tail->link;
- tail->link = ptr;
- }
- }
- void addtobeg(int data)
- {
- struct node *ptr;
- ptr = new node;
- ptr->info = data;
- ptr->link = start;
- start = ptr;
- }
- void display()
- {
- struct node * tail;
- tail = start;
- while (tail != NULL)
- {
- cout << tail->info << ", ";
- tail = tail->link;
- }
- system("pause>0");
- }
- void addtoend(int data)
- {
- struct node *ptr, *m;
- ptr = new node;
- ptr->info = data;
- m = start;
- while (m->link != NULL)
- {
- m = m->link;
- }
- m->link = ptr;
- ptr->link = NULL;
- }
- void del() {//at the end only
- struct node *tail,*ptr,*tera;
- ptr = start;
- while (ptr->link->link != NULL)
- {
- ptr = ptr->link;
- }
- delete ptr->link;
- ptr->link = NULL;
- }
- void addtopos(int data,int pos){
- struct node *ptr,*temp, *temp2;
- ptr = new node;
- ptr->info = data;
- temp = start;
- temp2 = start;
- for(int i = 0; i<pos-2;i++)
- {
- temp = temp->link;
- }
- for(int i = 0;i<pos-1;i++)
- {
- temp2 = temp2->link;
- }
- temp->link = ptr;
- ptr->link = temp2;
- }
- void delatpos(int pos){
- struct node *temp, *ptr;
- if (pos != 1){
- temp = start;
- ptr = start;
- for(int i = 0; i<pos-2;i++)
- {
- temp = temp->link;
- }
- for(int i = 0; i<pos;i++)
- {
- ptr = ptr->link;
- }
- delete temp->link;
- temp->link = ptr;
- }
- //1st value
- else if (pos == 1){
- ptr = start;
- temp = ptr->link;
- delete ptr;
- ptr = temp;
- start = ptr;
- }
- }
- void finddata(){
- cout<<"Enter data you want to find: ";
- int i = 1, j;
- // d = &j;
- node *ptr, *d, *y;
- y = new node;
- cin>>y->info;
- // y->info = j;
- ptr = start;
- cout<<"The position[s] of the value you have entered are: ";
- while (ptr->link!=NULL){
- ptr = ptr->link;
- if(ptr->info == y->info){
- cout<<"["<<i+1<<"]"<<" ";
- }i++;
- }
- cout<<"\n";
- system("pause");
- }
- void newline()
- {
- char s;
- do {
- cin.get(s);
- } while (s != '\n');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement