顺序表的链式储存

#ifndef __LINKLIST_H__
#define __LINKLIST_H__
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

typedef int DataType;
typedef struct LinkNode
{
	DataType data;
    struct LinkNode* next;
}LinkNode,*pLinkNode;

typedef struct LinkList
{
	pLinkNode pHead;
}LinkList,*pLinkList;

void PrintList(pLinkList pList);//打印函数 
void InitLinkList(pLinkList pList);//初始化函数 
void PushBack(pLinkList pList,DataType x);//尾插 
void PopBack(pLinkList pList);//尾删 
void PushFront(pLinkList pList,DataType x);//头插 
void PopFront(pLinkList pList);//头删 
pLinkNode Create_Node(DataType x);//创建结点
pLinkNode Find_Node(DataType x);//找结点
void Insert(pLinkList pList,pLinkNode pos,DataType x);//指定为插入 
void Remove(pLinkList pList,DataType x);//指定数删除 
void RemoveAll(pLinkList pList,DataType x);//删除所有 
void BubbleSort(pLinkList pList);//排序 
void Erase(pLinkList pList,pLinkNode pos);//指定位删除
void Destroy(pLinkList pList);//销毁
#endif //__LINKLIST_H__


void PrintList(pLinkList pList)//打印
{
	assert(pList);
    pLinkNode cur=NULL;
	cur=pList->pHead;
	while(cur!=NULL)
	{
		printf("%d->",cur->data);
		cur=cur->next;
	}
    printf("NULL\n");
}
void InitLinkList(pLinkList pList)//初始化
{
	assert(pList);
	pList->pHead=NULL;
}
pLinkNode Create_Node(DataType x)//创建新结点
{
    pLinkNode newNode=(pLinkNode)malloc(sizeof(pLinkNode));
	if(NULL==newNode)
	{
		printf("out of memory\n");
		exit(EXIT_FAILURE);
	}
	newNode->data=x;
	newNode->next=NULL;
	return newNode;
}
void Destroy(pLinkList pList)//释放
{
    assert(pList);
	pLinkNode cur=NULL;
	cur=pList->pHead;
	while(cur)//?
	{
      pLinkNode tmp=cur;
      cur=cur->next;
	  free(tmp);
	  tmp=NULL;
	}
	pList->pHead=NULL;
		
}
void PushBack(pLinkList pList,DataType x)//尾插
{
    assert(pList);
    pLinkNode cur=NULL;
    pLinkNode newNode=NULL;
	cur=pList->pHead;
	newNode=Create_Node(x);
    if(cur=NULL)
	{
       pList->pHead=newNode;
	}
	else
	{
        while(cur->next)//?
		{
			cur=cur->next;
		}
		cur->next=newNode;
	}
}
void PopBack(pLinkList pList)//尾删 
{
   assert(pList);
   pLinkNode cur=NULL;
   pLinkNode tmp=NULL;
   cur=pList->pHead;
   if(pList->pHead==NULL)
   { 
	   return ;
   }
   else if(pList->pHead->next==NULL)
   {
      free(pList->pHead);
      pList->pHead=NULL;
   }
   else 
   {
	   while(cur->next)//?
	   {
		   tmp=cur;
		   cur=cur->next;
	   }
	   free(cur);
	   cur=NULL;
	   tmp->next=NULL;
   }
}
void PushFront(pLinkList pList,DataType x)//头插 
{
	assert(pList);
    pLinkNode newNode=NULL;
	newNode=Create_Node(x);
	newNode=pList->pHead;
    pList->pHead=newNode;
}
void PopFront(pLinkList pList)//头删
{
    pLinkNode tmp=NULL;
	if(pList->pHead==NULL)
	{
		return;
	}
	else
	{
		tmp=pList->pHead;
        pList->pHead=tmp->next;
		free(tmp);
		tmp=NULL;
	}
}
void Insert(pLinkList pList,pLinkNode pos,DataType x)//指定位后插入元素
{   
	assert(pList);
	pLinkNode newNode=Create_Node(x);
    pLinkNode cur=pList->pHead;
	if(pList->pHead->next==NULL)
	{
      PushFront(pList,x);
	  return;
	}
	else if(pos==NULL)
	{
		printf("no position\n");
	}
	else
	{
		while(cur!=pos)
		{
			cur=cur->next;
		}
        newNode->next=pos->next;
		pos->next->newNode;
	}

}
pLinkNode Find_Node(pLinkList pList,DataType x)//找结点
{
	assert(pList);
    pLinkNode  cur=pLinkList->pHead;
	while(cur)
	{
		if(cur->data==x)
		{
			return cur;
		}
		cur=cur->next;
	}
	return NULL;

}
void Remove(pLinkList pList,DataType x)//指定数删除 
{
    assert(pList);
	pLinkNode cur=pList->pHead;
    pLinkNode ret=NULL;
	if(pList->pHead==NULL)
	{
		return ;
	}
	else
	{
		ret=Find_Node(pList,x);
		while(cur->next!=ret)
		{
			cur=cur->next;
		}
		cur->next=ret->next;
		free(ret);
		ret=NULL;
	}

}
void RemoveAll(pLinkList pList,DataType x)//删除所有指定数
{
    pLinkNode ret=NULL;  
    pLinkNode cur=plist->phead;  
    assert(plist);  
    if(NULL == plist->phead)  
    {  
        return ;  
    }  
    else  
    {  
        while(cur->next != NULL)  
        {  
            ret=Find_NUM(plist,x);  
            while(cur->next != ret)  
            {  
                cur=cur++;  
            }  
            cur->next=ret->next;  
            free(ret);  
            ret=NULL;  
            cur=cur->next;  
        }  
    }  
}
void BubbleSort(pLinkList pList)//排序 
{

	pLinkNode cur=plist->pHead;  
    pLinkNode cmp=plist->pHead;  
    DataType flag=0;  
    while(cur->next != NULL)  
    {  
        flag=0;  
        while(cmp->next != NULL)  
        {  
            if(cmp->data > cmp->next->data)   //升序排列  
            {  
                DataType tmp=cmp->data;  
                cmp->data=cmp->next->data;  
                cmp->next->data=tmp;  
                flag=1;  
            }  
            cmp=cmp->next;  
        }  
        cur=cur->next;  
        if(flag == 0)  
            break;  
    }  
}
void Erase(pLinkList pList,pLinkNode pos)//指定位删除
{

	pLinkNode cur=plist->pHead;                                              
    assert(plist);  
    while(cur->next != pos)  
    {  
        cur=cur->next;  
    }  
    cur->next=pos->next;  
    free(pos);  
    pos=NULL;  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值