Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int data;
- struct node *link;
- };
- struct node* head=NULL;
- int l;
- void insertAtPosition(int j)
- { int i;
- struct node* temp;
- temp=(struct node*)malloc(sizeof(struct node));
- int pos;
- sos:
- {
- printf("Enter Position: ");
- scanf("%d",&pos);
- }
- if(pos<=j)
- {
- printf("Enter %d Position node data:",pos);
- scanf("%d",&temp->data);
- temp->link=NULL;
- struct node* p=head;
- if(pos==1)
- {
- temp->link=head;
- head=temp;
- }
- else
- {
- for(i=2; i<pos; i++)
- {
- p=p->link;
- }
- temp->link=p->link;
- p->link=temp;
- }
- }
- else if(pos==j+1)
- { printf("Enter %d Position node data:",pos);
- scanf("%d",&temp->data);
- temp->link=NULL;
- struct node* p=head;
- while(p->link!=NULL)
- {
- p=p->link;
- }
- p->link=temp;
- }
- else
- {
- printf("Invalid Position!\n");
- goto sos;
- }
- }
- void reverseDispList(int l)
- {
- struct node *p, *q;
- int i=0,j=l,k,temp;
- p=q=head;
- while(i<j)
- {
- k=0;
- while(k<j)
- {
- q=q->link;
- k++;
- }
- temp=p->data;
- p->data=q->data;
- q->data=temp;
- i++;
- j--;
- p=p->link;
- q=head;
- }
- }
- void insert(int data)
- {
- struct node* temp;
- temp=(struct node*)malloc(sizeof(struct node));
- temp->data=data;
- temp->link=NULL;
- if(head==NULL)
- {
- head=temp;
- }
- else
- {
- struct node *p=head;
- while(p->link!=NULL)
- {
- p=p->link;
- }
- p->link=temp;
- }
- }
- void deletenode(int j)
- {
- int n,i;
- struct node*p;
- p=head;
- sos:{
- printf("Enter Node which you want to delete: ");
- scanf("%d",&n);
- }
- if(n<=j+1)
- {
- if(n==1)
- {
- head=p->link;
- p->link=NULL;
- free(p);
- }
- else
- {
- struct node*q;
- for(i=1; i<n-1; i++)
- {
- p=p->link;
- }
- q=p->link;
- p->link=q->link;
- q->link=NULL;
- free(q);
- }
- }
- else
- {
- printf("Invalid Imput!");
- goto sos;
- }
- }
- void length()
- {
- struct node* temp;
- temp=head;
- while(temp!=NULL)
- {
- l++;
- temp=temp->link;
- }
- }
- void display()
- {
- struct node* temp;
- temp=head;
- while(temp!=NULL)
- {
- printf("%d ",temp->data);
- temp=temp->link;
- }
- }
- int main()
- {
- int n,i=5;
- int p=i;
- while(p--)
- {
- printf("Enter node data:");
- scanf("%d",&n);
- insert(n);
- }
- display();
- printf("\n");
- length();
- printf("The node Length id: %d\n",l);
- insertAtPosition(l);
- display();
- printf("\n");
- reverseDispList(l);
- printf("the reverse is:\n");
- display();
- printf("\n");
- deletenode(l);
- display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement