/* 1、编写一个程序,将字符数组s2中的全部字符拷贝到字符数组s1中。不用strcpy函数。拷贝时,‘/0’也要拷贝过去。 ‘/0’后面的字符不拷贝。*/
#include<iostream>
#include<windows.h>
using namespace std;
void scopy(char a[],char b[]);
void main()
{
int i;
char a[10];
char b[10]="Hello";
scopy(a,b);
for(i=0;a[i]!='\0';i++)
cout<<a[i];
cout<<endl;
system("PAUSE");
}
void scopy(char a[],char b[])
{
int i;
for(i=0;b[i]!='\0';i++)
{
a[i]=b[i];
}
a[i]='\0';
}
1, 将char a[10]改为 char a[2], 目的地址空间不足 输出"Hello", 会出现debug error; Error message:"Run-Time Check Failure #2-Stack around the variable 'a' was corrupted.".
2, 注释掉a[i] = '/0'; 后面会出现乱码 “Hello╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠¶”.
/* 2、用指针和结构体实现一双向链表,并实现其相应的增、删、遍历功能,并在实例中应用它。*/
#include <iostream>
using namespace std;
typedef struct node
{
int number;
struct node *next;
struct node *parent;
}Node,*LinkNode;
class LinkClass
{
public:
LinkNode first,current;//头指针,当前指针
void init();//初始化函数
LinkNode Insert(int data,LinkNode cur);//插入函数
void Remove(LinkNode p);//删除函数
void Prior(LinkNode head);//遍历函数
};
void LinkClass::init()
{
struct node *head=new struct node;
struct node *tail=new struct node;
head->parent=NULL;
head->next=tail;
tail->parent=head;
tail->next=NULL;
first=current=head;
}
LinkNode LinkClass::Insert(int data,LinkNode cur)
{
struct node *newNode=new struct node;
newNode->number = data;
newNode->next = cur->next;
cur->next = newNode;
newNode->parent = cur;
newNode->next->parent = newNode;
cur=newNode;
return cur;
}
void LinkClass::Prior(LinkNode head)
{
LinkNode cur=head->next;
while(cur->next!=NULL)
{
cout<<cur->number<<" ";
cur=cur->next;
}
cout<<""<<endl;
}
void LinkClass::Remove(LinkNode cur)
{
LinkNode temp=cur;
temp->parent->next=temp->next;
temp->next->parent=temp->parent;
delete(temp);
}
void main()
{
LinkClass lc;
lc.init();
LinkNode cur=lc.current;
for(int i=0;i<=10;i++)//用循环来初始化结构体内的number
{
cur=lc.Insert(i,cur);//调用插入函数
}
LinkNode head=lc.first;
cout<<"没调用删除函数的遍历:"<<endl;
lc.Prior(head);//遍历函数
for(int j=0;j<=3;j++)//删除元素6
{
cur=cur->parent;
}
lc.Remove(cur);//执行删除函数
cout<<"调用删除函数后的遍历:"<<endl;
lc.Prior(head);
}