Sei sulla pagina 1di 16

#include<stdio.

h>

#include<stdlib.h>

typedef int listEntry;

typedef struct node

listEntry entry;

struct node *next;

}Node;

Node *initLinkedList();

Node *createNode(listEntry entry);

Node *insertBegin(Node *head,listEntry entry);

Node *insertEnd(Node *head,listEntry entry);

Node *insertSorted(Node *head,listEntry entry);

Node *searchList(Node *head,int data);

Node *insertAtPosition(Node *head,listEntry entry,int position);

Node *deleteNode(Node *head,int data);

Node *reverse(Node *head);

Node *recursiveReverse(Node *head);

Node *listdivide(Node *head,Node *newhead);

Node *Nthnode(Node *head);

Node *totalOccurrence(Node *head);

Node *previousandnextnode(Node *head);

Node *appendList(Node * head1,Node* head2);

Node * sorted_lists(Node * head1,Node * head2);

void bubbleSort(Node **head,int count);

int printLinkedList(Node *head);

int main()

Node *head,*newhead;

int ch=-1;

int pos,no;
listEntry c;

head = NULL;

newhead=NULL;

printf("0: Print Linked List\n");

printf("1: Insert at Beginning\n");

printf("2: Insert at End\n");

printf("3: Insert in sorted manner\n");

printf("4: Insert at position\n");

printf("5: To find the occurence of a number\n");

printf("6: To delete a node\n");

printf("7: To reverse the Linked List\n");

printf("8: To reverse the Linked List RECURSIVELY\n");

printf("9: To sort the Linked List\n");

printf("10: To split the Linked List\n");

printf("11: To print the Nth node of the Linked List\n");

printf("12: To print no of occurrence of a number in the Linked List\n");

printf("13: To print the previous and next node of the Nth node of the Linked List\n");

printf("14: To append 2 Linked Lists\n");

printf("14: To append 2 Linked Lists in an arranged order\n");

printf("99:To Exit\n");

while(ch!=99)

printf("Enter a choice as following\n");

scanf("%d",&ch);

switch(ch)

case 0:no=printLinkedList(head);

break;

case 1:printf("Enter the number to be inserted\n");

fflush(stdin);

scanf("%d",&c);
head =insertBegin(head,c);

break;

case 2:printf("Enter the number to be inserted\n");

fflush(stdin);

scanf("%d",&c);

head =insertEnd(head,c);

break;

case 3:printf("Enter the number to be inserted\n");

fflush(stdin);

scanf("%d",&c);

head =insertSorted(head,c);

break;

case 4:printf("Enter the number to be inserted\n");

fflush(stdin);

scanf("%d",&c);

printf("Enter the position at which it is to be inserted\n");

scanf("%d",&pos);

head =insertAtPosition(head,c,pos);

break;

case 5:printf("Enter the number to be searched\n");

scanf("%d",&c);

searchList(head,c);

break;

case 6:printf("Enter the number to be deleted\n");

scanf("%d",&c);

deleteNode(head,c);

break;

case 7:head=reverse(head);

printf("Operation successful\n");

break;

case 8:head=recursiveReverse(head);
printf("Operation successful\n");

no=printLinkedList(head);

break;

case 9:printf("The Linked List to be sorted is");

pos=printLinkedList(head);

bubbleSort(&head,pos);

printf("Operation successful\n");

no=printLinkedList(head);

break;

case 10:newhead=listdivide(head,newhead);

printf("The two Linked List are:\n");

printLinkedList(head);

printf("\n");

printLinkedList(newhead);

break;

case 11:Nthnode(head);

break;

case 12:totalOccurrence(head);

break;

case 13:previousandnextnode(head);

break;

case 14:appendList(head,newhead);

break;

case 15:head=sorted_lists(head,newhead);

break;

case 99:exit(0);

default:printf("Wrong Input");

return 0;

}
Node *createNode(listEntry entry)

Node *p;

p =(Node*)malloc(sizeof(Node));

p->entry = entry;

p->next = NULL;

return(p);

Node *insertBegin(Node *head,listEntry entry)

Node *p;

p =createNode(entry);

if(head == NULL)

head=p;

else

p->next=head;

head = p;

return(head);

Node *insertEnd(Node *head,listEntry entry)

Node *p,*curr;

p =createNode(entry);

curr=head;

if(curr == NULL)

head=p;
}

else

while(curr->next!=NULL)

curr=curr->next;

curr->next=p;

return(head);

Node *insertSorted(Node *head,listEntry entry)

Node *p,*curr;

p =createNode(entry);

curr=head;

if(curr==NULL)

head=p;

else

curr=head;

while((curr->next!=NULL)&&(((curr->next->entry) <= (p->entry))))

curr=curr->next;

p->next=curr->next;

curr->next=p;

return(head);

Node * searchList(Node *head,int data)


{

Node *curr;

curr=head;

int i=0;

printf("The number to be searched is %d\n",data);

while(curr != NULL)

if(curr->entry==data)

i++;

curr=curr->next;

printf("The no of number%d present in list is %d\n",data,i);

return head;

Node *insertAtPosition(Node *head,listEntry entry,int pos)

int i=0;

Node *p,*curr;

p =createNode(entry);

curr=head;

if((curr == NULL)&&(pos == 1))

head=p;

return(head);

else
{

if((curr == NULL)&&(pos != 1))

printf("Incorrect Operation");

else

if((curr != NULL)&&(pos == 1))

p->next =head;

head=p;

else if(pos<1)

printf("Incorrect Operation");

else

if(curr != NULL)

curr=head;

for(i=2;((i < pos)&&(curr != NULL));i++)

curr=curr->next;

if(curr != NULL)

p->next=curr->next;

curr->next=p;

printf("Data successfully inserted");


}

else

printf("Data cannot be inserted");

return(head);

Node *deleteNode(Node *head,int data)

Node *prev,*curr,*p;

if(head == NULL)

printf("List is empty");

else if(head->entry == data)

prev = head;

curr = head->next;

free(prev);

head = curr;

printf("The node is deleted");

else

prev = head;

curr=head->next;

while((curr != NULL)&&(curr->entry != data))


{

prev = curr;

curr = curr->next;

if(curr == NULL)

printf("Can't be deleted");

else

prev->next = curr->next;

curr->next = NULL;

return head;

Node *reverse(Node *head)

Node *prev,*curr,*next;

curr = head;

prev = NULL;

next = NULL;

if(head != NULL)

while(curr != NULL)

next = curr->next;

curr->next = prev;

prev = curr;

curr = next;

}
}

head = prev;

return head;

Node *recursiveReverse(Node *head)

Node *revhead;

if(head == NULL)

return NULL;

if(head->next == NULL)

return head;

revhead = recursiveReverse(head->next);

head->next->next=head;

head->next=NULL;

return(revhead);

void bubbleSort(Node **head,int count)

Node** h;

Node *temp,*p1,*p2;

int i, j, flag=1;

for (i = 0; i <= count&& flag == 1; i++)

h = head;

flag = 0;

for (j = 0; j < count - i - 1; j++)

p1 = *h;

p2 = p1->next;

if (p1->entry > p2->entry)

{
temp=p2->next;

p2->next=p1;

p1->next=temp;

*h = p2;

flag = 1;

h = &(*h)->next;

Node *listdivide(Node *head,Node *newhead)

Node *curr;

curr=head;

int i,j=0;

i=printLinkedList(head);

while(j<((i/2)-1))

curr=curr->next;

j++;

newhead=curr->next;

curr->next=NULL;

printf("%d",newhead->entry);

return newhead;

Node *Nthnode(Node *head)

int i;

Node *curr;

curr=head;
printf("Enter the node you want");

scanf("%d",&i);

while(i>1)

curr=curr->next;

i--;

printf("\nThe node contains %d\n",curr->entry);

Node *totalOccurrence(Node *head)

Node *curr;

int i,count=0;

curr=head;

printf("Enter the number to be searched");

scanf("%d",&i);

while(curr->next!=NULL)

if(curr->entry==i)

count++;

curr=curr->next;

printf("11qqww");

if(curr->entry==i)

count++;

printf("\nThe total occurrence of %d is %d\n",i,count);

Node *previousandnextnode(Node *head)

int i;

Node *curr;
curr=head;

printf("Enter the node you want to enter");

scanf("%d",&i);

while(i>2)

curr=curr->next;

printf("\nThe previous node contains %d\n",curr->entry);

curr=curr->next;

curr=curr->next;

printf("\nThe next node contains %d\n",curr->entry);

Node * appendList(Node * head1,Node* head2){

Node *p;

p=head1;

while(p->next!=NULL){

p=p->next;

p->next=head2;

return(head1);

Node * sorted_lists(Node *head1,Node *head2){

Node *p,*curr;

p=(Node *)malloc(sizeof(Node));

p->next=NULL;

while(head1->next!=NULL||head2->next!=NULL)

if(head1->entry<head2->entry)

insertSorted(p,head1->entry);
head1=head1->next;

else

if(head1->entry<head2->entry)

insertSorted(p,head2->entry);

head2=head2->next;

if(head1->next==NULL)

while(head2->next!=NULL)

insertSorted(p,head2->entry);

head2=head2->next;

else

while(head1->next!=NULL)

insertSorted(p,head1->entry);

head1=head1->next;

return p;

int printLinkedList(Node *head)

Node *curr;

curr=head;
int i=0;

while(curr!=NULL)

i++;

printf("\n%d",curr->entry);

curr=curr->next;

printf("\nThe no of nodes in list is %d\n",i);

return i;

Potrebbero piacerti anche