///线性表学习总结
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef int ElemType;
typedef struct node
{
node *next;
ElemType data;
int length;
}node;
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
typedef int ElemType;
typedef struct node
{
node *next;
ElemType data;
int length;
}node;
///初始化
void InitList(node *L)
{
L->data=0;
L->length=0;
L->next=NULL;
}
void InitList(node *L)
{
L->data=0;
L->length=0;
L->next=NULL;
}
///新增节点 - 尾插法
void AddList(node *L,int n)
{
L->length+=n;
srand(time(0));
for(int i=0;i<n;i++)
{
while(L->next!=NULL)
{
L=L->next;
}
node *c=(node*)malloc(sizeof(node));
L->next=c;
c->next=NULL;
c->data=rand()%100+1;
}
}
void AddList(node *L,int n)
{
L->length+=n;
srand(time(0));
for(int i=0;i<n;i++)
{
while(L->next!=NULL)
{
L=L->next;
}
node *c=(node*)malloc(sizeof(node));
L->next=c;
c->next=NULL;
c->data=rand()%100+1;
}
}
///头插法
void AddListHead(node *L,int n)
{
L->length+=n;
for(int i=0;i<n;i++)
{
node *c=(node*)malloc(sizeof(node));
c->next=L;
}
}
///插入节点
void InsertList(node *L,int i,int x)
{
if(i>L->length || i<0)
{
cout<<"ERROR!\n";
exit(0);
}
L->length++;
int j=0;
while(L->next!=NULL && j<i)
{
L=L->next;
j++;
}
node *c=(node*)malloc(sizeof(node));
c->next=L->next;
L->next=c;
c->data=x;
}
///删除节点
void DeleteList(node *L,int i)
{
if(i<0 || i>L->length)
{
cout<<"ERROR!\n";
exit(0);
}
L->length--;
int j=0;
while(L->next!=NULL && j<i-1)
{
L=L->next;
j++;
}
node *c=L->next;
L->next=c->next;
free(c);
}
void AddListHead(node *L,int n)
{
L->length+=n;
for(int i=0;i<n;i++)
{
node *c=(node*)malloc(sizeof(node));
c->next=L;
}
}
///插入节点
void InsertList(node *L,int i,int x)
{
if(i>L->length || i<0)
{
cout<<"ERROR!\n";
exit(0);
}
L->length++;
int j=0;
while(L->next!=NULL && j<i)
{
L=L->next;
j++;
}
node *c=(node*)malloc(sizeof(node));
c->next=L->next;
L->next=c;
c->data=x;
}
///删除节点
void DeleteList(node *L,int i)
{
if(i<0 || i>L->length)
{
cout<<"ERROR!\n";
exit(0);
}
L->length--;
int j=0;
while(L->next!=NULL && j<i-1)
{
L=L->next;
j++;
}
node *c=L->next;
L->next=c->next;
free(c);
}
///获得某一位置节点
node* GetList(node *L,int i)
{
if(i<0 || i>L->length)
{
cout<<"ERROR!\n";
exit(0);
}
int j=0;
while(L->next!=NULL&&j<i)
{
L=L->next;
j++;
}
return L;
}
node* GetList(node *L,int i)
{
if(i<0 || i>L->length)
{
cout<<"ERROR!\n";
exit(0);
}
int j=0;
while(L->next!=NULL&&j<i)
{
L=L->next;
j++;
}
return L;
}
///输出链表
void PrintList(node *L)
{
cout<<"the length of List is : "<<L->length<<endl;
while(L->next!=NULL)
{
cout<<L->data<<"->";
L=L->next;
}
cout<<L->data<<endl;
}
void PrintList(node *L)
{
cout<<"the length of List is : "<<L->length<<endl;
while(L->next!=NULL)
{
cout<<L->data<<"->";
L=L->next;
}
cout<<L->data<<endl;
}
///清空节点
void ClearList(node *L)
{
node *p,*q;
p=L->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
L->next=NULL;
L->length=0;
}
void ClearList(node *L)
{
node *p,*q;
p=L->next;
while(p)
{
q=p->next;
free(p);
p=q;
}
L->next=NULL;
L->length=0;
}
int main()
{
node *head=(node*)malloc(sizeof(node));
cout<<"Initalize:\n";
InitList(head);
PrintList(head);
cout<<"Add 10 :\n";
AddList(head,10);
PrintList(head);
cout<<"Insert number 3 ,1111:\n";
InsertList(head,3,1111);
PrintList(head);
node *c=GetList(head,4);
cout<<"Get numeber "<<4<<":\n";
cout<<c->data<<" "<<c->next<<endl;
cout<<"Delete number 4:\n";
DeleteList(head,4);
PrintList(head);
ClearList(head);
cout<<"Clear List :\n";
PrintList(head);
return 0;
}
{
node *head=(node*)malloc(sizeof(node));
cout<<"Initalize:\n";
InitList(head);
PrintList(head);
cout<<"Add 10 :\n";
AddList(head,10);
PrintList(head);
cout<<"Insert number 3 ,1111:\n";
InsertList(head,3,1111);
PrintList(head);
node *c=GetList(head,4);
cout<<"Get numeber "<<4<<":\n";
cout<<c->data<<" "<<c->next<<endl;
cout<<"Delete number 4:\n";
DeleteList(head,4);
PrintList(head);
ClearList(head);
cout<<"Clear List :\n";
PrintList(head);
return 0;
}