如何从链表的尾部弹出元素? [英] How to pop element from tail in linked list?

查看:102
本文介绍了如何从链表的尾部弹出元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用各种推入和弹出功能来学习链表,但是我无法从链表的尾部弹出元素.谁能帮我解决popBack函数?

I am trying to learn linked list using various push and pop functions but I am not able to do popping of element from tail in linked list. Can anyone help me to solve popBack function?

我尝试过类似的事情:

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;
}

推荐答案

嘿撕裂了链接列表.

您可以通过以下方法来反向链接列表:转到最后一个节点,将最后一个节点(但只有一个)存储在临时节点中,然后反转指针.使用递归使其简单.

you can reverse the linked list by going to last node, storing the last but one node in temporary node, and just reverse the pointers. Use recursion to make it simple.

if lastbutone->next->next=NULL
lastbutone= temporary

recursion
last->next=temporary;
last=temporary; 

当心最后一个节点和第一个节点,给最后一个节点起个名字,例如headptr ..当您到达第一个节点时,将null分配给下一个节点.

beware of the last and first node, give a name to the last node say headptr.. and when u arrive to the first node then assign null to its next.

这篇关于如何从链表的尾部弹出元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆