链表的大小功能?

时间:2016-12-16 15:11:12

标签: c++ linked-list size

这里我有问题我认为在size函数中我想要删除前后整个链表的大小基本上,size函数说错误“size必须返回值”并且返回值与返回类型不匹配!欢呼求助!!感谢

//SLL Basic insert , delete and search functions

#include<iostream>
using namespace std;

/* defines the structure of a single linked list node*/
typedef struct list_node {
    int data;
    struct list_node *next; // pointer to next node in the list 
}node;

/* create new node */
node *getNewNode(int data) {
    node *new_node = new node;
    new_node->data = data;
    new_node->next = NULL;
    return new_node;
}

/* get size of list */
node* size(node *head)
{
    int len = 0;
    while (head != NULL)
    {
        len++;
        head = head->next;

    }
    return len;
}

/* displays the list elements */
void displayList(node *head) {
    cout << "Displaying List : ";
    while (head != NULL) {
        cout << head->data << " -> ";
        head = head->next;
    }
    cout << "NULL " << endl;
}

/* Search the node with element as data
Return the pointer to the node if found else return NULL */
node *searchNode(node *head, int data) {
    node *ptr = NULL;
    while (head) {
        if (head->data == data) {
            ptr = head;
            break;
        }
        head = head->next;
    }
    return ptr;
}

/* insert a node at the beginning of the list */
node *insertNodeBeg(node *head, int data) {
    node *ptr = getNewNode(data);
    if (head == NULL) { // if list is empty
        head = ptr;
    }
    else {
        ptr->next = head;
        head = ptr;
    }
    return head;
}

/* insert a node at the end of the list */
node *insertNodeEnd(node *head, int data) {
    node *ptr = getNewNode(data);
    if (head == NULL) { //if list is empty
        head = ptr;
    }
    else {
        node *temp = head;
        while (temp->next != NULL) { // move to the last node
            temp = temp->next;
        }
        temp->next = ptr; // insert node at the end
    }
    return head;
}

/* insert a node at the after a particular node in the list */
node *insertNodeAfter(node *head, int element, int data) {
    // search the element after which node is to be inserted
    node *temp = searchNode(head, element);
    if (temp == NULL) { // element not found
        cout << "Element not found ... " << endl;
    }
    else {
        node *ptr = getNewNode(data);
        if (temp->next == NULL) { // node has to inserted after the last node
            temp->next = ptr;
        }
        else {  // insert the node after the first or an intermediate node
            ptr->next = temp->next;
            temp->next = ptr;
        }
    }
    return head;
}

/* delete a particular node from the list */
node *deleteNode(node *head, int element) {
    node *temp = searchNode(head, element); // search the node to be deleted
    if (temp == NULL) { // element not found
        cout << "Node to be deleted not found ... " << endl;
    }
    else {
        if (temp == head) { // first node is to be deleted
            head = head->next;
            delete temp;
        }
        else { // node to deleted is an intermediate or last node
            node *ptr = head;
            while (ptr->next != temp) {
                ptr = ptr->next;
            }
            ptr->next = temp->next;
            delete temp;
        }
    }
    return head;
}

int main() 
{
    node *head = NULL;
    head = insertNodeBeg(head, 7);       // 7
    head = insertNodeBeg(head, 9);       // 9 -> 7
    head = insertNodeEnd(head, 11);      // 9 -> 7 -> 11
    head = insertNodeAfter(head, 9, 4);  // 9 -> 4 -> 7 -> 11
    head = insertNodeAfter(head, 7, 3);  // 9 -> 4 -> 7 -> 3 -> 11
    head = insertNodeAfter(head, 11, 6); // 9 -> 4 -> 7 -> 3 -> 11 -> 6
    head = size(head);// HERE i want to give size of LIST i,e 6 in this case !! 
    displayList(head);
    head = deleteNode(head, 7);          // 9 -> 4 -> 3 -> 11 -> 6
    head = deleteNode(head, 6);          // 9 -> 4 -> 3 -> 11
    head = deleteNode(head, 9);          // 4 -> 3 -> 11
    head = deleteNode(head, 3);         // 4 -> 11
    displayList(head);
    return 0;
}

2 个答案:

答案 0 :(得分:0)

语句return len;返回int len = 0;声明的int,但是您的函数定义是node* size(node *head),它要求您返回指向节点结构的指针。将node* size(...)更改为int size(...)

答案 1 :(得分:0)

制作返回类型&#34; int&#34;而不是&#34;节点*&#34;。或者更确切地说,将其设为size_t,因为它必须是无符号整数值。

    /* get size of list */
    size_t size(node *head)
    {
        size_t len = 0;
        while (head != NULL)
        {
             len++;
             head = head->next;
        }
        return len;
    }