单链表的基本操作
/*
单链表的基本操作
*/
#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<
<
以上定义了链表的基本操作
《数据结构课程笔记》