带头节点的单链表

//带头结点的单链表 
#include 
#include 
using namespace std;
typedef int ElemType;

typedef struct Node
{
	ElemType data;
	struct Node *next;
}LNode,*LinkList; 

void InitList(LinkList *L);
void Create_head(LinkList l);
void Create_rear(LinkList l);
void Output(LinkList l);
void ReverseList(LinkList l);
LNode *Pos(LinkList l,int i);
void Insert_Order(LinkList &l,int x);
LinkList MergeList(LinkList La,LinkList Lb);
int ListLength(LinkList l);
void SortList(LinkList l);

void ShowMenu()
{
    cout<<"——————————————————————————————"<>n;

        switch(n)
        {
            case 1:
                {
                    InitList(&l);
                    break;
                }
            case 2:
                {
                   Create_head(l);
                   break; 
                }
            case 3:{
                Create_rear(l);
                break;
            }
            case 4:{
                Output(l);
                break;
            }
            case 5:{
                ReverseList(l);
                break;
            }
            case 6:{
                cout<<"请输入你要查找的位置:";
                int m;
                cin>>m;
                LNode *p = Pos(l,m);
                if(p)
                cout<<"该位置的值为:"<data<>x;
                Insert_Order(l,x);
                cout<<"插入后输出该单链表:";     
                Output(l);
                break;
            }
            case 8:{
                cout<<"该单链表长度为:"<next = NULL;
}

void Create_head(LinkList l)
{
	int m ;
	while(1)
	{
		cin>>m;
		if(m==-1)
			break;
		LNode *s = new LNode;
		s->data = m;
		s->next = l->next;
		l->next = s;
	}
}

void Create_rear(LinkList l)
{

    cout<<"请依次输入数据,并以-1作为结束标记:\n";
	int m;
	LNode *p = l;
	while(1)
	{
		cin>>m;
		if(m==-1)
			break;
		LNode *s = new LNode;
		s->data = m;
		p->next = s;
		p = s;
	}
	p->next = NULL;
}

void ReverseList(LinkList l)
{
	LNode *p = l->next;		//p为原链表的当前处理节点
	l->next = NULL;			//逆置单链表初始为空
	LNode *q;		
	while(p)
	{
		q = p->next;		//q保存当前处理节点的下一节点
		p->next = l->next;
		l->next = p; 
		p = q;
	} 
}

/*
LNode *Pos(LinkList l,int i)
{
	if(i<=0)
		return NULL;
		
	LNode *p = l->next;
	int j = 1;
	while(jnext)
	{
		j++;
		p = p->next;				
	}
	if(i==j) return p;
	else	return NULL;
	
}
*/

LNode *Pos(LinkList l,int i)
{
	if(i<0)
		return NULL;
		
	LNode *p = l;
	while(i--)
	{
		p = p->next;
		if(p == NULL)
		{
			return NULL;
			break;
		}
	}
	return p;
	
}

//有序插入 
void Insert_Order(LinkList &l,int x)
{
	LNode *s = new LNode;
	LNode *p,*pre;
	s->data = x;
	s->next = NULL;
	if(l->next == NULL)
		l->next = s;
	if(x <= l->next->data)
	{
		s->next = l->next;
		l->next = s; 
	}
	else
	{
		p = l->next;
		while(p && x>p->data)
		{
			pre = p;
			p = p->next;
		}
		
		s->next = pre->next;
		pre->next = s;
	} 
}

LinkList MergeList(LinkList La,LinkList Lb)
{
	LNode *pa,*pb;
	LinkList Lc;
	pa = La->next;
	pb = Lb->next;
	
	Lc = La;
	Lc->next = NULL;
	LNode *r = Lc;
	
	while(pa && pb)
	{
		if(pa->data<=pb->data)
		{
			r->next = pa;
			r = pa;
			pa = pa->next;
		}
		else
		{
			r->next = pb;
			r = pb;
			pb = pb->next;
		}
	}
	
	if(pa)
		r->next = pa;
	else
		r->next = pb;
	
	delete Lb;
	
	return Lc;
}

int ListLength(LinkList l)
{
	LNode *p = l->next;
	int len = 0;
	while(p)
	{
		len++;
		p = p->next;
	}
	return len;
}

void SortList(LinkList l)
{
	LinkList p;
	int n,i,j;
	int temp;
	n = ListLength(l);
	if(n==0)
		return;
		
	p = l->next;
	for(i=1;inext;
		for(j=0;jdata > p->next->data)
			{
				temp = p->data;
				p->data = p->next->data;
				p->next->data = temp;
			}
			p = p->next;
		}
	}
}

void Output(LinkList l)
{
	LNode *p;
	p=l->next;
	while(p)
	{
		cout<data<<" ";
		p = p->next;
	}
	cout<<"\n";
}

你可能感兴趣的:(数据结构和算法基础)