⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpp1.cpp

📁 简单链表的建立、删除节点、插入节点、倒序、计算链表的长度
💻 CPP
字号:
#include  "iostream"
#include  "stdio.h"
#include "string.h"
#include "conio.h"
using namespace std;
typedef struct student
{
	int data;
	struct student *next;
}node;
node *creat()
{
	node *head,*p,*s;
	int x,c=1;
	head=(node *)malloc(sizeof(node));
	p=head;
	while(c)
	{
		
		cout<<"\n intput the data:";
		cin>>x;
		if(x!=0)
		{
			s=(node *)malloc(sizeof(node));
		    s->data=x;
		
		    cout<<"  "<<s->data;
	  	    p->next=s;
		    p=s;
		}
	    else c=0;
	}
	head =head->next;
	p->next=NULL;

  //  cout<<"\n  "<<head->data;
    return(head);

}
int length(node *head)
{
	int n=0;
	node *p;
	p=head;
	while(p!=NULL)
	{
		p=p->next;
		n++;
	}
	return (n);
}
void print(node *head)
{
	node *p ;
	int n;
	n=length(head);
	cout<<" \nrecords are "<<n<<"\n";
	p=head;
	if(head!=NULL)
		while(p!=NULL)
		{
			cout<<"  "<<p->data;
			p=p->next;
		}
}
node *del(node *head,int num)
{
	node *p1,*p2;
	p1=head;
	while(num!=p1->data && p1->next!=NULL)
	{
		p2=p1;
		p1=p1->next;
	}
	if(num==p1->data)
	{
		if(p1==head)
		{
			head=p1->next;
			free(p1);
		}
		else
			p2->next=p1->next;
	}
	else
		cout<<num<<"could not been foun";
	return(head);
}
node *insert(node *head,int num)
{
	node *p0,*p1,*p2;
	p1=head;
	p0=(node *)malloc(sizeof(node));
	p0->data=num;
	while(p0->data>p1->data && p1->next!=NULL)
	{
		p2=p1;p1=p1->next;
	}
	if(p0->data<=p1->data)
	{
		if(head==p1)
		{
			p0->next=p1;
			head=p0;
		}
		else
		{
			p2->next=p0;
			p0->next=p1;
		}
	}
	else
	{
		p1->next=p0;
		p0->next=NULL;
	}
	return(head);
}
node *reverse(node *head)
{
	node *p1,*p2,*p3;
	if(head==NULL || head->next==NULL)
		return head;
	p1=head;p2=p1->next;
	while(p2)
	{
		p3=p2->next;
		p2->next=p1;
		p1=p2;
		p2=p3;
	}
	head->next=NULL;
	head=p1;
	return(head);
}


void main()
{

	node *p1,*p2;
	p1=creat();
	print(p1); 
//del	p2=del(p1,3);
//insert	p2=insert(p1,5);
	p2=reverse(p1);
	print(p2);




//    print(creat());
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -