You are here:Home » CPP » Linear Linked List

Linear Linked List

These are the functions that you can use in a linear linked list:

The basic linked list node structure:

struct Node{
 int data;
 struct Node*addr;
}*head, *current, *node;

The class:

class list{
public:
 int createlist();
 int appendend();
 int delnode(int key);
 int search(int key);
 int displaynodes();
 int appendmiddle(int pos, int data);
 void swapnode();
 void sort();
};

The create list function:

int list::createlist(){
 int headdata,datanode,number,count;
 char ch;
 cout << "enter the data for head node";
 head = new Node;
 cin >> headdata;
 head->data = headdata;
 current = head;
 cout << "how many nodes you want to enter?";
 cin >> number;
 count = 1;
 do{
  int data;
  cout << "enter data to be entered";
  cin >> data;
  Node *addNode = new Node;
  addNode->data = data;
  current->addr = addNode;
  addNode->addr = NULL;
  current = addNode;
  count++;
 }while (count <=number)
 return 0;
}

The display nodes function:

int list::displaynodes(){
 Node *temp = new Node;
 temp = head;
 do{
  cout << temp->data << endl;
  temp = temp->addr;
 } while (temp != current);
 return 0;
}

The add to end of the list function:

int list::appendend(){
 int data;
 cout << "enter data to be entered";
 cin >> data;
 Node *addNode = new Node;
 addNode->data = data;
 current->addr = addNode;
 addNode->addr = NULL;
 current = addNode;
 return 0;
}

The append in middle of the list function:

int list::appendend(){
 int data;
 cout << "enter data to be entered";
 cin >> data;
 Node *addNode = new Node;
 addNode->data = data;
 current->addr = addNode;
 addNode->addr = NULL;
 current = addNode;
 return 0;
}

More functions soon...