📄 algo2-2.cpp
字号:
// algo2-2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
template <class T>
struct Node
{
T data;
Node<T> *next;
};
template <class T>
class LinkList
{
public:
LinkList(){head = new Node<T>; head->next = NULL;};
LinkList(T a[],int n);
void InitList();
void DestroyList();
int ListEmpty();
int ListLength();
void DispList();
int GetElem(int i,T &e);
int LocateElem(T e);
int ListInsert(int i,T e);
int ListDelete(int i,T &e);
private:
Node<T> *head;
};
template <class T>
LinkList<T>::LinkList(T a[],int n)
{ Node<T> *s;int i;
head=new Node;
/*创建头结点*/
head->next=NULL;
for (i=0;i<n;i++)
{ s=new Node<T>;
/*创建新结点*/
s->data=a[i]; s->next=head->next;
/*将*s插在原开始结点之前,头结点之后*/
head->next=s;
}
}
template <class T>
void LinkList<T>::InitList()
{
head=new Node<T>;
/*创建头结点*/
head->next=NULL;
}
template <class T>
void LinkList<T>::DestroyList()
{ Node<T> *p=head,*q=p->next;
while (q!=NULL)
{ delete(p);
p=q;q=p->next;
}
delete(p);
}
template <class T>
int LinkList<T>::ListEmpty()
{
return(head->next==NULL);
}
template <class T>
int LinkList<T>::ListLength()
{ Node<T> *p=head;int i=0;
while (p->next!=NULL)
{ i++;
p=p->next;
}
return(i);
}
template <class T>
void LinkList<T>::DispList()
{ Node<T> *p=head->next;
while (p!=NULL)
{ cout<<p->data<<' ';
p=p->next;
}
cout<<endl;
}
template <class T>
int LinkList<T>::GetElem(int i,T &e)
{ int j=0;
Node<T> *p=head;
while (j<i && p!=NULL)
{ j++;
p=p->next;
}
if (p==NULL) return 0; /*不存在第i个数据结点*/
else /*存在第i个数据结点*/
{ e=p->data;
return 1;
}
}
template <class T>
int LinkList<T>::LocateElem(T e)
{ Node<T> *p=head->next;int n=1;
while (p!=NULL && p->data!=e)
{ p=p->next; n++; }
if (p==NULL) return 0;
else return n;
}
template <class T>
int LinkList<T>::ListInsert(int i,T e)
{ int j=0;
Node<T> *p=head, *s;
while (j<i-1 && p!=NULL) /*查找第i-1个结点*/
{ j++;
p=p->next;
}
if (p==NULL) return 0; /*未找到位序为i-1的结点*/
else /*找到位序为i-1的结点*p*/
{ s=new Node<T>;
/*创建新结点*s*/
s->data=e;
s->next=p->next; /*将*s插入到*p之后*/
p->next=s;
return 1;
}
}
template <class T>
int LinkList<T>::ListDelete(int i,T &e)
{ int j=0;
Node<T> *p=head,*q;
while (j<i-1 && p!=NULL) /*查找第i-1个结点*/
{ j++;
p=p->next;
}
if (p==NULL) return 0; /*未找到位序为i-1的结点*/
else /*找到位序为i-1的结点*p*/
{ q=p->next; /*q指向要删除的结点*/
if (q==NULL) return 0;
/*若不存在第i个结点,返回0*/
p->next=q->next; /*从单链表中删除*q结点*/
delete(q); /*释放*q结点*/
return 1;
}
}
int main()
{
char X, Y;
LinkList<char> h;
cout<<"InitList"<<endl;
h.InitList();
cout<<"Insert a,b,c,d,e"<<endl;
h.ListInsert(1, 'a');
h.ListInsert(2, 'b');
h.ListInsert(3, 'c');
h.ListInsert(4, 'd');
h.ListInsert(5, 'e');
cout<<"DisplayList"<<endl;
h.DispList();
cout<<"ListLength is "<<h.ListLength()<<endl;
if(h.ListEmpty())cout<<"List Is Empty"<<endl;
else cout<<"List Isn't Empty"<<endl;
h.GetElem(2 ,X);
cout<<"Elem 2 Is "<<X<<endl;
cout<<"Elem 'a' is at place "<<h.LocateElem('a');
cout<<"Insert f"<<endl;
h.ListInsert(4, 'f');
cout<<"DisplayList"<<endl;
h.DispList();
cout<<"Delete Elem at place 3 is ";
h.ListDelete(3, Y);
cout<<X<<endl;
cout<<"DisplayList"<<endl;
h.DispList();
cout<<"DestroyList"<<endl;
h.DestroyList();
_getch();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -