Sie sind auf Seite 1von 33

Append

#include<stdio.h> #include<conio.h> struct singly { int data; struct singly *next; }; typedef struct singly node; node *head=NULL; node * create(node *,int ); node * insert_node(node *,int ); node *ptr1,*temp1; main() { int choice,j=1; printf("how many elements u wish to enter\t"); scanf("%d",&j); head= create(head,j); temp1=head; printf("\nAfter which node u want to insert a node <=%d \t",j); scanf("%d",&choice); if(choice>j) {

printf("insertion at this place is not possible"); } else { ptr1 =insert_node(head,choice); printf("\nEnter data in the new node \t"); scanf("%d",&ptr1->data); printf("The new list is:\n"); while(temp1!=NULL) { printf("%d\t",temp1->data); temp1=temp1->next; } } getch(); } node * create(node *head,int j) { node *ptr1, *ptr2; int i; for(i=0;i<j;i++) { ptr1=(node *)malloc(sizeof (node)); if(head==NULL) {

printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; head=ptr1; ptr2=ptr1; } else { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; ptr2->next=ptr1; ptr2=ptr1; } } return head; } node * insert_node(node *head,int choice) { node *ptr1, *ptr2, *temp; int i; temp=head; ptr1=(node *)malloc(sizeof (node)); for(i=1;i<choice;i++) {

temp=temp->next; } ptr1->next=temp->next; temp->next=ptr1; return ptr1; }

Circular delete

#include<stdio.h> #include<conio.h> struct singly { int data; struct singly *next; }; typedef struct singly node; node *head=NULL; node * create(node *,int ); node * delete_node(node *,int ); node *ptr1,*temp1; main() { int choice,j,k,i;

printf("How many elements u wish to enter\t"); scanf("%d",&j); head= create(head,j); printf("\nWhich node u want to delete \t"); scanf("%d",&choice); if(choice>j) { k=choice%j; head=delete_node(head,k); } else { head=delete_node(head,choice); } temp1=head; printf("The new list is:\n"); for(i=1;i<j;i++) { printf("%d\t",temp1->data); temp1=temp1->next; }

getch(); } node * create(node *head,int j)

{ node *ptr2; int i; for(i=0;i<j;i++) { ptr1=(node *)malloc(sizeof (node)); if(head==NULL) { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=ptr1; head=ptr1; ptr2=ptr1; } else { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=head; ptr2->next=ptr1; ptr2=ptr1; } } return head; }

node * delete_node(node *head,int choice) { node *n1, *ptr2, *temp; int i; n1=head; temp=head; if(choice==1) { head=temp->next; free(temp); } else { for(i=1;i<choice-1;i++) n1=n1->next; for(i=1;i<choice;i++) temp=temp->next; n1->next=temp->next; free(temp); } return head; }

Circular insert

#include<stdio.h> #include<conio.h> struct singly { int data; struct singly *next; }; typedef struct singly node; node *head=NULL; node * create(node *,int ); node * insert_node(node *,int ); node *ptr1,*temp1; main() { int i,choice,j,k; printf("how many elements u wish to enter\t"); scanf("%d",&j); head= create(head,j); temp1=head; printf("\nAfter which node u want to insert a node \t"); scanf("%d",&choice); if(choice>j) {

k=choice%j; ptr1=insert_node(head,k); printf("\nEnter data in the new node \t"); scanf("%d",&ptr1->data); } else { ptr1 =insert_node(head,choice); printf("\nEnter data in the new node \t"); scanf("%d",&ptr1->data); } printf("The new list is:\n"); for(i=1;i<=j+1;i++) { printf("%d\t",temp1->data); temp1=temp1->next; } getch(); } node * create(node *head,int j) { node *ptr1, *ptr2; int i; for(i=0;i<j;i++) {

ptr1=(node *)malloc(sizeof (node)); if(head==NULL) { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=ptr1; head=ptr1; ptr2=ptr1; } else { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=head; ptr2->next=ptr1; ptr2=ptr1; } } return head; } node * insert_node(node *head,int choice) { node *ptr1, *ptr2, *temp; int i; temp=head;

ptr1=(node *)malloc(sizeof (node)); for(i=1;i<choice;i++) { temp=temp->next; } ptr1->next=temp->next; temp->next=ptr1; return ptr1; }

Concatenate with functions

#include<stdio.h> #include<conio.h> struct singly { int data; struct singly *next; }; typedef struct singly node; node *head=NULL; node *head1=NULL; node * create(node *,int ); node * concatenate(node *,node * );

node *ptr1,*temp1,*temp2; main() { int j,j1; char choice;

printf("How many elements u wish to enter in list 1\t"); scanf("%d",&j); head=create(head,j); printf("How many elements u wish to enter in list 2\t"); scanf("%d",&j1); head1=create(head1,j1); temp1=head; temp2=head1; printf("\ndo you want to merge the two lists (y/n) \t",j); fflush(stdin); scanf("%c",&choice); if(choice=='y') { concatenate(head,head1); printf("\nThe new list formed after concatenation is:\n"); while(temp1!=NULL) { printf("%d\t",temp1->data); temp1=temp1->next; } }

else { printf("\nElements of list 1 are:\n"); while(temp1!=NULL) { printf("%d\t",temp1->data); temp1=temp1->next; } printf("\nElements of list 2 are:\n"); while(temp2!=NULL) { printf("%d\t",temp2->data); temp2=temp2->next; } } getch(); } node * create(node *head,int j) { node *ptr2; int i; for(i=0;i<j;i++) { ptr1=(node *)malloc(sizeof (node)); if(head==NULL)

{ printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; head=ptr1; ptr2=ptr1; } else { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; ptr2->next=ptr1; ptr2=ptr1; } } return head; } node * concatenate(node *head,node *head1) { node *ptr2,*temp; int i; temp=head; while(temp->next!=NULL) {

temp=temp->next; } temp->next=head1; return 0; }

Doubly delte #include<stdio.h> #include<conio.h> struct doubly { int data; struct doubly *next; struct doubly *prev; }; typedef struct doubly node; node *head=NULL; node * create(node *,int ); node * delete_node(node *,int ); node *ptr1,*temp1; main() { int choice,j; printf("how many elements u wish to enter\t");

scanf("%d",&j); head= create(head,j); printf("\nWhich node u want to delete <=%d \t",j); scanf("%d",&choice); if(choice>j) { printf("deletion at this place is not possible"); } else { head=delete_node(head,choice); temp1=head; printf("The new list is:\n"); while(temp1!=NULL) { printf("%d\t",temp1->data); temp1=temp1->next; } } getch(); } node * create(node *head,int j) { node *ptr2; int i;

for(i=0;i<j;i++) { ptr1=(node *)malloc(sizeof (node)); if(head==NULL) { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; ptr1->prev=NULL; head=ptr1; ptr2=ptr1; } else { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; ptr1->prev=ptr2; ptr2->next=ptr1; ptr2=ptr1; } } return head; } node * delete_node(node *head,int choice)

{ node *n1, *ptr2, *temp,*temp2; int i; n1=head; temp=head; if(choice==1) { head=temp->next; head->prev=NULL; free(temp); } else { for(i=1;i<choice-1;i++) n1=n1->next; for(i=1;i<choice;i++) temp=temp->next; temp2=temp->next; n1->next=temp->next; temp2->prev=n1; free(temp); } return head; }

Doubly insert

#include<stdio.h> #include<conio.h> struct doubly { int data; struct doubly *next; struct doubly *prev; }; typedef struct doubly node; node *head=NULL; node * create(node *,int ); node * insert_node(node *,int ); node *ptr1,*temp1; main() { int choice,j=1; printf("how many elements u wish to enter\t"); scanf("%d",&j); head= create(head,j); temp1=head; printf("\nAfter which node u want to insert a node <=%d \t",j); scanf("%d",&choice); if(choice>j)

{ printf("insertion at this place is not possible"); } else { ptr1 =insert_node(head,choice); printf("\nEnter data in the new node \t"); scanf("%d",&ptr1->data); printf("The new list is:\n"); while(temp1!=NULL) { printf("%d\t",temp1->data); temp1=temp1->next; } } getch(); } node * create(node *head,int j) { node *ptr1, *ptr2; int i; for(i=0;i<j;i++) { ptr1=(node *)malloc(sizeof (node)); if(head==NULL)

{ printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; ptr1->prev=NULL; head=ptr1; ptr2=ptr1; } else { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; ptr1->prev=ptr2; ptr2->next=ptr1; ptr2=ptr1; } } return head; } node * insert_node(node *head,int choice) { node *ptr1, *ptr2, *temp; int i; temp=head;

ptr1=(node *)malloc(sizeof (node)); for(i=1;i<choice;i++) { temp=temp->next; } ptr1->prev=temp; ptr1->next=temp->next; temp->next=ptr1; return ptr1; }

Move

#include<stdio.h> #include<conio.h> struct singly { int data; struct singly *next; }; typedef struct singly node; node *head=NULL; node * create(node *,int ); node * move_node(node *,int ,int ); node *ptr1,*temp1;

main() { int choice,j=1,move; printf("how many elements u wish to enter\t"); scanf("%d",&j); head= create(head,j); printf("\nWhich node u want to MOVE <=%d \t",j); scanf("%d",&choice); if(choice>j) { printf("NO NODE EXISTS AT THIS LOCATION"); } else { printf("After which node u want to place it? \t"); scanf("%d",&move); head=move_node(head,choice,move); temp1=head; printf("The new list is:\n"); while(temp1!=NULL) { printf("%d\t",temp1->data); temp1=temp1->next; } }

getch(); } node * create(node *head,int j) { node *ptr1, *ptr2; int i; for(i=0;i<j;i++) { ptr1=(node *)malloc(sizeof (node)); if(head==NULL) { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; head=ptr1; ptr2=ptr1; } else { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; ptr2->next=ptr1; ptr2=ptr1; }

} return head; } node * move_node(node *head,int choice,int move) { node *ptr1, *ptr2, *temp,*temp2,*temp3; int i; temp=head; temp2=head; temp3=head; for(i=1;i<move;i++) { temp2=temp2->next; } if(choice==1) { head=temp->next; temp->next=temp2->next; temp2->next=temp; } else { for(i=1;i<choice-1;i++) { temp=temp->next;

} temp3=temp->next; temp->next=temp3->next; temp3->next=temp2->next; temp2->next=temp3; } return head; }

Singly del

#include<stdio.h> #include<conio.h> struct singly { int data; struct singly *next; }; typedef struct singly node; node *head=NULL; node * create(node *,int ); node * delete_node(node *,int ); node *ptr1,*temp1; main() {

int choice,j; printf("how many elements u wish to enter\t"); scanf("%d",&j); head= create(head,j); printf("\nWhich node u want to delete <=%d \t",j); scanf("%d",&choice); if(choice>j) { printf("deletion at this place is not possible"); } else { head=delete_node(head,choice); temp1=head; printf("The new list is:\n"); while(temp1!=NULL) { printf("%d\t",temp1->data); temp1=temp1->next; } } getch(); } node * create(node *head,int j) {

node *ptr2; int i; for(i=0;i<j;i++) { ptr1=(node *)malloc(sizeof (node)); if(head==NULL) { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; head=ptr1; ptr2=ptr1; } else { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; ptr2->next=ptr1; ptr2=ptr1; } } return head; } node * delete_node(node *head,int choice)

{ node *n1, *ptr2, *temp; int i; n1=head; temp=head; if(choice==1) { head=temp->next; free(temp); } else { for(i=1;i<choice-1;i++) n1=n1->next; for(i=1;i<choice;i++) temp=temp->next; n1->next=temp->next; free(temp); } return head; }

Singly insert #include<stdio.h> #include<conio.h>

struct singly { int data; struct singly *next; }; typedef struct singly node; node *head=NULL; node * create(node *,int ); node * insert_node(node *,int ); node *ptr1,*temp1; main() { int choice,j=1; printf("how many elements u wish to enter\t"); scanf("%d",&j); head= create(head,j); temp1=head; printf("\nAfter which node u want to insert a node <=%d \t",j); scanf("%d",&choice); if(choice>j) { printf("insertion at this place is not possible"); } else {

ptr1 =insert_node(head,choice); printf("\nEnter data in the new node \t"); scanf("%d",&ptr1->data); printf("The new list is:\n"); while(temp1!=NULL) { printf("%d\t",temp1->data); temp1=temp1->next; } } getch(); } node * create(node *head,int j) { node *ptr1, *ptr2; int i; for(i=0;i<j;i++) { ptr1=(node *)malloc(sizeof (node)); if(head==NULL) { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; head=ptr1;

ptr2=ptr1; } else { printf("\nEnter data in the %d node \t",i+1); scanf("%d",&ptr1->data); ptr1->next=NULL; ptr2->next=ptr1; ptr2=ptr1; } } return head; } node * insert_node(node *head,int choice) { node *ptr1, *ptr2, *temp; int i; temp=head; ptr1=(node *)malloc(sizeof (node)); for(i=1;i<choice;i++) { temp=temp->next; } ptr1->next=temp->next; temp->next=ptr1;

return ptr1; }

Das könnte Ihnen auch gefallen