#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#define Element int
//to adjust Element's type
#define turn int
//to adjust which point to choice printup or printdown
//if change "int",The scanf's number in it must be change too;
typedef struct Single
{
Element D;
Single *next;//to back
}list;
//single list type
typedef struct Double
{
Element D;
Double *front;
Double *next;
}list_double;
//double list type
//單鏈表例程
void init_single(list *&L);
void creatlist_single(list *&L,Element n);
void printlist_single(list *&L);
void insertlist_single(list *&L,Element n);//插入例程
list *findlist_single(list *&L,Element n);//用於發現一個節點的前驅元.n爲要發現的節點
void delelist_single(list *&L,Element n);//刪除爲n的
void delelist_all(list *&L,Element n);//刪除所有爲n的
void init_single(list *&L)//has been done
{
L=(list*)malloc(sizeof(list));
L->next=NULL;
}
void creatlist_single(list *&L,int n)//has been done
{
list *s,*r;
init_single(L);
r=L;
while(n!=0)
{
s=(list*)malloc(sizeof(list));
scanf("%d",&s->D);
r->next=s;
r=r->next;//use plus to position to position
n--;
}
r->next=NULL;
}
void printlist_single(list *&L)//has been done
{
list *p;
p=L->next;
while(p!=NULL)
{
printf("%d",p->D);
p=p->next;
}
printf("\n");
}
list* findlist_single(list *&L,Element n)//find plus preious
{
list *p;
p=L;
while(p->next!=NULL && p->next->D!=n)
p=p->next;
return p;
}
void delelist_single(list *&L,Element n)//has been done
{
list *p,*temp;
p=findlist_single(L,n);
if(p!=NULL)
{
temp=p->next;
p->next=temp->next;
free(temp);
}
}
void delelist_all(list *&L,Element n)//has been done!!!
{
list *p,*tmp;
p=L;
list *r;
while(1)
{
r=findlist_single(L,n);
if(r->next==NULL)
break;
tmp=r->next;
r->next=tmp->next;
free(tmp);
}
}
void insertlist_single(list *&L,Element n)//has been done!!!
{
list *newcell=NULL;
list *p;
Element m;
p=L;
printf("請輸入要插入的數:\n");
scanf("%d",&m);
while(p->next!=NULL)
{
if(p->D==n)
{
newcell=(list*)malloc(sizeof(list));
newcell->D=m;
newcell->next=p->next;
p->next=newcell;
}
p=p->next;
}
if(p->D==n && p->next==NULL)
{
newcell=(list*)malloc(sizeof(list));
newcell->D=m;
p->next=newcell;
newcell->next=NULL;
}
}
//雙鏈表例程
void init_double(list_double *&head,list_double *&rear);//different from init_single
void creatlist_double(list_double *&head,list_double *&rear,Element n);//it use "front plus"
void prints(list_double *&head,list_double *&rear,turn tag);
void insertlist_double(list_double *&head,Element n);
void delelist_double(list_double *&head,Element n);
void init_double(list_double *&head,list_double *&rear)//has been done;
{
head=(list_double*)malloc(sizeof(list_double));
head->next=NULL;
head->front=NULL;
rear=(list_double*)malloc(sizeof(list_double));
rear->next=NULL;
rear->front=NULL;
}
void creatlist_double(list_double *&head,list_double *&rear,Element n)//has been done
{
init_double(head,rear);
list_double *s,*r,*temp=NULL;
r=head;
while(n--)
{
s=(list_double*)malloc(sizeof(list_double));
scanf("%d",&s->D);
r->next=s;
temp=r;
r=r->next;
r->front=temp;
}
r->next=NULL;
rear=r;
}
void prints(list_double *&head,list_double *&rear,turn tag)//if tag=1 upprint,else downprint
{
list_double *p;
if(tag==1)
{ p=head->next;
while(p!=NULL)
{
printf("%d",p->D);
p=p->next;
}
}
else if(tag==0)
{
p=rear;
while(p->front!=NULL)
{
printf("%d",p->D);
p=p->front;
}
}
printf("\n");
}
void insertlist_double(list_double *&head,Element n)//insert back after n. has been done
{
list_double *p1,*p3=NULL;
p1=head;
Element m;
printf("請輸入要插入的數:\n");
scanf("%d",&m);
while(p1->next!=NULL)
{
if(p1->D==n)
{
p3=(list_double*)malloc(sizeof(list_double));
if(p3==NULL)
{
printf("there is no space!\n");
break;
}
p3->D=m;
p3->front=p1->front;
p3->next=p1->next;
p1->next=p3;
p1->front=p3;
}
p1=p1->next;
}
if(p1->D==n && p1->next==NULL)//LAST POINT
{
p3=(list_double*)malloc(sizeof(list_double));
p3->D=m;
p1->next=p3;
p1->front=p3;
p3->next=NULL;
p3->front=NULL;
}
}
void delelist_double(list_double *&head,Element n)//has been done
{
list_double *p1,*p2;
p1=head;
while(p1->next!=NULL)
{
p2=p1->next;
if(p2->D==n)
{
if(p2->next!=NULL)//not last point
{
p2->front->next=p2->next;
p2->next->front=p2->front;
free(p2);
}
else//last point
{
p2->front->next=NULL;
free(p2);
}
}
else
p1=p1->next;
}
}
int main()
{
Element n,m;
Element a,b;
turn tag;//tag=1 up tag=0 down
list *L;//相當於head指針
list_double *head;
list_double *rear;
scanf("%d",&n);//確定鏈表長度
/*Single list creat test*/
//creatlist_single(L,n);
//printlist_single(L);
/*Single delete list creat test*/
//scanf("%d",&m);//要刪除的數
//delelist_single(L,m);
//delelist_all(L,m);
//printlist_single(L);
/*Single insert*/
//insertlist_single(L,a);//在所有a後插入一個數
//printlist_single(L);
/*Double list creat test*/
creatlist_double(head,rear,n);
prints(head,rear,1);
/*Double delete list creat test*/
//delelist_double(head,m);
//prints(head,rear,1);
/*Double insert list creat test*/
printf("請輸入要在什麼數後插入:\n");
scanf("%d",&a);
/*Double insert*/
insertlist_double(head,a);
prints(head,rear,1);
return 0;
}