链表

单链表的基本操作

/*
单链表的基本操作
*/

#include
   
   
    
    
#include
    
    
     
     
using namespace std;

/*
typedef int ElemType;
struct LNode
{
	ElemType data;
	LNode* next;
};
*/

//初始化一个链表
void InitList(LNode *&LN)
{
	LN = NULL;
}

// 删除链表中的所有节点
void ClearList(LNode *&LN)
{
	LNode *cp, *np;
	cp = LN;
	while(cp!=NULL)
	{
		np = cp->next;
		delete cp;
		cp = np;
	}
	LN = NULL;
}

//得到链表的长度
int ListSize(LNode *&LN)
{
	LNode *p = LN;
	int i = 1;
	while(p!=NULL)
	{
		p = p->next;
		i++;
	}
	return i-1;
}


//检查链表是否为空
int ListEmpty(LNode *&NL)
{
	return(NL == NULL);
}

//得到链表第pos个节点中的元素
ElemType GetElem(LNode *&NL, int pos)
{
	if(pos < 1)
	{
		cerr<<"pos is out range!"<
     
     
      
      next;
	}
	if(p != NULL)
	{
		return p->data;
	}
	else
	{
		cout<<"pos is not in this link"<
      
      
       
       data<<" ";
		p = p->next;
	}
	cout<
       
       
         data == item) { break; } else { p = p->next; i++; } } return i; } //更新链表中具有给定值的一个元素 int UpdataList(LNode *&NL, const ElemType &item, const ElemType &updata) { LNode *p = NL; while(p != NULL) { if(p->data == item) { break; } else { p = p->next; } } if(p == NULL) { cout<<"原链表中没有输入的元素!"; return 0; } else { p->data = updata; return 1; } } //向单链表中添加一个元素 void InsertRear(LNode *&NL, const ElemType &item) { LNode *newptr; newptr = new LNode; //动态申请结点 if(newptr == NULL) { cerr<<"Memery allocation failure!"< 
        
          data = item; newptr->next = NULL; if(NL == NULL) { NL = newptr; //无表头 } else { LNode *p = NL; // 遍历链表直到链表尾 while(p->next != NULL) { p = p->next; } p->next = newptr; } } //向单链表的表头插入一个元素 void InsertFront(LNode *&NL, const ElemType &item) { LNode *newptr; newptr = new LNode; if(newptr == NULL) { cerr << "Memory allocation failure!"< 
         
           data = item; newptr->next = NL; NL = newptr; } //向单链表中满足条件的位置插入一个元素 void Insert(LNode *&NL, const ElemType &item) { LNode *newptr; newptr = new LNode; if(newptr == NULL) { cerr<<"Memory allocation failure!"< 
          
            data = item; LNode *cp; //指向当前结点的指针 LNode *ap; //指向前一个结点的指针 cp = NL; ap = NULL; while(cp != NULL) { if(item<=cp->data) { break; } else { ap = cp; cp = cp->next; } } if(ap == NULL) { newptr->next = NL; NL = newptr; } else { newptr->next = cp; ap->next = newptr; } } //从链表中删除头元素 ElemType DeleteType(LNode *&NL) { if(NL == NULL) { cerr<<"链表中没有元素!"< 
           
             next; ElemType temp = p->data; delete p; return temp; //返回被删除元素的值 } //从链表中删除等于给定值的元素 int Delete(LNode *&NL, const ElemType &item) { if(NL == NULL) { cerr<<"链表为空!"< 
            
              data == item) { break; } else { ap = cp; cp = cp->next; } } if(cp == NULL) { cerr<<"元素不存在"< 
             
               next; } else { ap->next = cp->next; delete cp; } return 1; } 
              
             
            
           
          
         
       
      
      
     
     
    
    
   
   

测试链表的基本操作:

/*
数组排序
*/

#include
   
   
    
    
#include
    
    
     
     
using namespace std;

typedef int ElemType;
struct LNode
{
	ElemType data;
	LNode* next;
};

#include "SList.h"

void main()
{
	LNode *head;
	InitList(head); //初始化链表
	int i, num;
	int a[] = {3, 4, 2, 9, 8, 7, 10,6, 5,1};
	for(i = 0; i < 10; i++)
	{
		num = a[i];
		InsertRear(head,num);
	}
	TraverseList(head);   //遍历链表 
	
	//删除元素
	cout<<"输入一个要删除的整数:";
	cin>>num;
	if(Delete(head,num))
	{
		TraverseList(head);   //遍历链表 
	}
	cout<<"删除表头元素!"<
     
     
      
      >num;
	InsertFront(head, num);
	TraverseList(head);
	
	cout<<"在链表中满足条件的位置插入一个元素:";
	cin>>num;
	Insert(head,num);  //在链表中满足条件的位置插入一个元素
	TraverseList(head);
	
	//更新元素
	cout<<"更新链表中具有给定值的第一个元素";
	cout<
      
      
       
       >num;
	cout<<"输入更新值:";
	cin>>data;
	UpdataList(head,num,data);
	TraverseList(head);
	
	//查找元素
	cout<<"从链表中找出一个给定值的元素。"<
       
       
         >num; int key; key=Find(head, num); cout< 
        
          < 
         
           >pos; int type; //返回输出的元素 type=GetElem(head,pos); cout< 
          
            < 
            
           
          
         
       
      
      
     
     
    
    
   
   


以上定义了链表的基本操作

《数据结构课程笔记》

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值