浙江大学数据结构陈越老师视频课程
链表代码整理
- 链表结构头文件
//structLinkList.h
#include <iostream>
typedef struct LNode *List;
struct LNode{
double Data;
List Next;
};
//list length
int Length(List PtrL)
{
List p=PtrL;
int j=0;
while(p){
p=p->Next;
j++;
}
return j;
}
//find kth element by its sequence number
List FindKth(int K, List PtrL)
{
List p=PtrL;
int i=1;
while(p!=NULL && i<K){
p=p->Next;
i++;
}
if(i==K) return p;
else return NULL;
}
//find element by its value
int Find(double X,List PtrL, List rstL)
{
List p=PtrL;
int pos=1;
while(p!=NULL&&p->Data!=X)
{
p=p->Next;
pos++;
}
return pos;
}
//insert new element at position i
List Insert(double X, int i, List &PtrL)
{
List p,s;
if(i==1){
s=(List)malloc(sizeof(struct LNode));
s->Data=X;
PtrL=s;
return PtrL;
}
p=FindKth(i-1,PtrL);
if(p==NULL){
printf("parameter i wrong\n");
return NULL;
}
else{
s=(List)malloc(sizeof(struct LNode));
s->Data=X;
s->Next=p->Next;
p->Next=s;
return PtrL;
}
}
//delete element at position i
List Delete(int i, List &PtrL)
{
List p,s;
if(i==1){
s=PtrL;
if(PtrL!=NULL) PtrL=PtrL->Next;
else return NULL;
free(s);
return PtrL;
}
p=FindKth(i-1,PtrL);
if(p==NULL){
printf("the %d element does not exist.\n",i-1);
return NULL;
}else if(p->Next==NULL){
printf("the %d element does not exist.\n",i);
return NULL;
}else{
s=p->Next;
p->Next=s->Next;
free(s);
return PtrL;
}
}
- 主函数,调用链表结构头文件,实现链表一系列具体操作。
//main.cpp
#include "structLinkList.h"
using namespace std;
int main()
{
List mylist=(List)malloc(sizeof(struct LNode));
List tmpList;
int i;
cout<<"Insert elements into the list."<<endl;
for(i=1;i<10;i++)
{
double vluEle=3.0*i;
cout<<"Insert "<<vluEle<<" into the list."<<endl;
Insert(vluEle,i,mylist);
}
cout<<endl;
int lgtList = Length(mylist);
cout<<"Length of the list is : "<<lgtList<<endl;
cout<<"After insertation:"<<endl;
for(i=1;i<=lgtList;i++)
{
tmpList=FindKth(i,mylist);
cout<<"Position "<<i<<" of list is "<<tmpList->Data<<endl;
}
cout<<endl;
cout<<"Find member by its value"<<endl;
int position=0;
position=Find(7,mylist,tmpList);
if(position>lgtList){
cout<<"There is no element has value "<<7<<endl;
}else{
cout<<"Position of value "<<7<<" in the list is: "<<position<<endl;
}
position=Find(6,mylist,tmpList);
if(position>lgtList){
cout<<"There is no element has value "<<6<<endl;
}else{
cout<<"Position of value "<<6<<" in the list is: "<<position<<endl;
}
cout<<"Find member by its sequence number"<<endl;
tmpList=FindKth(5,mylist);
if(tmpList==NULL){
cout<<"Element at position "<<5<<" does not exist."<<endl;
}else{
cout<<"Value of position "<<5<<" is: "<<tmpList->Data<<endl;
}
tmpList=FindKth(9,mylist);
if(tmpList==NULL){
cout<<"Element at position "<<9<<" does not exist."<<endl;
}else{
cout<<"Value at position "<<9<<" is : "<<tmpList->Data<<endl;
}
cout<<"Delete element at position "<<4<<endl;
Delete(4,mylist);
lgtList=Length(mylist);
cout<<"Length of the list after deletion is : "<<lgtList<<endl;
cout<<endl;
cout<<"After deletion:"<<endl;
for(i=1;i<=lgtList;i++)
{
tmpList=FindKth(i,mylist);
cout<<"Position "<<i<<" of list is "<<tmpList->Data<<endl;
}
return 0;
}
- 结果: