创建单链表
// ds01.cpp: 分别采用前插法和后插法创建链表
// 最新修改:2010-10-21
// 作者:zxh
#include "stdafx.h"
using namespace std;
//链表节点
struct Book
{
string name;
Book *next;
};
//前插法创建链表
Book *CreateLink()
{
size_t bookNum = 0;
string name = "";
Book *head = new Book();
head->next = NULL;
cout << "please input the number of the books : ";
if(cin >> bookNum,bookNum!=0)
{
for(size_t i=0; i< bookNum ; i++)
{
cout << endl << "please input the " << (i+1) << "th book's name: ";
cin >> name;
Book *p = new Book();
p->name = name;
//前插法
p->next = head->next;
head->next = p;
}
}
else
{
cout << endl << "invalid input!" << endl;
exit(EXIT_FAILURE);
}
return head;
}
//尾插法创建链表
Book *CreateLinkEnd()
{
size_t bookNum = 0;
string name = "";
Book *head = new Book();
Book *parent = head;
head->next = NULL;
cout << "please input the number of the books : ";
if(cin >> bookNum,bookNum!=0)
{
for(size_t i=0; i< bookNum ; i++)
{
cout << endl << "please input the " << (i+1) << "th book's name: ";
cin >> name;
Book *p = new Book();
p->name = name;
//尾插法
p->next = NULL;
parent->next = p;
parent = p;
}
}
else
{
cout << endl << "invalid input!" << endl;
exit(EXIT_FAILURE);
}
return head;
}
void ShowBookNames(Book* head)
{
cout << "you have the following books:";
while(head!=NULL)
{
cout << head->name << endl;
head=head->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Book *head = CreateLink();
ShowBookNames(head);
Book *tail = CreateLinkEnd();
ShowBookNames(tail);
char *str = "Linux was first developed for 386/486-based PCs";
char *accept = "aabcLiddnaacxujjd";
cout << "Has:" << strcspn(str,accept);
time_t timp;
time(&timp);
cout << endl << asctime(gmtime(&timp)) << endl;
cout << ctime(&timp) << endl;
char *a = (char *)calloc(20,sizeof(char));
char b[] = "string(b)";
_memccpy(a,b,'b',sizeof(b));
cout << "memccpy():" << a;
system("pause");
return 0;
}
// 最新修改:2010-10-21
// 作者:zxh
#include "stdafx.h"
using namespace std;
//链表节点
struct Book
{
string name;
Book *next;
};
//前插法创建链表
Book *CreateLink()
{
size_t bookNum = 0;
string name = "";
Book *head = new Book();
head->next = NULL;
cout << "please input the number of the books : ";
if(cin >> bookNum,bookNum!=0)
{
for(size_t i=0; i< bookNum ; i++)
{
cout << endl << "please input the " << (i+1) << "th book's name: ";
cin >> name;
Book *p = new Book();
p->name = name;
//前插法
p->next = head->next;
head->next = p;
}
}
else
{
cout << endl << "invalid input!" << endl;
exit(EXIT_FAILURE);
}
return head;
}
//尾插法创建链表
Book *CreateLinkEnd()
{
size_t bookNum = 0;
string name = "";
Book *head = new Book();
Book *parent = head;
head->next = NULL;
cout << "please input the number of the books : ";
if(cin >> bookNum,bookNum!=0)
{
for(size_t i=0; i< bookNum ; i++)
{
cout << endl << "please input the " << (i+1) << "th book's name: ";
cin >> name;
Book *p = new Book();
p->name = name;
//尾插法
p->next = NULL;
parent->next = p;
parent = p;
}
}
else
{
cout << endl << "invalid input!" << endl;
exit(EXIT_FAILURE);
}
return head;
}
void ShowBookNames(Book* head)
{
cout << "you have the following books:";
while(head!=NULL)
{
cout << head->name << endl;
head=head->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Book *head = CreateLink();
ShowBookNames(head);
Book *tail = CreateLinkEnd();
ShowBookNames(tail);
char *str = "Linux was first developed for 386/486-based PCs";
char *accept = "aabcLiddnaacxujjd";
cout << "Has:" << strcspn(str,accept);
time_t timp;
time(&timp);
cout << endl << asctime(gmtime(&timp)) << endl;
cout << ctime(&timp) << endl;
char *a = (char *)calloc(20,sizeof(char));
char b[] = "string(b)";
_memccpy(a,b,'b',sizeof(b));
cout << "memccpy():" << a;
system("pause");
return 0;
}