单链表复习

原创 2015年07月07日 13:33:34

单链表复习


作者:vpoet

mails:18200268879@163.com

注:转载请注明出处,谢谢合作


#include <iostream>
#include <stack>
using namespace std;

typedef struct ListNode
{
	int data;
	struct ListNode* next;
}NODE;

NODE *CreateList()
{
	NODE * head,*p,*s;

	head=(NODE*)malloc(sizeof(NODE));
	p=head;

	int LinkData;
	int InputIndex=1;
	while(InputIndex)
	{
		cout<<"Please input the Node data.(if 0 is inputed,CreateLink Over!): ";
		cin>>LinkData;
		if(0!=LinkData)
		{
			s=(NODE*)malloc(sizeof(NODE));
			s->data=LinkData;
			p->next=s;
			p=s;
		}
		else
		{
			InputIndex=0;
		}
	}

	head=head->next;
	p->next=NULL;

	return head;
}

void Print(NODE *head)
{
	NODE *p=head;
	cout<<"The LinkList is: ";
	while(p!=NULL)
	{
		cout<<p->data<<"  ";
		p=p->next;
	}
}

void ListLength(NODE*head)
{
	
	NODE* p=head;
	int count =0;
	while(p!=NULL)
	{
		count++;
		p=p->next;
	}
	cout<<"The length is: "<<count<<endl;
}

void InsertNode(NODE* head)
{
	NODE *p=head;
	
	while(p->next!=NULL)
	{
		p=p->next;
	}

	NODE *New;
	New=(NODE*)malloc(sizeof(NODE));
	cout<<"Please input the new data: ";
	cin>>New->data;
	p->next=New;
	New->next=NULL;
	
	head=p;
}

void ReversePrint(NODE* head)
{
	stack <NODE*> StackNode;
	NODE *p=head;
	while(p!=NULL)
	{
		StackNode.push(p);
		p=p->next;
	}
	cout<<"Reverse to Print: ";
	while(!StackNode.empty())
	{
		NODE* temp;
		temp=StackNode.top();
		StackNode.pop();
		cout<<temp->data<<" ";
	}
}

void DeleteNode(NODE *head,int DelValue)
{
	NODE *p=head;
	
	while(p!=NULL)
	{
		if(p->next->data==DelValue)
		{
			p->next=p->next->next;
			break;
		}
		else
		{
			p=p->next;
		}
	}
	head=p;
}


int main()
{
	cout<<"Create New LinkList....\n"<<endl;
	NODE *p=CreateList();
	cout<<"Print the LinkList.....\n"<<endl;
	Print(p);
	cout<<"Print the length of LinkList...\n"<<endl;
	ListLength(p);

	cout<<"Insert a New LinkNode....\n"<<endl;
	InsertNode(p);
	cout<<"Cout The New LinkNode....\n"<<endl;
	Print(p);

	cout<<"Reverse to input the LinkList...\n"<<endl;
	ReversePrint(p);

	cout<<"Delete a Node in LinkList....\n"<<endl;
	int DelValue;
	cout<<"Please input the Delete Node Value"<<endl;
	cin>>DelValue;
	DeleteNode(p,DelValue);
	cout<<"Print the del Node LinkList....\n"<<endl;
	Print(p);
	cout<<endl;
	return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

数据结构复习--java实现单链表基本操作

单链表的基本操作包括建立单链表、查找运算(按序查找和按值查找)、插入运算(前插和后插)和删除运算。下面给出具体的java实现程序: package com.zpp.test; //首先创建一个节点类 ...

一步一步复习数据结构和算法基础-单链表冒泡排序

其实个人以为用单链表排序就是自己找麻烦,用数组排序多好啊, 但是转念一想,还是挑战一下自己吧,这个程序的声明和前几次的不同,但是仅仅是换汤不换药. 谨以此作为链表的练习吧. #include #...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)