Insert at the front of a linked list

来源:互联网 发布:人力资源软件排名2016 编辑:程序博客网 时间:2024/06/07 03:28

Suppose that every node contains an integer data


typedef struct IntElement{struct IntElement *next;int data;}IntElement;

First, we consider inserting a node at the head of the linked list. There is a caveat here, which requires us to use a double pointer for the head, otherwise, we will only update the local copy of head. 

bool insertInFront(IntElement **head, int data){IntElement *newElem = malloc(sizeof(IntElement));if(!newElem) return false;newElem->data = data:newElem->next = *head;*head = newElem;return true;}




阅读全文
0 0