C Program for remove comments from source file of all Programming Languages
C Program for remove comments from source file of all Programming Languages
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>

class linklist {
  struct node {
    int data;
    node * next;
  }* p;
  int cn;
  public:
    linklist() {
      p = NULL;
      ifstream f;
      int num, i = 1;
      f.open("linklist.txt", ios::beg);
      f >> cn;
      while (i <= cn) {
        f >> num;
        node * q, * t;
        if (p -> next == NULL) {
          t = new node;
          t -> data = num;
          t -> next = NULL;
          p -> next = t;
        } else {
          q = p;
          while (q -> next != NULL)
            q = q -> next;
          t = new node;
          t -> data = num;
          t -> next = NULL;
          q -> next = t;
        }
        i++;
      };
      f.close();
    }~linklist() {
      int num;
      ofstream f;
      node * q;
      q = p;
      f.open("linklist.txt");
      f << cn << endl;
      while (q -> next != NULL) {
        q = q -> next;
        num = q -> data;
        f << num << endl;
      }
      f.close();
    }
  void add_last(int);
  void add_first(int);
  void add_position(int, int);
  void display();
  void del_last();
  void del_first();
  void del_position(int);
  void del_all();
  void search(int);
  void sort_asn();
  void sort_dsn();
};
void linklist::add_last(int num) {
  node * q, * t;
  if (p -> next == NULL) {
    t = new node;
    t -> data = num;
    t -> next = NULL;
    p -> next = t;
  } else {
    q = p;
    while (q -> next != NULL)
      q = q -> next;
    t = new node;
    t -> data = num;
    t -> next = NULL;
    q -> next = t;
  }
  cn++;
  cout << "One number inserted";
  getch();
}
void linklist::display() {
  node * q;
  q = p;
  if (p -> next == NULL)
    cout << "linklist is empty ";
  else {
    cout << "Content of linklist..." << endl;
    while (q -> next != NULL) {

      q = q -> next;
      cout << q -> data << " ";
    }
  }
}
void linklist::add_first(int num) {
  node * t;
  t = new node;
  t -> data = num;
  t -> next = p -> next;
  p -> next = t;
  cn++;
  cout << "One number inserted";
  getch();
}
void linklist::add_position(int num, int n) {
  node * q, * t;
  q = p;
  for (int i = 1; i < n; i++)
    q = q -> next;
  if (q -> next == NULL) {
    cout << "Can not insert ..";
    getch();
    return;
  } else {
    t = new node;
    t -> data = num;
    t -> next = q -> next;
    q -> next = t;
    cn++;
    cout << "One element inserted";
    getch();
  }
}
void linklist::del_last() {
  node * q;
  q = p;
  if (q -> next == NULL) {
    cout << "No elements in linklist";
    getch();
    return;
  } else {
    while (q -> next -> next != NULL)
      q = q -> next;
    q -> next = NULL;
    cn--;
    cout << "One element deleted";
    getch();
  }
}
void linklist::del_first() {
  node * q;
  q = p;
  if (q -> next == NULL) {
    cout << "No element in linklist";
    getch();
  } else {
    q = q -> next;
    p -> next = q -> next;
    cn--;
    cout << "One element deleted";
    getch();
  }
}
void linklist::del_position(int n) {
  node * q;
  q = p;
  for (int i = 1; i < n; i++)
    q = q -> next;
  if (q -> next == NULL) {
    cout << "No element in that position ";
    getch();
  } else {
    q -> next = q -> next -> next;
    cn--;
    cout << "Element deleted";
    getch();
  }
}
void linklist::del_all() {
  char ch;
  if (p -> next == NULL) {
    cout << "No element in linklist";
    getch();
  } else {
    cout << "Are you sure for Delete all data" << endl;
    cout << "press key y for confirm.....  ";
    ch = getche();
    if (ch == 'y' || ch == 'Y') {
      p -> next = NULL;
      cn = 0;
      cout << endl << "All node deleted.";
      getch();
    }
  }
}
void linklist::search(int num) {
  node * q;
  int flag = 0;
  q = p;
  if (q -> next == NULL) {
    cout << "No element in linklist to search";
    getch();
  } else {
    while (q -> next != NULL) {
      q = q -> next;
      if (q -> data == num) {
        flag = 1;
        cout << "Searching element found : " << q -> data << endl;
      }
    }
    if (flag == 0)
      cout << "Element not found";
    getch();
  }
}
void linklist::sort_asn() {
  node * q, * r;
  q = p;
  r = q; //->next;
  do {
    do {
      r = r -> next;
      if (q -> data >= r -> data) {
        int temp;
        temp = q -> data;
        q -> data = r -> data;
        r -> data = temp;
      }
    } while (r -> next != NULL);
    q = q -> next;
    r = q;
  } while (q -> next != NULL);
}
void linklist::sort_dsn() {
  node * q, * r;
  q = p;
  r = q;
  do {
    do {
      r = r -> next;
      if (q -> data <= r -> data) {
        int temp;
        temp = q -> data;
        q -> data = r -> data;
        r -> data = temp;
      }
    } while (r -> next != NULL);
    q = q -> next;
    r = q;
  } while (q -> next != NULL);
}
int main() {
  linklist l1;
  int ch, ch1, n, num;
  clrscr();
  do {
    clrscr();
    cout << "***********MENU************" << endl;
    cout << "1 for insert" << endl << "2 for delete" << endl << "3 for display" << endl;
    cout << "4 for Search" << endl << "5 for Sort" << endl << "6 for exit" << endl;
    cout << "Enter your choice :  ";
    ch = getche();
    if (ch == '1') {
      clrscr();
      cout << "***********MENU************" << endl;
      cout << "1 for Insert at begining" << endl;
      cout << "2 for Insert at last" << endl;
      cout << "3 for Insert at any position" << endl;
      cout << "Enter your choice  : ";
      ch1 = getche();
      clrscr();
      cout << "Enter element to insetrt  :  ";
      cin >> num;
      if (ch1 == '1') {
        l1.add_first(num);
      } else if (ch1 == '2') {
        l1.add_last(num);
      } else if (ch1 == '3') {
        cout << "Enter position of number  :  ";
        cin >> n;
        l1.add_position(num, n);
      } else {
        cout << "wrong choice";
        getch();
      }
    } else if (ch == '2') {
      clrscr();
      cout << "***********MENU************" << endl;
      cout << "1 for delete at begining" << endl;
      cout << "2 for delete at last" << endl;
      cout << "3 for delete at any position" << endl;
      cout << "4 for delete all" << endl;
      cout << "Enter your choice  :  ";
      ch1 = getche();
      if (ch1 == '1') {
        l1.del_first();
      } else if (ch1 == '2') {
        l1.del_last();
      } else if (ch1 == '3') {
        clrscr();
        cout << endl << "Enter position number   : ";
        cin >> n;
        l1.del_position(n);
      } else if (ch1 == '4') {
        clrscr();
        l1.del_all();
      } else {
        cout << endl << "wrong choice";
        getch();
      }
    } else if (ch == '3') {
      clrscr();
      l1.display();
      getch();
    } else if (ch == '4') {
      clrscr();
      cout << endl << "Enter number to be search  :   ";
      cin >> num;
      l1.search(num);
    } else if (ch == '5') {
      clrscr();
      cout << "***********MENU************" << endl;
      cout << "1 for Acending Order" << endl;
      cout << "2 for Decending Order" << endl;
      cout << "Enter your choice  :  ";
      ch1 = getche();
      if (ch1 == '1') {
        cout << endl << "After sorting...." << endl;
        l1.sort_asn();
        l1.display();
        getch();
      } else if (ch1 == '2') {
        cout << endl << "After sorting...." << endl;
        l1.sort_dsn();
        l1.display();
        getch();
      } else {
        cout << endl << "Wrong choice";
        getch();
      }
    } else if (ch == '6')
      break;
    else {
      cout << endl << "Wrong choice";
      getch();
    }
  } while (1);
  return (0);
}

This linked list program is written in the CPP language. This program have complete functionality of add, edit, view and delete features. You can run this program with cpp compiler.

Read our another Article: A classic Tank Game written in CPP language

LEAVE A REPLY

Please enter your comment!
Please enter your name here