数据结构 - C++单链表删除指定元素的问题?
黄舟
黄舟 2017-04-17 15:29:02
0
2
749
template <class T>
bool SingleList<T>::Delsame(T x)
{
    Node<T> *head;
    if(NULL == head) return head;
    Node<T> *p=head;
    Node<T> *first=NULL;
    while(NULL != p){
        if(p->element == x){
            Node<T> *del =p;
            p=p->link;
            if(NULL!= first){
                first->link=p;
            }else{
                head=p;
            }
            delete del;
        } else{
            first=p;
            p=p->link;
        }
    }
    return head;
}

代码如上,如 1 2 3 2 4 5 2
需要删除所有的2,
希望得到的结果是1 3 4 5
但目前得到的是1 2 3 4 5,
以及存在无法删除只有一个的元素、连续相同元素无法删除的问题,
如何修改可以将所有相同元素都删去呢?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

모든 응답(2)
大家讲道理
여기에 몇 가지 질문이 있습니다

처음에는 정의되었지만 값을 할당하지 않고 널 검사를 수행했습니다. 과제가 없지만 는 무작위 주소이지만 여전히 무작위 주소이지만 여전히 매우 명확한 논리적 오류입니다
    .
  1. 단어의 의미의 관점에서 첫 번째 노드를 의미하지만 이전 노드를 나타냅니다. 같은 방식으로, 현재 포인터 의 이름을 로 바꾸는 것이 좋습니다. <..> IF 조건에서 if 조건에서는 head를 가리키기 위해 a head를 정의 할 필요가없는 것 같습니다.变> 현재 포인터를 변경합니다. <..> 로 정의되었지만 반품은 입니다.
  2. 다른 논리에서 발견 된 문제는 위의 문제를 먼저 분석 한 다음 효과를 볼 수 있다고 생각합니다.
  3. javaScript 에 대한 알고리즘을 제공하십시오 JavaScript는 주로 알고리즘 아이디어를 제공합니다. 그런 다음이 아이디어에 따라 C 를 어떻게 구현할 수 있는지 생각할 수 있습니다. first 참고 : last p
    class SingleList {
        constructor() {
            this.head = null;
            this.tail = null;
        }
    
        print(tag) {
            let current = this.head;
            function* iterate() {
                while (current) {
                    yield current.value;
                    current = current.next;
                }
            }
            console.log(`${tag}: ${Array.from(iterate())}`);
        }
    
        add(...args) {
            let current = this.findTail();
            args.forEach(v => {
                if (current) {
                    current.next = {
                        value: v
                    };
                    current = current.next;
                } else {
                    this.head = current = {
                        value: v
                    };
                }
            });
        }
    
        findTail() {
            if (!this.head) {
                return null;
            }
    
            let current = this.head;
            while (current.next) {
                current = current.next;
            }
            return current;
        }
    
        remove(v) {
            let current = this.head;
            let last = null;
            while (current) {
                if (current.value === v) {
                    if (last) {
                        last.next = current.next;
                    } else {
                        this.head = current.next;
                    }
                    current = current.next;
                } else {
                    last = current;
                    current = current.next;
                }
            }
        }
    }
    
    function main() {
        const list = new SingleList();
        list.add(1, 2, 3, 2, 4, 5, 2);
        list.print("原始值");
        list.remove(2);
        list.print("删除 2");
        list.add(2, 3, 1, 3, 1);
        list.print("新增值");
        list.remove(1);
        list.print("删除 1");
    }
    
    main();
    current
  4. C 코드를 작성하지 마십시오. 뇌를 움직일 수 있도록 하나, 다른 하나는 환경과 일치하기에는 너무 게으르기 때문입니다
  5. .

洪涛

으아악

왼쪽이 닫히고 오른쪽이 열리는 간격입니다. 즉, [첫 번째, 마지막) 범위 내의 데이터
사용 예: li.Remove(3)
여기서: 시작 끝은 다음과 같습니다.

으아악

효과는 다음과 같습니다.

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿