创建单链表
2010-10-28 14:50
183 查看
// 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;
}
相关文章推荐
- 线性表---单链表(创建、插入、删除、排序、测长和打印输出)
- 单链表的创建,删除,插入以及打印。
- 单链表的创建删除排序插入逆向打印各种操作
- 头插法创建单链表
- 数据结构单链表之创建链表(头插法)
- 单链表的创建,插入,删除等操作——精简版
- 用头插法实现单链表整表创建
- 头插法(逆序)和尾插法(顺序)实现单链表的创建与排序
- 单链表的创建(有头结点和无头结点)
- 单链表操作,创建,遍历,插入,删除,排序等操作
- 单链表的创建与操作
- 面试题一 C/C++面试秘笈之单链表的创建,增删改查及打印
- 创建无头结点单链表
- 数据结构——单链表的创建、删除、遍历以及节点的插入、删除等操作
- 单链表的创建,查找,删除,顺序打印,倒序打印(剑指offer)
- 单链表操作——创建
- 单链表的创建、插入、删除、倒置操作
- 单链表的创建及输出
- 单链表的创建
- 数据结构单链表之创建链表(尾插法)