c++的相关问题,用c++的方法解决

关于删除单链表的倒数第i个结点的问题,用c++的方法,尽量简单,不要过于复杂,给给思路和

img

如下:

#include <iostream>
using namespace std;
typedef struct _node
{
    int data;
    struct _node* next;
}LinkNode;



int main()
{
    int pos;
    LinkNode* head,*p,*t,*front;
    int i,n;
    head = new LinkNode;
    head->next = 0;

    p = head;
    //输入n
    cin >> n;
    for(i=0;i<n;i++)
    {
        t = new LinkNode;
        t->next = 0;
        cin >> t->data; //输入数据
        p->next = t;
        p = t;
    }
    //输入需要删除的位置pos
    cin >> pos;
    if(pos>n)
    {
        cout << "error"<<endl;
        return 0;
    }
    else
    {
        pos = n - pos+1; //得到正数
        front = head;
        p = front->next;
        i=1;
        while(p && i<pos)
        {
            front = p;
            p = p->next;
            i++;
        }
        if(p)
        {
            front->next = p->next;
            delete p;
            p = 0;
        }
        else
        {
            cout <<"error";
            return 0;
        }
    }

    //输出
    p = head->next;
    while(p)
    {
        if(p==head->next)
            cout <<p->data;
        else
            cout <<" "<<p->data;
        p = p->next;
    }

    return 0;
}

若要删除链接列表的最后一个节点,请找到倒数第二个节点,并将该节点的下一个指针设为 null。
在C中使用malloc()动态分配链表的每个节点,我们需要调用free()来释放分配给要删除的节点的内存。


// CPP program to remove last node of
// linked list.
#include <iostream>
using namespace std;
 
/* Link list node */
struct Node {
    int data;
    struct Node* next;
};
 
/* Function to remove the last node 
   of the linked list */
Node* removeLastNode(struct Node* head)
{
    if (head == NULL)
        return NULL;
 
    if (head->next == NULL) {
        delete head;
        return NULL;
    }
 
    // Find the second last node
    Node* second_last = head;
    while (second_last->next->next != NULL)
        second_last = second_last->next;
 
    // Delete last node
    delete (second_last->next);
 
    // Change next of second last
    second_last->next = NULL;
 
    return head;
}
 
// Function to push node at head
void push(struct Node** head_ref, int new_data)
{
    struct Node* new_node = new Node;
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}
 
// Driver code
int main()
{
    /* Start with the empty list */
    Node* head = NULL;
 
    /* Use push() function to construct  
       the below list 8 -> 23 -> 11 -> 29 -> 12 */
    push(&head, 12);
    push(&head, 29);
    push(&head, 11);
    push(&head, 23);
    push(&head, 8);
 
    head = removeLastNode(head);
    for (Node* temp = head; temp != NULL; temp = temp->next)
        cout << temp->data << " ";
 
    return 0;
}

#include<stdio.h>
#include<stdlib.h>
typedef int Status;
typedef int ElemType;
 
 
/*线性表的单链表存储结构 */ 
typedef struct LNode{
    ElemType       data;    
    struct LNode  *next;
}LNode,*LinkList;
 
/*初始化链表*/
void InitList_L(LinkList &L,int n)
{
    int i;
    LinkList p,pre;
    L = (LinkList)malloc(sizeof(LNode));
    pre = L;              //当前指针 
    for (i=0; i<n; i++){
        p = (LinkList)malloc(sizeof(LNode));
        scanf("%d",&p->data);    
        pre->next = p; 
        p->next = NULL;
        pre = pre->next;  //当前指针后移    
    } 
}
 
/*删除单链表的倒数第i个结点*/
void DeleteList_L(LinkList &L,int i,int n)
{
    int j = 0;
    LinkList p, q;    
    p = L;
    while (p->next && j<n-i){    //寻找第n-i个结点,并另p指向其前驱
        p = p->next;
        j++;
    }
    if(!(p->next) && j<n-i) {    //删除位置不合理 
        exit(-1);    
    }
    q = (LinkList)malloc(sizeof(LNode));
    q = p->next;
    p->next = q->next;
    free(q);
}
 
/*输出链表数据*/
void OutputList_L(LinkList &L){
    LinkList p;
    p = L;
    while (p->next != NULL)
    {
        p = p->next;
        printf("%d ",p->data);
    }
    printf("\n");
}
 
int main()
{
    LinkList L;    
    int n,i;
    scanf("%d",&n);            //链表长度 
    InitList_L(L,n);
    scanf("%d",&i);
    DeleteList_L(L,i,n);    //删除倒数第i个结点 
     OutputList_L(L);
    return 0;
}