顺序表
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=5005;
typedef struct
{
int *date;
int length;
int size;
}List;
// 创建链表
void creat(List *p,int n)
{
p->date=(int *)malloc(N*sizeof(int));
p->size=n;p->length=0;
for(int i=0;i<p->size;i++)
{
scanf("%d",&p->date[i]);
p->length++;
}
}
// 逆置
void reverse(List *p)
{
for(int i=0;i<=p->length/2;i++)
{
int t=p->date[i];p->date[i]=p->date[p->length-i-1];p->date[p->length-i-1]=t;
}
}
// 判断链表是否为空
int empty(List *p)
{
if(p->size==0)return 1;return 0;
}
// 查找元素下标位置
int find(List *p,int val)
{
int *arr=p->date;int i=0;
while(i<p->length&&(*arr++)!=val)i++;
if(i<p->length)return i+1;return -1;
}
// 插入元素
void insert(List *p,int pos,int val)
{
if(pos>p->size){puts("容量达到上限");return;}
int *q=&p->date[p->length-1];
for(;q>=&p->date[pos-1];q--)*(q+1)=*q;
p->date[pos-1]=val;
p->length++;p->size++;
}
// 删除位置为pos的元素
void delet(List *p,int pos)
{
if(pos>p->length||pos<1){puts("操作错误");return;}
for(int *q=&p->date[pos-1];q<&p->date[p->length-1];q++)*q=*(q+1);
p->length--;p->size--;
}
// 合并两个链表
void union_list(List *p,List *q)
{
int *i=&p->date[p->length-1],*j=&q->date[0];
p->length+=q->length;
p->size+=q->size;
for(i++;i<=&p->date[p->length-1];i++)
*i=*(j++);
}
// 合并两个有序链表
List merge_list(List *p,List *q)
{
List ans;
ans.date=(int *)malloc(N*sizeof(int));
int *k=ans.date;
int *i=&p->date[0],*j=&q->date[0];
while(i<=&p->date[p->length-1]&&j<=&q->date[q->length-1])
{
if(*i<=*j)*(k++)=*(i++);
else *(k++)=*(j++);
}
while(i<=&p->date[p->length-1])*(k++)=*(i++);
while(j<=&q->date[q->length-1])*(k++)=*(j++);
ans.length=ans.size=p->length+q->length;
return ans;
}
// 链表的排序
void sort(List *p)
{
for(int i=0;i<p->length-1;i++)
{
for(int j=0;j<p->length-i-1;j++)
{
if(p->date[j]>p->date[j+1])
{
int t=p->date[j];p->date[j]=p->date[j+1];p->date[j+1]=t;
}
}
}
}
//输出链表
void output(List *p)
{
for(int i=0;i<p->length;printf("%d ", p->date[i]),i++);puts("");
}
int main(int argc, char const *argv[])
{
}
单链表
#include<cstdio>
#include<iostream>
#include<iomanip>
#include<malloc.h>
#include<algorithm>
using namespace std;
struct list
{
int data;
list *next;
};
list *creat(int len)
{
list *head,*node,*tail;
head=(list *)malloc(sizeof(list));
tail=head;
for(int i=0;i<len;i++)
{
node=(list *)malloc(sizeof(list));
scanf("%d",&node->data);
tail->next=node;
tail=node;
}
tail->next=NULL;
return head;
}
int length(list *head)
{
int len=0;
for(list *p=head;p->next!=NULL;p=p->next,len++);return len;
}
list *find(list *p,int key)
{
list *q;
if(p==NULL)return NULL;
for(q=p;q->next!=NULL and q->data!=key;q=q->next);
if(q->data==key)return q;return NULL;
}
list *insert_head(list *p,int data,int key)
{
list *node;
node=(list *)malloc(sizeof(list));
node->data=data;node->next=NULL;
list *q;
for(q=p;q->next!=NULL and q->next->data!=key;q=q->next);
if(q==NULL){puts("no find");return p;}
if(p->data==key)node->next=p,p=node;
else
{
node->next=q->next;
q->next=node;
}
return p;
}
list *insert_tail(list *p,int data,int key)
{
list *node;
node=(list *)malloc(sizeof(list));
node->data=data;node->next=NULL;
list *q=find(p,key);
if(q==NULL){puts("no find");return p;}
if(p->next==NULL) p->next=node;
else
{
node->next=q->next;
q->next=node;
}
return p;
}
list *delet(list *p,int key)
{
if(p->next==NULL)return p;
list *q=p;
list *temp=NULL;
if(p->data==key)
{
p=p->next;
free(q);q=NULL;return p;
}
for(q=p;q->next!=NULL and q->next->data!=key;q=q->next);
if(q->next==NULL){puts("no find");return p;}
temp=q->next;
q->next=temp->next;
free(temp);temp=NULL;
return p;
}
list *reverse(list *p)
{
list *q=NULL,*temp=NULL;
if(p==NULL){puts("the list is empty");return p;}
while(p!=NULL)
{
temp=p;p=p->next;
temp->next=q;q=temp;
}return q;
}
list *sort1(list *p)
{
for(list *i=p;i;i=i->next)
{
for(list *j=i->next;j;j=j->next)
{
if(i->data>j->data)
{
list temp=*i;*i=*j;*j=temp;
list *t=i->next;i->next=j->next;j->next=t;
}
}
}return p;
}
list *sort2(list *head)
{
list *p=head;
int n=length(head);
for(int i=1;i<n;i++)
{
p=head;
for(int j=0;j<n-i;j++)
{
if(p->data>p->next->data)
{
int temp=p->data;
p->data=p->next->data;
p->next->data=temp;
}p=p->next;
}
}return head;
}
void output(list *p)
{
for(list *q=p;q->next!=NULL;q=q->next,printf("%d%c",q->data," \n"[q->next==NULL]));
}
int main(int argc, char const *argv[])
{
list *p=creat(5);
cout<<length(p)<<endl;
list *q=sort1(p);
cout<<length(q)<<endl;
while(q->next!=NULL)printf("%d ",q->data),q=q->next;
}
十字链表
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define Status int
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef struct OLNode
{
int i,j,e;
struct OLNode *right,*down;
}OLNode,*OLink;
typedef struct
{
OLink *rhead,*chead;
int mu,nu,tu;
}CrossList;
Status CreateSMatrix(CrossList &M)
{
scanf("%d%d%d",&M.mu,&M.nu,&M.tu);
M.rhead=(OLink *)malloc((M.mu+1)*sizeof(OLink));
M.rhead=(OLink *)malloc((M.nu+1)*sizeof(OLink));
M.rhead=NULL;M.chead=NULL;
for(int i,j,e;i!=0;scanf("%d%d%d",&i,&j,&e))
{
OLink p;
p=(OLink)malloc(sizeof(OLNode));
p->i=i;p->j=j;p->e=e;
if(M.rhead[i]==NULL||M.rhead[i]->j>j)p->right=M.rhead[i],M.rhead[i]=p;
else
{
OLink q;
for(q=M.rhead[i];(q->right)&&q->right->j<j;q=q->right);
p->right=q->right;
q->right=p;
}
if(M.chead[j]==NULL||M.chead[j]->i>i)p->down=M.chead[j],M.chead[j]=p;
else
{
OLink q;
for(q=M.chead[j];(q->down)&&q->down->i<i;q=q->down);
p->down=q->down;
q->down=p;
}
}
}
双链表
#include<cstdio>
#include<iostream>
#include<iomanip>
#include<malloc.h>
#include<algorithm>
using namespace std;
struct list
{
int data;
list *pre,*next;
};
list *creat(int n)
{
list *head=(list *)malloc(sizeof(list));
list *p,*node;
p=head,p->pre=NULL;
for(int i=0;i<n;i++)
{
node=(list *)malloc(sizeof(list));
scanf("%d",&node->data);
p->next=node;
node->pre=p;p=node;
}
p->next=NULL;return head;
}
int length(list *p)
{
int n=0;
for(list *q=p;q->next!=NULL;q=q->next,n++);return n;
}
void output(list *p)
{
for(list *q=p;q->next!=NULL;q=q->next,printf("%d%c",q->data," \n"[q->next==NULL]));
}
void insert_head(list *p,int data)
{
list *node=(list *)malloc(sizeof(list));
list *q=p;
node->data=data;node->next=q->next;node->pre=q;
if(node->next!=NULL)node->next->pre=node;
q->next=node;
}
void insert_tail(list *p,int data)
{
list *node=(list *)malloc(sizeof(list));
node->data=data;
list *q=p;
while(q->next!=NULL)q=q->next;
q->next=node;
node->pre=q;node->next=NULL;
}
// list *insert(list *p,int data,int key,int index)
// {
// }
void delet(list *p,int key)
{
if(p->next==NULL)return ;
list *q=p;
list *temp=NULL;
if(p->data==key)
{
p=p->next;
free(q);q=NULL;return ;
}
for(q=p;q->next!=NULL and q->next->data!=key;q=q->next);
if(q->next==NULL){puts("no find");return ;}
temp=q->next;
q->next=temp->next;
free(temp);temp=NULL;
}
list *sort(list *head)
{
for(list *p=head->next;p->next!=NULL;p=p->next)
{
list *min;
for(list *node=p->next;node!=NULL;node=node->next)
{
if(node->data<min->data)min=node;
}
if(min!=p)
{
min->data^=p->data;p->data^=min->data;min->data^=p->data;
}
}return head;
}
int main()
{
list *p=creat(5);
insert_tail(p,123);
output(p);
delet(p,5);output(p);
list *q=sort(p);
while(q->next!=NULL)q=q->next,cout<<q->data<<" ";
}
©著作权归作者所有:来自51CTO博客作者qinXpeng的原创作品,如需转载,请注明出处,否则将追究法律责任
链表51cto数据结构