C++做一个链表,输出一系列字符串

10 篇文章 0 订阅
9 篇文章 0 订阅

起因

突然手痒想写个表达字符串的链表,通常都是傻傻的输出数字,id什么的。想输出一系列数字,如“wo ai wo de zhu guo”, “wo ai de ren bu ai wo”:

实现

但是又嫌扣细节麻烦,故当了一个表,
c++链表样例参考源:
自己改为实现形式:

  • 所有int都改为char []数组了;
  • 自然,所有 char *=> cha[] 改变都要用 strcpy(char[], char *)
  • 所有比较改为 strcmp(char a1[], char a2[])

效果

有意思的描述

代码

//为了学习链表,而又没时间扣很长的构建class,copy一份,自己实现简单功能
#include<iostream>
#include<string>
#include<stdio.h>
#include<conio.h>
#include<string.h>

/**
* cstdio是将stdio.h的内容用C++头文件的形式表示出来。
*stdio.h是C标准函数库中的头文件,即:standard buffered input&output。
*提供基本的文字的输入输出流操作(包括屏幕和文件等)。
*/
 
/**
*conio是Console Input/Output(控制台输入输出)的简写,其中定义了通过控制台进行数据输入和数据输出的函数,
*主要是一些用户通过按键盘产生的对应操作,比如getch()()函数等等。
*/
using namespace std;

struct node
{
	char data[20];
	node *next;
};
typedef struct node node, *list; //类型 struct node 定义为类型 node和 指针 *list;
 
// 创建单链表
node *creat()// 不断输入值,创建链表
{
	node *head, *p;
	head = new node;
	p = head;
 
	int cycle = 1;
	char x[20];
	while (cycle)
	{
		cout << "***Please input the data for single linker :  stop to end*****\n";//<<endl
		//cin >> x;
		//gets(x);
		scanf("%s", x);
 
		if (strcmp(x, "stop")!=0)
		{
			node *s = new node;			//s->data = x;
			strcpy(s->data, x);
/* 			cout << "Input data : " << endl;//<< x 
			scanf("%s", x); */
 
			p->next = s;
			p = s;
		}
		else
		{
			cycle = 0;
			cout << "Input done! " << endl;
		}
	}
 
	head = head->next;
	p->next = NULL;
	//cout << "\nFirst data of single linker is " << head->data << endl;
 
	return head;
}
 
// 单链表测长
int length(node *head)
{
	int n = 0;
	node *p = head;
 
	while (p != NULL)
	{
		p = p->next;
		n++;
	}
 
	return n;
}
 
// 单链表打印
void printL(node *head)
{
	node *p = head;
 
    cout<<"What u just put is:\n";
	while (p != NULL)
	{
		/* cout << "Single Linker data is " << p->data << endl; */
		cout << " " << p->data;
		p = p->next;
	}
	cout<<endl;
}
 
// 单链表插入
node *insert(node *head, char* num)
{
	node *p0, *p1, *p2;
	p1 = head;
 
	p2 = new node;
	p0 = new node; // 插入节点
	//p0->data = num;// 插入数据  char* 和 char[] 转换要用memcpy 计算大小和处理 \0问题; 或者 strcpy();
	//memcpy(p0->data, num, strlen(num));
	strcpy(p0->data, num);
 
	while (p0->data > p1->data && p1->next != NULL)
	{
		p2 = p1;
		p1 = p1->next;// p0,p1和p2位置: p2->p1->p0
	}
 
	if (p0->data <= p1->data)
	{
		if (p1 == head)
		{// 头部前段插入 p0和p1位置: p0->p1->...
			head = p0;
			p0->next = p1;
		}
		else
		{// 插入中间节点 p0,p1和p2位置: p2-> p0 -> p1
			p2->next = p0;
			p0->next = p1;
		}
	}
	else
	{   // 尾部插入节点 p0,p1和p2位置: p2->p1->p0->NULL
		p1->next = p0;
		p0->next = NULL;
	}
	return head;
}
 
// 单链表删除
node *del(node *head, char num[])
{
	node *p1, *p2;
	p2 = new node;
	p1 = head;
	p2 = p1;
 
	while (strcmp(num, p1->data) != 0 && p1->next != NULL) //num != p1->data 
	{
		p2 = p1;
		p1 = p1->next;// p1和p2位置: p2->p1		
	}
 
	if (strcmp(num,p1->data) == 0)
	{
		if (p1 == head)// 删除头节点
		{
			head = p1->next;
			delete p1;
		}
		else
		{
			p2->next = p1->next;
			delete p1;
		}
	}
	else
	{
		cout << num << " could not been found in the current single linker!" << endl;
	}


	return head;
}
 
//=============插入排序====================
node *insertSort( node *head )
{
	node  *p1, *prep1, *p2, *prep2, *temp;
	prep1 = head->next;
	p1 = prep1->next;
	//prep1和p1是否需要手动后移
	bool flag;
 
	while (p1 != NULL)
	{
		flag = true;
		temp = p1;
		//由于是单向链表,所以只能从头部开始检索
		for (prep2 = head, p2 = head->next; p2 != p1; prep2 = prep2->next, p2 = p2->next)
		{
			//发现第一个较大值
			if (strcmp(p2->data, p1->data)) //(p2->data > p1->data)
			{
				p1 = p1->next;
				prep1->next = p1;
				prep2->next = temp;
				temp->next = p2;
				flag = false;
				break;
			}
		}
		//手动后移prep1和p1
		if (flag)
		{
			prep1 = prep1->next;
			p1 = p1->next;
		}
	}
	return head;
}

int main(){
	//推荐样例: 
	//wo ai de ren bu ai wo: 删除 bu (我爱的人不爱我 =》 我爱的人爱我)
	//wo ai wo de zhu guo 删除 guo (我爱我的祖国 => 我爱我的猪)
    cout<<"*** create link*** input \'stop\' to end***"<<endl;
    node *head = creat();
    cout << endl;

    cout << " ************length is: ************"<<'\n'<< length(head)<<endl;

    cout<<" ************ They are : **************"<<endl;
    printL(head);

    cout<<"Insert new node -Really\?- "<<endl;

    head = insert(head, "Really\?");
    printL(head);

	getchar();


    cout<<"delete link"<<endl;
	char arr[20];
	scanf("%s", arr);
    head = del(head, arr);
    printL(head); 

	getchar();
	getchar();

/*     cout<<"Sort link"<<endl;
     head = insertSort(head); //node *sortHead
    //printL(sortHead);
    printL(head); */

    return 0;

}


小结

技术太多,不可能什么都懂。有时候可以“不求甚解”一些。毕竟不是人人都有底层写驱动、过代码的需求的,更多的越来越前台、越来越糙猛快节奏。所以,快看,刷过也是一种值得的学习路线。当然需要投入和探究,放低编程能力的代价,求得是广博程度,应该有“每有会意,便欣然忘食”的下面境界才可。

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值