如何从链表中弹出元素?

我正在尝试使用各种推送和弹出function来学习链表,但我无法从链表中的尾部弹出元素。 任何人都可以帮我解决popBackfunction吗?

我尝试过类似的东西:

 typedef struct { float val; }data; typedef struct nodePtr { struct nodePtr *next; data *d; }node; typedef struct { node *head; node *tail; }linkList; linkList* createlinkList() { linkList *ll = (linkList*)malloc(sizeof(linkList)); ll->head = NULL; ll->tail = NULL; return ll; } node* createNode(data *d) { node *nd = (node*)malloc(sizeof(node)); nd-> d = d; nd-> next = NULL; return nd; } data* createData(float val) { data *iptr = (data*)malloc(sizeof(data)); iptr->val = val; return iptr; } void addFront(linkList *ll,data *d) { node *new_node = createNode(d); if(ll->head == NULL && ll->tail == NULL ) { ll->head = new_node; ll->tail = new_node; } else { new_node->next = ll->head; ll->head = new_node; } } void addBack(linkList *ll,data *d) { node *new_node = createNode(d); if(ll->head == NULL && ll->tail == NULL ) { ll->head = new_node; ll->tail = new_node; } else { ll->tail->next = new_node; ll->tail = new_node; } } void printList(linkList *ll) { node *temp = ll->head; while(temp!=NULL) { printf("%f\n",temp->d->val); temp = temp->next; /*if(temp==ll->tail) { printf("%f\n",temp->d->val); break; }*/ } } int listSize(linkList *ll) { int size=0; node *count; for(count=ll->head;count!=NULL;count=count->next) { size++; } return size; } data* popFront(linkList *ll) { data *popf; popf = ll->head->d; node *temp = ll->head; ll->head = ll->head->next; free(temp); return popf; } **data* popBack(linkList *ll) { data *popb; popb = ll->tail->d; while(ll->head->next!=NULL) { printf("%f\n",ll->head->next->d->val); if(ll->head->next==NULL) { node* temp = ll->head->next; free(temp); } } return popb; }** int main(int argc, char* argv[]) { linkList *ll = createlinkList(); data *iptr = createData(11.10); addFront(ll,iptr); data *iptr1 = createData(10.10); addFront(ll,iptr1); data *iptr2 = createData(12.10); addBack(ll,iptr2); printList(ll); int count = listSize(ll); printf("%d\n",count); popFront(ll); printList(ll); popBack(ll); printList(ll); return 0; } 

你有一个单链接列表。 你需要做几件事。 您需要找到列表尾部之前的节点(仅当列表中有2个或更多节点时才存在),并提取尾部(易于查找,您有指向它的指针)。

  • 找到尾巴
  • 找到尾部的节点prev(ious)
  • 将尾部指向prev(ious)节点
  • 将prev(ious)设置为NULL(已删除)旁边,或者
  • 将头设置为NULL(列表中只有一个项)

怎么做?

 data* popBack(linkList *ll) { node* prev; //need to find this node* iter; //iterator through list data *popb; if(!ll) return NULL; //list is non-existant if(!ll->head) return NULL; //list is empty prev=>NULL; //don't have a previous yet. iter=ll->head; while(iter!=ll->tail) { prev=iter; //not the tail, there must be a node after this iter=iter->next; //not null, could be tail } //iter==ll->tail, so prev must be either before tail or NULL (only 1 node on list) if( prev==NULL ) { ll->head=NULL; } else prev->next=NULL; printf("%f\n",ll->tail->d->val); popb = ll->tail->d; free(ll->tail); //free old tail ll->tail = prev; //new tail return popb; } 

嘿撕裂扭转链表。

你可以通过转到最后一个节点来反转链表,将最后一个节点存储在临时节点中,然后反转指针。 使用递归使其变得简单。

 if lastbutone->next->next=NULL lastbutone= temporary recursion last->next=temporary; last=temporary; 

注意最后一个节点和第一个节点,给最后一个节点命名为headptr ..当你到达第一个节点然后为其下一个节点分配null。

 data* popBack(linkList *ll) { data *popb; popb = ll->tail->d; node* ptrf = ll->head; node* ptre = ll->tail; if(ptrf == NULL && ptre == NULL){ //no node in the linkList return NULL; }else if(ptrf == ptre){ //only one node in the linkList free(ptre); ptrf = ptre = NULL; return popb; }else { //at least two nodes in the linkList while((ptrf->next) != ptre) { ptrf = ptrf->next; } free(ptre); ptrf->next = NULL; ll->tail = ptrf; return popb; } }