Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <string>
- #include <string.h>
- using namespace std;
- struct Node{
- int val;
- Node *next;
- };
- struct linkedlist
- {
- private:
- Node *head = NULL;
- public:
- void push_front(int v){
- Node *temp = new Node;
- temp->val = v;
- temp->next = NULL;
- if (head == NULL){
- head = temp;
- }
- else{
- temp->next = head;
- head = temp;
- }
- }
- void display()
- {
- if (head == NULL){
- cout << "no element." << endl;
- }
- else{
- Node *temp = head;
- while (temp != NULL){
- cout << temp->val << " ";
- temp = temp->next;
- }
- cout << endl;
- }
- }
- void dispose(){
- Node *temp;
- while (head != NULL){
- temp = head;
- head = head->next;
- delete temp;
- }
- cout << "all elements are cleared." << endl;
- }
- int pop_front(){
- if (head == NULL){
- cout << "list is empty." << endl;
- return -1;
- }
- else{
- Node *temp = head;
- head = temp->next;
- int res = temp->val;
- delete temp;
- return res;
- }
- }
- int get_the_first_element(){
- if (head == NULL){
- cout << "no elements. " << endl;
- return -1;
- }
- else{
- return head->val;
- }
- }
- Node *search(int v){
- Node *temp = head;
- int count = 0;
- while (temp != NULL)
- {
- if (temp->val == v)
- {
- return temp;
- }
- else
- {
- temp = temp->next;
- count++;
- }
- }
- return NULL;
- }
- bool deleted(int v){
- if (head == NULL){
- return false;
- }
- else if (head->val == v){
- pop_front();
- return true;
- }
- else{
- Node *temp = head;
- while (temp->next != NULL){
- if (temp->next->val == v){
- Node *temp2 = temp->next;
- temp->next = temp->next->next;
- delete temp2;
- return true;
- }
- temp = temp->next;
- }
- }
- }
- Node *getelement(int pos)
- {
- Node *temp = head;
- int count = 0;
- while (temp != NULL)
- {
- if (pos == count)
- {
- return temp;
- }
- else
- {
- count++;
- temp = temp->next;
- }
- }
- return NULL;
- }
- bool deleteat(int pos)
- {
- int count = 1;
- if (head == NULL)
- {
- cout << "no eleement is here!!!!!!" << endl;
- return false;
- }
- else if (pos == 1)
- {
- pop_front();
- return true;
- }
- else
- {
- Node *temp = head;
- while (temp->next != NULL)
- {
- if (pos == count)
- {
- Node *temp2 = temp->next;
- temp->next = temp->next->next;
- delete temp2;
- return true;
- }
- count++;
- temp = temp->next;
- }
- }
- }
- };
- int main()
- {
- linkedlist l1;
- cout << "Choose one of the option: " << endl;
- cout << "1. Insert element at the 1st position: " << endl;
- cout << "2. print all element: " << endl;
- cout << "3. delete an element at first position: " << endl;
- cout << "4. search by value : " << endl;
- cout << "5. get element at given position: " << endl;
- cout << "6. Delete element at given index: " << endl;
- cout << "7. delete element of given value: " << endl;
- cout << "8. Quit " << endl;
- int po, r = 0, element;
- while (r < 9)
- {
- cout << "enter option:";
- cin >> r;
- if (r == 1)
- {
- cout << "enter element:";
- cin >> element;
- l1.push_front(element);
- }
- else if (r == 2)
- {
- l1.display();
- }
- else if (r == 3)
- {
- l1.pop_front();
- }
- else if (r == 4)
- {
- cout << "enter element:";
- cin >> element;
- struct Node *m = l1.search(element);
- if (m == NULL)
- cout << "element not found" << endl;
- else
- cout << "element found" << endl;
- }
- else if (r == 5)
- {
- cout << "enter position:";
- cin >> po;
- struct Node *m = l1.getelement(--po);
- if (m == NULL)
- cout << "element not found" << endl;
- else
- cout << m -> val << endl;
- }
- else if (r == 6)
- {
- cout << "enter position:";
- cin >> po;
- if (l1.deleteat(po) == true)
- {
- cout << "element deleted" << endl;
- }
- else
- {
- cout << "element not found" << endl;
- }
- }
- else if (r == 7)
- {
- cout << "enter element:";
- cin >> element;
- if (l1.deleted(element) == true)
- {
- cout << "element deleted" << endl;
- }
- else
- {
- cout << "element not found" << endl;
- }
- }
- else if (r == 8)
- {
- l1.dispose();
- return 0;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement