Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node {
- int data;
- struct Node* next;
- };
- void addToHead(struct Node* &initialHead, int newHeadValue);
- void displayList(struct Node* head);
- void addToTail(struct Node* initialNode, int tailValue);
- int getSize(struct Node* head);
- void addAtPosition(struct Node* head, int position, int valueToBeAdded);
- int main()
- {
- struct Node* head = NULL; // Head-ul listei, primul element din lista! NU TE ATINGE DE EL decat daca iti cere programul.
- // alocare dinamica: (<tipul elementului unde vei stoca date>)malloc(sizeof(<tipul elementului>))
- head = (struct Node*)malloc(sizeof(struct Node));
- head->data = 6;
- addToHead(head, 1);
- addToHead(head, 4);
- addToTail(head, 45);
- addToHead(head, 15);
- addToTail(head, 12);
- displayList(head);
- // 15 4 1 6 45 12
- cout << "-------------------\n\n\n";
- addAtPosition(head, 3, 999);
- displayList(head);
- // 15 4 999 1 6 45 12
- cout << "Our list has: " << getSize(head) << " elements";
- return 0;
- }
- void addAtPosition(struct Node* head, int position, int valueToBeAdded) {
- int linkedListSize = getSize(head);
- if (position > linkedListSize) {
- cout << "There are fewer elements in the list, compared to the position";
- } else {
- if (position == 1) {
- addToHead(head, valueToBeAdded);
- } else {
- int currentPosition = 1;
- struct Node* currentNode = head;
- while(currentPosition+1 < position) {
- currentPosition++;
- currentNode= currentNode->next;
- }
- struct Node* nodeToBeAdded = (struct Node*) malloc(sizeof(struct Node));
- nodeToBeAdded->data = valueToBeAdded;
- nodeToBeAdded->next = currentNode->next;
- currentNode->next = nodeToBeAdded;
- }
- }
- }
- void addToHead(struct Node* &initialHead, int newHeadValue) {
- struct Node* newHead = (struct Node*)malloc(sizeof(struct Node));
- newHead->data = newHeadValue;
- newHead->next = initialHead;
- initialHead = newHead;
- }
- void addToTail(struct Node* initialNode, int tailValue) {
- if(initialNode == NULL) {
- addToHead(initialNode, tailValue);
- } else {
- struct Node* tempNode = initialNode;
- while(tempNode->next != NULL) {
- tempNode = tempNode->next;
- }
- struct Node* tail = (struct Node*) malloc(sizeof(struct Node)); // Dynamic memory allocation
- tail->data = tailValue;
- // Tempnode will hold the last node
- tempNode->next = tail;
- tail->next = NULL;
- }
- }
- int getSize(struct Node* head) {
- if (head == NULL) return 0;
- int size = 0;
- struct Node* tempHead = head;
- while(tempHead != NULL) {
- size++; // whatever operation u want
- tempHead = tempHead->next;
- }
- return size;
- }
- void displayList(struct Node* head){
- if (head == NULL) {
- // List is empty
- cout << "List is empty";
- } else {
- struct Node* tempHead = head;
- while (tempHead != NULL) {
- cout << tempHead->data;
- if (tempHead->next != NULL) {
- cout<< "->";
- }
- tempHead = tempHead->next;
- }
- cout << endl;
- }
- }
Add Comment
Please, Sign In to add comment