顺序表+单链表+双链表

来源:互联网 发布:3d场景软件 编辑:程序博客网 时间:2024/05/20 14:22

顺序表用数组存放数据,它的数据个数是固定的,有一个上限位Max,而单链表则没有上限,而单链表的创建有两种方法,头插法和尾插法,头插法是创建栈的方法,而尾插法是创建队列的方法,单链表的每个结点包含一个存放数据的变量,和指向下一结点的指针,而双链表的每个结点包含一个存放数据的变量和指向前面结点的指针和指向后面结点的指针,也即双链表多提供了一个指针,用来指向它前面的结点,这样在进行一些操作时就容易的多了,也直接多了(相对于单链表),虽然以前写过链表的代码,但这次对于链表的操作与以往不同,主要体现在以前是在头增加或删除一个结点,现在是在任何位置增加或删除一个结点,以前是取队首元素的值,现在是取任意位置的值,下面提供头插法创建的单链表和双链表代码:

//顺序链表的各种操作//search(int x); 查找某个元素是否存在//Insert(int i,int x);在第i个元素之前,插入值x//Delete(int i);删除结点i//Display();显示链表//init();初始化#include <stdio.h>#include <stdlib.h>#define Max 100typedef int type_data;typedef struct subseq{type_data data[Max];int len;}Subseq;bool Is_empty(Subseq *seq){return seq->len==0;}bool Is_full(Subseq *seq){return seq->len==Max;}void Init(Subseq *seq){seq->len=0;}void Insert(Subseq *seq,int i,type_data x){     if(Is_full(seq)){        printf("wrong operation,the seq if full\n");        return ; } if(i<=0){ printf("the position you are insert doesn't exist\n");        return ; } if(i>seq->len){ seq->data[seq->len]=x; seq->len++; return ; }     for(int j=seq->len;j>=i;j--)        seq->data[j]=seq->data[j-1]; seq->data[i-1]=x; seq->len++;}void Delete(Subseq *seq,int i){if(Is_empty(seq)){printf("wrong operation,the seq is empty\n");return ;}if(i<=0 || i>seq->len){printf("the position you are deleting doesn't exist\n");return ;}for(int j=i-1;j<seq->len-1;j++)seq->data[j]=seq->data[j+1];seq->len--;}void Search(Subseq *seq,type_data x){for(int i=0;i<seq->len;i++)if(seq->data[i]==x){printf("%d is in the position of %d\n");            return ;}printf("not exist\n");}void Display(Subseq *seq){for(int i=0;i<seq->len;i++)printf("%d ",seq->data[i]);printf("\n");}int main(){   Subseq seq;Init(&seq);int x,y;while(true){printf("a------------------退出\n");printf("b------------------插入\n");printf("c------------------删除\n");printf("d------------------显示\n");switch(getchar()){case 'a': exit(0); break;case 'b': printf("input two operators\n");scanf("%d%d",&x,&y);Insert(&seq,x,y);break;case 'c':printf("input the number you want to delete\n");scanf("%d",&x);Delete(&seq,x);break;case 'd':Display(&seq);break;}getchar();}return 0;}

单链表代码:

//链表提供操作//1查找第i个元素,返回指针//2查找给定结点的值并输出其输出位置//3在第i个位置之后插入结点值为x//4删除第i个结点//5创建单链表n个//6.显示#include <stdio.h>#include <stdlib.h>typedef int type_data;typedef struct Seq{type_data data;struct Seq *next;}*seq,Seq;void Creat_seq(seq p,int n){type_data data;seq temp;for(int i=0;i<n;i++){scanf("%d",&data);        temp=(seq)malloc(sizeof(Seq));temp->data=data;temp->next=p;p=temp;}}seq Search_number(seq p,int i){int num=0;  for(seq q=p;q;q=q->next){  num++;  if(num==i)  return q;  }  return NULL;}int Search_data(seq p,type_data x){int num=0;for(seq q=p;q;q=q->next){num++;if(q->data==x)return num;}printf("the value of data %d doesn't exist\n",x);return -1;}void Delete(seq p,int i){if(p==NULL){printf("the seq is empty\n");return ;}seq t;if(i==1){t=p->next;free(p);p=t;return ;}int num=1;for(seq q=p;q->next;q=q->next){num++;if(num==i){t=q->next->next;free(q->next);q->next=t;return ;}}}void Insert(seq p,int i,type_data x){if(i<=0){printf("the position is wrong\n");return ;}/*if(i==1){t=(seq)malloc(sizeof(*seq));t->data=x;t->next=p;p=t;return ;}*/int num=0;for(seq q=p;q;q=q->next){num++;if(num==i){seq t=(seq)malloc(sizeof(Seq));t->data=x;t->next=q->next;q->next=t;return ;}}}void Display(seq p){for(seq q=p;q;q=q->next)printf("%d ",q->data);printf("\n");}int main(){return 0;}


双向链表代码:

//双链表提供的操作//创建双链表Creat();查找某个数据元素的值Search(int x);在第i个位置之前插入新元素Insert(int i,int x);Delete删除第i个元素的结点;显示(Display)#include <stdio.h>#include <stdlib.h>typedef int type_data;typedef struct Seq{type_data data;struct Seq *first;struct Seq *next;}seq;void Init(seq *&p){p->next=NULL;}bool Is_empty(seq *p){return p->next==NULL;}void Creat(seq *&p,int n){type_data data;seq *t;for(int i=0;i<n;i++){scanf("%d",&data);t=(seq*)malloc(sizeof(seq));t->data=data;t->next=p;p->first=t;p=t;}}int Search(seq *p,type_data data){int num=1;for(seq *q=p;q->next;q=q->next){if(q->data==data){printf("the data of %d is in the position of %d \n",data,num);return num;}num++;}printf("the data doesn't exist in the seq\n");return -1;}void Insert(seq *&p,int i,type_data data){if(i<=0){printf("wrong operation\n");return ;}seq *t=(seq*)malloc(sizeof(seq));if(p->next==NULL || i==1){t->data=data;t->next=p;p->first=t;p=t;return ;}int num=1;for(seq *q=p;q->next;q=q->next){if(num==i){t->data=data;q->first->next=t;t->first=q->first;q->first=t;t->next=q;return ;}num++;}}void Delete(seq *&p,int i){if(p->next==NULL){printf("the seq is empty\n");return ;}if(i<=0){printf("wrong operation\n");return ;}    if(i==1){seq *t=p;p=p->next;free(t);return ;}int num=1;for(seq *q=p;q->next;q=q->next){if(num==i){q->first->next=q->next;q->next->first=q->first;free(q);return ;}num++;}}void Display(seq *p){for(seq *q=p;q->next;q=q->next)printf("%d ",q->data);}int main(){seq vec;seq *p=&vec;Init(p);Creat(p,10);Display(p);Delete(p,10);Display(p);/*Insert(p,8,1);Insert(p,1,2);Display(p);Delete(p,1);Display(p);Insert(p,1,3);Insert(p,1,4);Display(p);Search(p,1);*/return 0;}



 

原创粉丝点击