10_2.cpp
来自「本文档是(作者:钱能)《C++程序设计教程》课后习题答案。 选题编辑:张朝阳 」· C++ 代码 · 共 66 行
CPP
66 行
//10_2
#include <iostream.h>
#include <string.h>
struct Node{
char str[20];
Node* next;
};
void Insert(Node*& head);
void main()
{
Node* p;
Node* x=new Node;
strncpy(x->str,"hello",20);
x->next = NULL;
p =x;
x=new Node;
strncpy(x->str,"jone",20);
x->next = p;
p=x;
x=new Node;
strncpy(x->str,"good",20);
x->next = p;
p=x;
x=new Node;
strncpy(x->str,"better",20);
x->next = p;
p=x;
cout <<"\n插入之前:\n";
for(Node* pT=p; pT; pT=pT->next)
cout <<pT->str <<"->";
cout <<"0\n";
Insert(p);
cout <<"\n插入之后:\n";
for(Node* pT=p; pT; pT=pT->next)
cout <<pT->str <<"->";
cout <<"0\n";
}
void Insert(Node*& head)
{
Node* p=new Node;
strncpy(p->str,"marit",20);
head->str[19]='\0';
if(!head){
head = p;
p->next = NULL;
return;
}
if(!strcmp(head->str, "jone")){
p->next = head;
head = p;
return;
}
Node* sp;
for(sp=head; sp->next&& strcmp(sp->next->str,"jone"); sp=sp->next);
p->next = sp->next;
sp->next = p;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?