创建单链表

// 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 
*= 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 
*= 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 *= (char *)calloc(20,sizeof(char));
    
char b[] = "string(b)";

    _memccpy(a,b,
'b',sizeof(b));

    cout 
<< "memccpy():" << a;
    
   

    system(
"pause");
    
return 0;
}

 

转载于:https://www.cnblogs.com/zxher/archive/2010/10/28/1863460.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值