Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct Node{
- int q;
- Node *pre;
- Node *re;
- }node;
- node *lead;
- void atend(int g)
- {
- node *sub,*an;
- an=lead;
- if(lead==NULL){
- lead=(node*)malloc(sizeof(node));
- lead->q=g;
- lead->re=NULL;
- lead->pre=NULL;
- }
- else{
- sub=(node*)malloc(sizeof(node));
- sub->q=g;
- sub->re=NULL;
- while(an->re!=NULL){
- an=an->re;
- }
- sub->pre=an;
- an->re=sub;
- }
- return;
- }
- void atfirst(int a)
- {
- if(lead==NULL){
- lead=(node*)malloc(sizeof(node));
- lead->q=a;
- lead->re=NULL;
- lead->pre=NULL;
- return;
- }
- node *sub;
- sub=(node*)malloc(sizeof(node));
- sub->q=a;
- lead->pre=sub;
- sub->pre=NULL;
- sub->re=lead;
- lead=sub;
- return;
- }
- void atn(int v,int p)
- {
- node *sub,*end=lead;
- if(lead==NULL){
- printf("List is empty. Create list by pressing 1 or 2.\n");
- return;
- }
- sub=(node*)malloc(sizeof(node));
- sub->q=v;
- if(p==1){
- lead->pre=sub;
- sub->re=lead;
- sub->pre=NULL;
- lead=sub;
- return;
- }
- p-=2;
- while(p--){
- end=end->re;
- if(end->re==NULL)break;
- }
- sub->re=end->re;
- sub->pre=end->re->pre;
- end->re->pre=sub;
- end->re=sub;
- return;
- }
- void ataftern(int v,int p)
- {
- node *sub,*end=lead;
- if(lead==NULL){
- printf("List is empty. Create list by pressing 1 or 2.\n");
- return;
- }
- sub=(node*)malloc(sizeof(node));
- sub->q=v;
- if(p==1){
- sub->re=end->re;
- sub->pre=end->re->pre;
- end->re->pre=sub;
- end->re=sub;
- return;
- }
- p-=1;
- while(p--){
- end=end->re;
- if(end->re==NULL)break;
- }
- sub->re=end->re;
- sub->pre=end->re->pre;
- end->re->pre=sub;
- end->re=sub;
- return;
- }
- void eran(int d)
- {
- node *nn,*sub=lead;
- if(lead==NULL)return;
- if(d==1){
- nn=lead;
- lead=nn->re;
- lead->pre=NULL;
- free(nn);
- return;
- }
- d-=2;
- while(d--){
- if(sub->re==NULL){
- printf("Not found.\n");
- return;
- }
- sub=sub->re;
- }
- nn=sub->re;
- sub->re=nn->re;
- nn->re->pre=nn->pre;
- free(nn);
- return;
- }
- int lookfor(int z)
- {
- int nt=1;
- node *sub=lead;
- while(sub->q!=z){
- nt++;
- if(sub->re==NULL)return -1;
- sub=sub->re;
- }
- return nt;
- }
- void eravl(int z)
- {
- node *nn,*sub=lead;
- int x=lookfor(z);
- if(x==-1){
- printf("Not found\n");
- return;
- }
- if(x==1){
- nn=lead;
- lead=nn->re;
- lead->pre=NULL;
- free(nn);
- return;
- }
- x-=2;
- while(x--){
- sub=sub->re;
- }
- nn=sub->re;
- sub->re=nn->re;
- nn->re->pre=nn->pre;
- free(nn);
- return;
- }
- void listprint()
- {
- node *sub,*rev;
- sub=lead;
- while(sub!=NULL){
- printf("%d ",sub->q);
- sub=sub->re;
- }
- return;
- }
- void revlistprint()
- {
- node *sub,*rev;
- sub=lead;
- while(sub->re!=NULL){
- sub=sub->re;
- }
- rev=sub;
- while(rev!=NULL){
- printf("%d ",rev->q);
- rev=rev->pre;
- }
- return;
- }
- int main()
- {
- lead=NULL;
- int z,w,r;
- printf("1.Value at end\n");
- printf("2.Value at first\n");
- printf("3.Value at n-th place\n");
- printf("4.Value at after n-th place\n");
- printf("5.Delete n-th node\n");
- printf("6.Delete a value\n");
- printf("7.Find a value\n");
- printf("8.Print the list\n");
- printf("9.Print the reverse list\n");
- while(scanf("%d",&z)!=EOF){
- switch(z){
- case 1:
- scanf("%d",&w);
- atend(w);
- break;
- case 2:
- scanf("%d",&w);
- atfirst(w);
- break;
- case 3:
- printf("Value > ");
- scanf("%d",&r);
- printf("Which position > ");
- scanf("%d",&w);
- atn(r,w);
- break;
- case 4:
- printf("Value > ");
- scanf("%d",&r);
- printf("After which position > ");
- scanf("%d",&w);
- ataftern(r,w);
- break;
- case 5:
- scanf("%d",&r);
- eran(r);
- break;
- case 6:
- scanf("%d",&r);
- eravl(r);
- break;
- case 7:
- scanf("%d",&r);
- printf("Position %d\n",lookfor(r));
- break;
- case 8:
- listprint();
- printf("\n");
- break;
- case 9:
- revlistprint();
- printf("\n");
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement