#include <iostream>
using namespace std;
int strcmp(char *s1, char *s2){
while(*s1 && *s2 && *s1 == *s2){
++s1;
++s2;
}
return *s1 - *s2;
}
int main(){
char s1[500] = {'\0'};
char s2[500] = {'\0'};
cout<<"s1:";cin>>s1;
cout<<"s2:";cin>>s2;
if(strcmp(s1, s2) == 0)
cout<<"s1等于s2"<<endl;
else if(strcmp(s1, s2) > 0)
cout<<"s1大于s2"<<endl;
else
cout<<"s1小于s2"<<endl;
return 0;
}
执行结果:
#include <iostream>
using namespace std;
class Node{
public:
int value;
Node *next;
Node(int value, Node *next){
this->value = value;
this->next = next;
}
};
class LinkedList{
private:
Node *head;
public:
LinkedList(){
head = NULL;
}
void addNode(int val){
if(head == NULL)
head = new Node(val, NULL);
else{
Node *p = head;
//令p指针指向最后一个节点
while(p->next){
p = p->next;
}
p->next = new Node(val, NULL);
}
}
//显示元素
void mydisplay(){
Node *p = head;
for(;p != NULL;p=p->next)
cout<<p->value<<" ";
cout<<endl;
}
//析构函数
~LinkedList(){
Node *p = head;
Node *temp = NULL;
for(;p != NULL;){
temp = p->next;
delete p;
p = temp;
}
}
};
int main(){
LinkedList ll;
cout<<"输入链表数据以-1结束:"<<endl;
int val = 0;
while(true){
cin>>val;
if(val == -1)
break;
else
ll.addNode(val);
}
//打印元素
cout<<"打印元素"<<endl;
ll.mydisplay();
return 0;
}
执行结果:
追问剩下两题呢
追答另2题,已补充