删除链表中的指定元素

输入n个元素的数组,把该数组建成链表,删除链表中的指定元素

输入:

n

n个元素

x指定元素

#include<iostream>
#define MAXSIZE 100
using namespace std;
typedef struct Lnode {
    int data;
    Lnode* next;
}linknode;

void Delete(linknode* &L, int x) {
    linknode* q;
    q = L->next;
    /*while (q->data == x) {
        q = q->next;
    }*/
    //L = q;
   // q = q->next;
    linknode* m = L;
    while (q != NULL) {
        if (x == q->data) {
            m->next = q->next;
            q = q->next;
        }
        else{
        	m=q;
        	q = q->next;
		}
    }
}

int main()
{
    int n;

    while (cin >> n)
    {
        if (n == 0) break;
        linknode* L = (linknode*)malloc(sizeof(linknode));
        linknode* q = L;
        while (n--) {
            linknode *p = (linknode*)malloc(sizeof(linknode));
           
            cin >> p->data;
            q->next=p;
            q=p;
            p = p->next;
        }
        q->next = NULL;
        
        int x;
        cin >> x;
       
        Delete(L, x);
        linknode *p = (linknode*)malloc(sizeof(linknode));
        p=L->next;
        while(p!=NULL){
            cout << p->data<<" ";
           	p = p->next;
		}
    }
    return 0;
}

相关推荐

  1. 删除指定元素

    2024-03-18 00:42:02       40 阅读
  2. 删除排序重复元素

    2024-03-18 00:42:02       56 阅读
  3. 82. 删除排序重复元素 II

    2024-03-18 00:42:02       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-18 00:42:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-18 00:42:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-18 00:42:02       82 阅读
  4. Python语言-面向对象

    2024-03-18 00:42:02       91 阅读

热门阅读

  1. reverse_iterator实现

    2024-03-18 00:42:02       31 阅读
  2. springBoot + mybatis + Vue3的前后端分离小demo

    2024-03-18 00:42:02       41 阅读
  3. 【C/C++ 学习笔记】内存

    2024-03-18 00:42:02       45 阅读
  4. 【C语言】等边等腰三角形的判断

    2024-03-18 00:42:02       40 阅读
  5. 【Git】git pull fatal: refusing to merge unrelated histories

    2024-03-18 00:42:02       46 阅读
  6. 【Vue2】v-model

    2024-03-18 00:42:02       43 阅读
  7. Git使用

    Git使用

    2024-03-18 00:42:02      33 阅读