ZBLOG

#include using namespace std; struct node { int data; node* next; }; enum errorcode { success, overflow, underflow, rangeerror }; class list { public: list(); int length()const; errorcode get_element(const int i, int& x)const; e...

在您提供的代码中,存在一些问题导致运行错误。以下是修正后的代码:

#include <iostream>
using namespace std;

struct node {
    int data;
    node* next;
};

enum errorcode { success, overflow, underflow, rangeerror };

class list {
public:
    list();
    int length() const;
    errorcode get_element(const int i, int& x) const;
    errorcode insert(const int i, const int x);
    errorcode delete_element(const int i);
    node* get_head() { return head; }
    void create1(); //头插法
    void insert1(int x); //增序排列
    void display(); //输出函数
    int search();
private:
    int count;
    node* head;
};

list::list() {
    head = new node;
    head->next = nullptr; // 修改为nullptr
    count = 0;
}

int list::length() const {
	node* p = head->next;
	int n = 0;
	while (p != nullptr) { // 修改为nullptr
		p = p->next;
		n++;
	}
	return n;
}

errorcode list::get_element(const int i, int& x) const {
	node* p = head->next;
	int j = 1; // 初始化j为1
	while (j != i && p != nullptr) { // 修改为nullptr
		p = p->next;
		j++;
	}
	if (p == nullptr)
		return rangeerror;
	x = p->data;
	return success;	
}

errorcode list::insert(const int i, const int x) {
	node* p = head; 
	int j = 0; 
	while (j != i - 1 && p != nullptr) { // 修改为nullptr
		p = p->next; 
		j++;
	}
	if (i < 1 || i > count + 1)
		return rangeerror;
	node* s = new node;
	s->data = x;
	s->next = p->next;
	p->next = s;
	count++;
	return success;
}

errorcode list::delete_element(const int i) {
	node* p = head; 
	int j = 0; 
	while (j != i - 1 && p != nullptr) { // 修改为nullptr
		p = p->next; 
		j++;
	}
	if (i < 1 || i > count)
		return rangeerror;
	node* u = p->next;
	p->next = u->next;
	delete u;
	count--;
	return success;
}

void list::create1() {
	int x;
	head->data = 0;

	cout << "输入链表内容(输入-1结束):";
	cin >> x;

	while (x != -1) {
        count++;
        node* s = new node;
        s->data = x;
        s->next = head->next;
        head->next = s;

        cin >> x;
    }
}

void list::insert1(int x) {
	node* u, *P;

	P = head;

	while (P->next != nullptr && P->next->data < x) // 修改为nullptr
	    P= P -> next;

	if (P -> next == nullptr || P -> next -> data > x) {
	    u=new node();
	    u -> data=x ;
	    u -> next=P -> next ;
	    P -> next=u ;

    	count++ ;
  	}
}

void list::display() {
	node* p=head -> next ;

	while(p!=nullptr){ // 修改为nullptr
	  	cout<<p -> data<<" ";
	 	p=p -> next ;
	}
}

int list::search() {
	node* p=head -> next;
	int n=0;

	while(p!=nullptr){ // 修改为nullptr
		if(p->data%2==0){
			p=p->next;
			n++;
		}else{
			p=p->next;
		}
	}

	return n;
}

int main() {
	list l1;
	l1.create1();
	cout << l1.search();
	return 0;
}

此代码在头插法创建链表后,统计了偶数节点的个数并输出。希望这可以解决您的问题,并且能够成功运行。


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:http://sdn.0voice.com/?id=3430

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?