Delet Frequency in linked list

This is a discussion on Delet Frequency in linked list within the C Programming forums, part of the General Programming Boards category; hi, i want write function Delet Frequency in linked list By Id , I dont know what wrong withe this ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    24

    Delet Frequency in linked list

    hi,
    i want write function Delet Frequency in linked list By Id ,
    I dont know what wrong withe this code?
    Code:
     void del_freq(ptr *head){
    	ptr p,q;data Z;
    	int i,j,n=size(*head);
    	p=*head;
    	q=(*head)->next;
    	for(i=0;i<n-1;i++){
    		for(j=i+1;j<n;j++){
    			if(q->emp.id==p->emp.id){
    				del_first(&p,&Z);
    				n--;
    		}
    			q=q->next;
    		}
    		q=p->next;
    	}
    }
    ========================================
    and this is my all program
    Code:
    /*********** lap9 - linked list ***********/
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    /*********** Data ***********/
    typedef struct {
    	int id;
    	char name[60];
    	int age;
    	float salary;
    	char pos[30];
    }data;
    /*********** linkd list ***********/
    typedef struct node{
    	data emp;
    	struct node *next;
    }*ptr,recnode;
    /*********** The functions ***********/
    void menu();
    void Read(data*);
    void insert_first(ptr*,data);
    void insert_last(ptr*,data);
    void insert_mid(ptr*,data);
    int size(ptr);
    void print(ptr);
    void sorted(ptr*);
    void search(ptr,int,ptr*,ptr*);
    void search_By_name(ptr,char[],ptr*,ptr*);
    void delet(ptr*,int,data *);
    void del_first(ptr*,data *);
    void del_last(ptr*,data *);
    void max_salary(ptr,ptr*);
    void rev(ptr*);
    void del_freq(ptr*);
    /*********** main ***********/
    int main(){
    	int choice,t;
    	data R;
    	ptr head=NULL;
    	ptr cur,pre,p;
    	char a[60];
    	menu();
    	scanf("%d",&choice);
    	while(choice!=14){
    		switch(choice){
    		case 1:
    			Read(&R);
    			insert_first(&head,R);
    			break;
    		case 2:
    			Read(&R);
    			insert_last(&head,R);
    			break;
    		case 3:
    			Read(&R);
    			insert_mid(&head,R);
    			break;
    		case 4:
    			print(head);
    			break;
    		case 5:
    			sorted(&head);
    			break;
    		case 6:
    			del_first(&head,&R);
    				menu();
    			break;
    		case 7:
    			del_last(&head,&R);
    			menu();
    			break;
    		case 8:
    			printf("Eneter the id you want to delet :");
    			scanf("%d",&t);
    			delet(&head,t,&R);
    			menu();
    			break;
    		case 9:
    			printf("Enter the id to you finds :");
    			scanf("%d",&t);
    			search(head,t,&cur,&pre);
    			if(cur==NULL)
    				printf("Sorry not found\n");
    			else
    				printf("The Position in the list is %s\n",cur->emp.pos);
    			menu();
    				break;
    		case 10:
    			p=head;
    			max_salary(head,&p);
    			if(p!=NULL)
    			printf("\nThe Id is: %d , Name: %s , Age: %d , Salary: %.2f , Position: %s\n",p->emp.id,p->emp.name,p->emp.age,p->emp.salary,p->emp.pos);
    			menu();
    			break;
    		case 11:
    			rev(&head);
    			menu();
    			break;
    		case 12:
    			printf("Enter The name you want to change Id: ");
    			getchar();
    			gets(a);
    			search_By_name(head,a,&cur,&pre);
    			if(cur==NULL)
    				printf("Sorry not found the name\n");
    			else{
    				printf("\n");
    				printf("Enter the new id :");
    				scanf("%d",&cur->emp.id);}
    			menu();
    				break;
    		case 13:
    			del_freq(&head);
    			menu();
    			break;
    		default:
    			printf("Erorr choice\n");
    			menu();
    			break;
    		}
    		scanf("%d",&choice);
    	}
    	return 0;
    }
    /*********** menu ***********/
    void menu(){
    	printf("+------------------------------------------------------+");
    	printf("\n|                | The main menu |                     |");
    	printf("\n+------------------------------------------------------+");
    	printf("\n| 1- Inserts a new employee in the head of list        |");
    	printf("\n| 2- Inserts a new employee in the last.               |");
    	printf("\n| 3- Inserts a new employee in the middle of the list. |");
    	printf("\n| 4- Print all the employees and thir attributes.      |");
    	printf("\n| 5- Sorts the employees according to thir salaries.   |");
    	printf("\n| 6- Delet a first employee.                           |");
    	printf("\n| 7- Delet a last employee.                            |");
    	printf("\n| 8- Delet a given employee.                           |");
    	printf("\n| 9- Finds a given employee and returns his position.  |");
    	printf("\n| 10- Prints the with maximum salary.                  |");
    	printf("\n| 11- Revers the linked list.                          |");
    	printf("\n| 12- chang Id.                                        |");
    	printf("\n| 13- Delet Frequency By Id.                           |");
    	printf("\n| 14- End.                                             |");
    	printf("\n+------------------------------------------------------+");
    	printf("\nEnter your choice :");
    }
    /*********** Read Record ***********/
    void Read(data *R){
    	printf("\n+-------------------------------+");
    	printf("\n|   Enter the info of employees |");
    	printf("\n+-------------------------------+");
    			printf("\nEnter the id number :");
    			scanf("%d",&R->id);
    			printf("\n");
    						printf("Enter the name (first - middle -last):");
    			scanf("%s",R->name);
    			printf("\n");
    			getchar();
    			printf("Enter the age :");
    			scanf("%d",&R->age);
    			printf("\n");
    			printf("Enter the salary :");
    			scanf("%f",&R->salary);
    			printf("\n");
    			printf("Enter the position :");
    			scanf("%s",R->pos);
    			printf("\n");
    			getchar();
    			menu();
    }
    /*********** insert_first ***********/
    void insert_first(ptr *head,data R){
    	ptr p;
    	p=(struct node*)malloc(sizeof(recnode));
    	p->emp=R;
    	if(*head==NULL){
    		*head=p;
    		(*head)->next=NULL;
    	}
    	else{
    		p->next=*head;
    		*head=p;
    	}
    }
    /*********** Print linkedlist ***********/
    void print(ptr head){
    	while(head!=NULL){
    		printf("\nThe Id is: %d , Name: %s , Age: %d , Salary: %.2f , Position: %s",head->emp.id,head->emp.name,head->emp.age,head->emp.salary,head->emp.pos);
    		head=head->next;
    	}
    	printf("\n");
    	menu();
    }
    /*********** insert_last ***********/
    void insert_last(ptr *head,data R){
    	ptr p,q;
    	p=(struct node*)malloc(sizeof(recnode));
    	p->emp=R;
    	p->next=NULL;
    	if(*head==NULL)
    		*head=p;
    	else{
    		q=*head;
    		while(q->next!=NULL)
    			q=q->next;
    		q->next=p;
    	}
    }
    /*********** size ***********/
    int size(ptr head){
    	int n=0;
    	while(head!=NULL){
    		n++;
    		head=head->next;
    	}
    	return n;
    }
    /*********** insert_mid ***********/
    void insert_mid(ptr*head,data R){
    	ptr p,q,r;
    	int n,k;
    	n=size(*head);
    	p=(struct node*)malloc(sizeof(recnode));
    	p->emp=R;
    	if(*head==NULL){
    		*head=p;
    		(*head)->next=NULL;
    	}
    	else if((*head)->next==NULL){
    		(*head)->next=p;
    		p->next=NULL;
    	}
    	else{
    		k=n/2;
    		for(q=*head;k>0;k--)
    			q=q->next;
    		k=(n/2)-1;
    		for(r=*head;k>0;k--)
    			r=r->next;
    		p->next=q;
    		r->next=p;
    	}
    }
    /*********** sort ***********/
    void sorted(ptr *head){
    	ptr p,q;
    	data t;
    	int n,j,i;
    	if(*head==NULL)
    		printf("sorry no linked list to sort\n");
    	else{
    	p=*head;
    	q=(*head)->next;
    	n=size(*head);
    	for(i=0;i<n-1;i++){
    		for(j=i+1;j<n;j++){
    			if((p->emp.salary)<(q->emp.salary)){
    				t=p->emp;
    				p->emp=q->emp;
    				q->emp=t;
    			}
    			q=q->next;
    			p=p->next;
    		}
    		p=*head;
    		q=(*head)->next;
    	}}
    	menu();
    }
    /*********** search ***********/
    void search(ptr head,int target,ptr *cur,ptr *pre){
    	*cur=*pre=NULL;
    	while((head!=NULL) && (*cur==NULL)){
    		if((head->emp.id)==target)
    			*cur=head;
    		else{
    			*pre=head;
    			head=head->next;
    		}
    	}
    }
    /*********** Delet ***********/
    void delet(ptr *head,int t,data *Z){
    	ptr cur,pre;
    	search(*head,t,&cur,&pre);
    	if(cur==NULL)
    		printf("Sorry the id :%d not found \n",t);
    	else{
    		if(cur==*head)
    			del_first(head,Z);
    		else{
    			if(cur->next==NULL)
    			del_last(head,Z);
    		else{
    			pre->next=cur->next;
    			*Z=cur->emp;
    			free(cur);
    		}
    	}
    }
    }				
    /*********** Delet First ***********/
    void del_first(ptr *head,data *Z){
    	ptr p;
    	if(*head==NULL)
    		printf("Sorry the linked list is empty\n");
    	else{
    		if((*head)->next==NULL){
    			*Z=(*head)->emp;
    			free(*head);
    		}
    		else{
    			p=*head;
    			*Z=p->emp;
    			*head=(*head)->next;
    			free(p);
    		}
    	}
    }
    /*********** Delet last ***********/
    void del_last(ptr *head,data *Z){
    	
    	ptr p,q;
    	if(*head==NULL)
    		printf("Sorry the linked list is empty\n");
    	else{
    		if((*head)->next==NULL){
    			*Z=(*head)->emp;
    			free(*head);
    		}
    		else{
    			p=*head;
    			q=(*head)->next;
    			while(q->next!=NULL){
    				q=q->next;
    				p=p->next;
    			}
    			*Z=q->emp;
    			p->next=NULL;
    			free(q);
    		}
    	}
    }
    /*********** Max salary ***********/
    void max_salary(ptr head,ptr *cur){
    	ptr p;
    	float max;
    	if(head==NULL)
    		printf("Sorry Empty linked linst\n");
    	else{
    		max=head->emp.salary;
    		p=head->next;
    		while(p!=NULL){
    			if(max<p->emp.salary){
    				max=p->emp.salary;
    				*cur=p;
    			}
    			p=p->next;
    		}
    	}
    }
    /*********** Revers ***********/
    void rev(ptr *head){
    	ptr p,q;
    	data t;
    	int i,n,j;
    	p=*head;
    	q=(*head)->next;
    	n=size(*head);
    	for(i=0;i<n-1;i++){
    		for(j=i+1;j<n;j++){
    			t=p->emp;
    			p->emp=q->emp;
    			q->emp=t;
    			q=q->next;
    			p=p->next;
    		}
    		q=(*head)->next;
    		p=*head;
    	}
    }
    /*********** search ***********/
    void search_By_name(ptr head,char a[],ptr *cur,ptr *pre){
    	*cur=*pre=NULL;
    	while((head!=NULL) && (*cur==NULL)){
    		if(strcmp(head->emp.name,a)==0)
    			*cur=head;
    		else{
    			*pre=head;
    			head=head->next;
    		}
    	}
    }
    /*********** Delet Frequency By Id***********/
    void del_freq(ptr *head){
    	ptr p,q;data Z;
    	int i,j,n=size(*head);
    	p=*head;
    	q=(*head)->next;
    	for(i=0;i<n-1;i++){
    		for(j=i+1;j<n;j++){
    			if(q->emp.id==p->emp.id){
    				del_first(&p,&Z);
    				n--;
    		}
    			q=q->next;
    		}
    		q=p->next;
    	}
    }
    /*********** End ***********/

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    hi,
    i want write function Delet Frequency in linked list By Id ,
    I dont know what wrong withe this code?
    I don't know, how about you tell me? Seriously. What do you want it to do, what does it do, what doesn't it do. Let me guess, you didn't READ THIS, did you?

    Consider changing your loop.
    Code:
    for( n = first_node; n != NULL; n = n->next )
        for( o = n->next; o != NULL; o = o->next )
            if( o->data == n->data )
            {
                n->next = o->next;
                free( o );
            }
    Something like that should suffice. You're overcomplicating.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    first thanks for help - second sorry becaus i did not clear my idea very well
    e.g the input in linked list
    1->2->3->1->2->5->3
    the output is
    1->2->3->5
    ---------------------------
    i chang the code but not work???
    this the new code
    Code:
    void del_freq(ptr *head){
    	ptr p,q;
    	for(p=*head;p!=NULL;p=p->next)
    		for(q=p->next;q!=NULL;q=q->next)
    			if(q->emp.id==p->emp.id){
    				p->next=q->next;
    				free(q);//i think here is mistake the q is dead and in loop write q=q->next??
    			}
     
    }

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    Adding q=p; in the code (see below) should do,

    Code:
    void del_freq(ptr *head)
    {
          ptr p,q;
          for(p=*head;p!=NULL;p=p->next)
          {
                 for(q=p->next;q!=NULL;q=q->next)
                 {
                          if(q->emp.id==p->emp.id)
                          {
                                  p->next = q->next;
                                  free(q);
                                  q=p; // Similar to starting the loop from the beginning once again
                          }
                 }
          }
    }

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    thnks alot doraiashok the code work

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    hi, again
    when i want delet all freq in list
    e.g
    1->2->1->3->3->4
    the out is
    2->4
    ------
    i add in code
    Code:
    void del_freq(ptr *head){
    	ptr p,q,r;
    	for(p=*head;p!=NULL;p=p->next)
    		for(q=p->next;q!=NULL;q=q->next)
    			if(q->emp.id==p->emp.id){
    				p->next=q->next;
    				r=q->next;
    				free(p);
    				free(q);
    				p=r;
    				q=p;
    				
    			}
     
    }
    what is worng now?

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    Input:
    1->2->1->3->2->1->4

    Output:
    3->4

    The code below does above,

    Code:
    void del_freq(ptr *head)
    {
            ptr p,q,r;
            for(p=*head;p!=NULL;p=p->next)
            {
                  r=NULL;
                  for(q=p->next;q!=NULL;q=q->next)
                  {
                         if(q->emp.id==p->emp.id)
                         {
                                p->next=q->next;
    	                    free(q);
    	                    q=p;
                                r=p;
    	             }
                  }
                  if(r!=NULL)
                         free(r);
           }
    }
    I guess above works.

    Error in your code:

    Code:
    free(p);
    free(q);
    Above code clears two occurences (mulitples of 2) so if the number of occurence is odd then one occurence will be left out.

  8. #8
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    the code not work i add something and still not work
    Code:
    void del_freq(ptr *head)
    {
            ptr p,q,r;
            for(p=*head;p!=NULL;p=p->next)
            {
                  r=NULL;
                  for(q=p->next;q!=NULL;q=q->next)
                  {
                         if(q->emp.id==p->emp.id)
                         {
                                p->next=q->next;
    	                    free(q);
    	                    q=p;
                            r=p;
    	             }
                  }
                  if(r!=NULL){
    				  p=p->next;//to keep the p before freed
    				  free(r);// r and p deleted becuse in after r=p;
    			  r=p;
    			  }
           }
    }

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    Sorry, I forgot that but if you do this, the p = p->next in the outer for-loop has to be removed. So the code now looks as below,

    Code:
    void del_freq(ptr *head)
    {
            ptr p,q,r;
            for(p=*head;p!=NULL; )
            {
                  r=NULL;
                  for(q=p->next;q!=NULL;q=q->next)
                  {
                   	      	if(q->emp.id==p->emp.id)
                   		{
                                p->next=q->next;
    	                    free(q);
    	                    q=p;
                                r=p;
                         	}
                  }
                  if(r!=NULL)
                  {
    			p=p->next; 
    			free(r);
    			//  r=p; // Not required since it becomes NULL
                  }
                  else
                  {
     			p=p->next;
                  }
           }
    }
    Hope it works.

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    the code did not work.....i do not know why????
    i tried many times but it did not work????

  11. #11
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    Could you please attach the entire code. I will try to debug it and give you back.

    Thanks.

  12. #12
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    ok......this is all code but a have another trouble in last function
    that function put the Odd id number in the left and put the Even id in right
    e.g
    1->2->3->4->5
    out put is
    1->3->5->2->4
    -------------------
    this is the code for it
    Code:
    void add_odd_in_lefe_linked(ptr *head){
    	ptr p,q;
    	data t;
    	if(*head==NULL)
    		printf("sorry no linked list to sort\n");
    	else{
    	p=*head;
    	q=(*head)->next;
    	for(p=*head;p!=NULL;p=p->next){
    		for(q=p->next;q!=NULL;q=q->next){
    			if((q->emp.id%2)!=0){
    				t=p->emp;
    				p->emp=q->emp;
    				q->emp=t;
    			}
    		}
    		p=*head;
    		q=(*head)->next;
    	}
    ----------------------------------------------------------------------------------
    and this all my program
    Code:
    /*********** lap9 - linked list ***********/
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    /*********** Data ***********/
    typedef struct {
    	int id;
    	char name[60];
    	int age;
    	float salary;
    	char pos[30];
    }data;
    /*********** linkd list ***********/
    typedef struct node{
    	data emp;
    	struct node *next;
    }*ptr,recnode;
    /*********** The functions ***********/
    void menu();
    void Read(data*);
    void insert_first(ptr*,data);
    void insert_last(ptr*,data);
    void insert_mid(ptr*,data);
    int size(ptr);
    void print(ptr);
    void sorted(ptr*);
    void search(ptr,int,ptr*,ptr*);
    void search_By_name(ptr,char[],ptr*,ptr*);
    void delet(ptr*,int,data *);
    void del_first(ptr*,data *);
    void del_last(ptr*,data *);
    void max_salary(ptr,ptr*);
    void rev(ptr*);
    void del_freq(ptr*);
    void add_odd_in_lefe_linked(ptr *);
    /*********** main ***********/
    int main(){
    	int choice,t;
    	data R;
    	ptr head=NULL;
    	ptr cur,pre,p;
    	char a[60];
    	menu();
    	scanf("%d",&choice);
    	while(choice!=15){
    		switch(choice){
    		case 1:
    			Read(&R);
    			insert_first(&head,R);
    			break;
    		case 2:
    			Read(&R);
    			insert_last(&head,R);
    			break;
    		case 3:
    			Read(&R);
    			insert_mid(&head,R);
    			break;
    		case 4:
    			print(head);
    			break;
    		case 5:
    			sorted(&head);
    			break;
    		case 6:
    			del_first(&head,&R);
    				menu();
    			break;
    		case 7:
    			del_last(&head,&R);
    			menu();
    			break;
    		case 8:
    			printf("Eneter the id you want to delet :");
    			scanf("%d",&t);
    			delet(&head,t,&R);
    			menu();
    			break;
    		case 9:
    			printf("Enter the id to you finds :");
    			scanf("%d",&t);
    			search(head,t,&cur,&pre);
    			if(cur==NULL)
    				printf("Sorry not found\n");
    			else
    				printf("The Position in the list is %s\n",cur->emp.pos);
    			menu();
    				break;
    		case 10:
    			p=head;
    			max_salary(head,&p);
    			if(p!=NULL)
    			printf("\nThe Id is: %d , Name: %s , Age: %d , Salary: %.2f , Position: %s\n",p->emp.id,p->emp.name,p->emp.age,p->emp.salary,p->emp.pos);
    			menu();
    			break;
    		case 11:
    			rev(&head);
    			menu();
    			break;
    		case 12:
    			printf("Enter The name you want to change Id: ");
    			getchar();
    			gets(a);
    			search_By_name(head,a,&cur,&pre);
    			if(cur==NULL)
    				printf("Sorry not found the name\n");
    			else{
    				printf("\n");
    				printf("Enter the new id :");
    				scanf("%d",&cur->emp.id);}
    			menu();
    				break;
    		case 13:
    			del_freq(&head);
    			menu();
    			break;
    		case 14:
    			add_odd_in_lefe_linked(&head);
    			menu();
    			break;
    		default:
    			printf("Erorr choice\n");
    			menu();
    			break;
    		}
    		scanf("%d",&choice);
    	}
    	return 0;
    }
    /*********** menu ***********/
    void menu(){
    	printf("+------------------------------------------------------+");
    	printf("\n|                | The main menu |                     |");
    	printf("\n+------------------------------------------------------+");
    	printf("\n| 1- Inserts a new employee in the head of list        |");
    	printf("\n| 2- Inserts a new employee in the last.               |");
    	printf("\n| 3- Inserts a new employee in the middle of the list. |");
    	printf("\n| 4- Print all the employees and thir attributes.      |");
    	printf("\n| 5- Sorts the employees according to thir salaries.   |");
    	printf("\n| 6- Delet a first employee.                           |");
    	printf("\n| 7- Delet a last employee.                            |");
    	printf("\n| 8- Delet a given employee.                           |");
    	printf("\n| 9- Finds a given employee and returns his position.  |");
    	printf("\n| 10- Prints the with maximum salary.                  |");
    	printf("\n| 11- Revers the linked list.                          |");
    	printf("\n| 12- chang Id.                                        |");
    	printf("\n| 13- Delet Frequency By Id.                           |");
    	printf("\n| 14- Add Odd In Left of Linked List.                  |");
    	printf("\n| 15- End.                                             |");
    	printf("\n+------------------------------------------------------+");
    	printf("\nEnter your choice :");
    }
    /*********** Read Record ***********/
    void Read(data *R){
    	printf("\n+-------------------------------+");
    	printf("\n|   Enter the info of employees |");
    	printf("\n+-------------------------------+");
    			printf("\nEnter the id number :");
    			scanf("%d",&R->id);
    			printf("\n");
    						printf("Enter the name (first - middle -last):");
    			scanf("%s",R->name);
    			printf("\n");
    			getchar();
    			printf("Enter the age :");
    			scanf("%d",&R->age);
    			printf("\n");
    			printf("Enter the salary :");
    			scanf("%f",&R->salary);
    			printf("\n");
    			printf("Enter the position :");
    			scanf("%s",R->pos);
    			printf("\n");
    			getchar();
    			menu();
    }
    /*********** insert_first ***********/
    void insert_first(ptr *head,data R){
    	ptr p;
    	p=(struct node*)malloc(sizeof(recnode));
    	p->emp=R;
    	if(*head==NULL){
    		*head=p;
    		(*head)->next=NULL;
    	}
    	else{
    		p->next=*head;
    		*head=p;
    	}
    }
    /*********** Print linkedlist ***********/
    void print(ptr head){
    	while(head!=NULL){
    		printf("\nThe Id is: %d , Name: %s , Age: %d , Salary: %.2f , Position: %s",head->emp.id,head->emp.name,head->emp.age,head->emp.salary,head->emp.pos);
    		head=head->next;
    	}
    	printf("\n");
    	menu();
    }
    /*********** insert_last ***********/
    void insert_last(ptr *head,data R){
    	ptr p,q;
    	p=(struct node*)malloc(sizeof(recnode));
    	p->emp=R;
    	p->next=NULL;
    	if(*head==NULL)
    		*head=p;
    	else{
    		q=*head;
    		while(q->next!=NULL)
    			q=q->next;
    		q->next=p;
    	}
    }
    /*********** size ***********/
    int size(ptr head){
    	int n=0;
    	while(head!=NULL){
    		n++;
    		head=head->next;
    	}
    	return n;
    }
    /*********** insert_mid ***********/
    void insert_mid(ptr*head,data R){
    	ptr p,q,r;
    	int n,k;
    	n=size(*head);
    	p=(struct node*)malloc(sizeof(recnode));
    	p->emp=R;
    	if(*head==NULL){
    		*head=p;
    		(*head)->next=NULL;
    	}
    	else if((*head)->next==NULL){
    		(*head)->next=p;
    		p->next=NULL;
    	}
    	else{
    		k=n/2;
    		for(q=*head;k>0;k--)
    			q=q->next;
    		k=(n/2)-1;
    		for(r=*head;k>0;k--)
    			r=r->next;
    		p->next=q;
    		r->next=p;
    	}
    }
    /*********** sort ***********/
    void sorted(ptr *head){
    	ptr p,q;
    	data t;
    	int n,j,i;
    	if(*head==NULL)
    		printf("sorry no linked list to sort\n");
    	else{
    	p=*head;
    	q=(*head)->next;
    	n=size(*head);
    	for(i=0;i<n-1;i++){
    		for(j=i+1;j<n;j++){
    			if((p->emp.salary)<(q->emp.salary)){
    				t=p->emp;
    				p->emp=q->emp;
    				q->emp=t;
    			}
    			q=q->next;
    			p=p->next;
    		}
    		p=*head;
    		q=(*head)->next;
    	}}
    	menu();
    }
    /*********** search ***********/
    void search(ptr head,int target,ptr *cur,ptr *pre){
    	*cur=*pre=NULL;
    	while((head!=NULL) && (*cur==NULL)){
    		if((head->emp.id)==target)
    			*cur=head;
    		else{
    			*pre=head;
    			head=head->next;
    		}
    	}
    }
    /*********** Delet ***********/
    void delet(ptr *head,int t,data *Z){
    	ptr cur,pre;
    	search(*head,t,&cur,&pre);
    	if(cur==NULL)
    		printf("Sorry the id :%d not found \n",t);
    	else{
    		if(cur==*head)
    			del_first(head,Z);
    		else{
    			if(cur->next==NULL)
    			del_last(head,Z);
    		else{
    			pre->next=cur->next;
    			*Z=cur->emp;
    			free(cur);
    		}
    	}
    }
    }				
    /*********** Delet First ***********/
    void del_first(ptr *head,data *Z){
    	ptr p;
    	if(*head==NULL)
    		printf("Sorry the linked list is empty\n");
    	else{
    		if((*head)->next==NULL){
    			*Z=(*head)->emp;
    			free(*head);
    		}
    		else{
    			p=*head;
    			*Z=p->emp;
    			*head=(*head)->next;
    			free(p);
    		}
    	}
    }
    /*********** Delet last ***********/
    void del_last(ptr *head,data *Z){
    	
    	ptr p,q;
    	if(*head==NULL)
    		printf("Sorry the linked list is empty\n");
    	else{
    		if((*head)->next==NULL){
    			*Z=(*head)->emp;
    			free(*head);
    		}
    		else{
    			p=*head;
    			q=(*head)->next;
    			while(q->next!=NULL){
    				q=q->next;
    				p=p->next;
    			}
    			*Z=q->emp;
    			p->next=NULL;
    			free(q);
    		}
    	}
    }
    /*********** Max salary ***********/
    void max_salary(ptr head,ptr *cur){
    	ptr p;
    	float max;
    	if(head==NULL)
    		printf("Sorry Empty linked linst\n");
    	else{
    		max=head->emp.salary;
    		p=head->next;
    		while(p!=NULL){
    			if(max<p->emp.salary){
    				max=p->emp.salary;
    				*cur=p;
    			}
    			p=p->next;
    		}
    	}
    }
    /*********** Revers ***********/
    void rev(ptr *head){
    	ptr p,q;
    	data t;
    	int i,n,j;
    	p=*head;
    	q=(*head)->next;
    	n=size(*head);
    	for(i=0;i<n-1;i++){
    		for(j=i+1;j<n;j++){
    			t=p->emp;
    			p->emp=q->emp;
    			q->emp=t;
    			q=q->next;
    			p=p->next;
    		}
    		q=(*head)->next;
    		p=*head;
    	}
    }
    /*********** search ***********/
    void search_By_name(ptr head,char a[],ptr *cur,ptr *pre){
    	*cur=*pre=NULL;
    	while((head!=NULL) && (*cur==NULL)){
    		if(strcmp(head->emp.name,a)==0)
    			*cur=head;
    		else{
    			*pre=head;
    			head=head->next;
    		}
    	}
    }
    /*********** Delet Frequency By Id***********/
    void del_freq(ptr *head)
    {
            ptr p,q,r;
            for(p=*head;p!=NULL; )
            {
                  r=NULL;
                  for(q=p->next;q!=NULL;q=q->next)
                  {
    				  if(q->emp.id==p->emp.id)
                   		{
                            p->next=q->next;
    	                    free(q);
    	                    q=p;
    						r=p;
                         	}
                  }
                  if(r!=NULL)
                  {
    			p=p->next; 
    			free(r);
    		//	r=p; // Not requird since it becomes NULL
                  }
                  else
                  {
     			p=p->next;
                  }
           }
    }
    
    
    /*********** Add Odd In Left of Linked List***********/
    void add_odd_in_lefe_linked(ptr *head){
    	ptr p,q;
    	data t;
    	if(*head==NULL)
    		printf("sorry no linked list to sort\n");
    	else{
    	p=*head;
    	q=(*head)->next;
    	for(p=*head;p!=NULL;p=p->next){
    		for(q=p->next;q!=NULL;q=q->next){
    			if((q->emp.id%2)!=0){
    				t=p->emp;
    				p->emp=q->emp;
    				q->emp=t;
    			}
    		}
    		p=*head;
    		q=(*head)->next;
    	}
    	menu();
    }
    }
    /*********** End ***********/
    Attached Files Attached Files

  13. #13
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    This code will work.

    Code:
    void del_freq(ptr *head)
    {
      ptr p,q,prev,prev1;
      int flag = 0;
      for(p=*head;p!=NULL;p=p->next )
        {
          flag=0;
          prev=p;
          for(q=p->next;q!=NULL;q=q->next)
    	{
    	  if(q->emp.id == p->emp.id)
    	    {
    	      flag = 1;
    	      prev->next = q->next;
    	      free(q);
    	      q=prev;
    	    }
    	  prev=q;
    	}
          
          if(flag == 1)
    	{
    	  if(*head == p)
    	    {
    	      *head = p->next;
    	      free(p);
    	      p=*head;
    	    }
    	  else
    	    {
    	      prev1->next = p->next;
    	      free(p);
    	      p=prev1;
    	    }
    	}
          prev1=p;
        }
    }
    I have also attached the changed file. I tried the program using g++ compiler and it perfectly works.

    Thanks.
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    yeeees its worked thaanks alot
    --------------------------------------------
    and what about this

    that function put the Odd id number in the left and put the Even id in right
    e.g
    1->2->3->4->5
    out put is
    1->3->5->2->4
    -------------------
    this is the code for it
    Code:
     void add_odd_in_lefe_linked(ptr *head){
    	ptr p,q;
    	data t;
    	if(*head==NULL)
    		printf("sorry no linked list to sort\n");
    	else{
    	p=*head;
    	q=(*head)->next;
    	for(p=*head;p!=NULL;p=p->next){
    		for(q=p->next;q!=NULL;q=q->next){
    			if((q->emp.id%2)!=0){
    				t=p->emp;
    				p->emp=q->emp;
    				q->emp=t;
    			}
    		}
    		p=*head;
    		q=(*head)->next;
    	}
    
    }
    }

  15. #15
    Registered User
    Join Date
    Dec 2003
    Posts
    24
    The code below will work, Try it.

    Code:
    void add_odd_in_lefe_linked(ptr *head)
    {
    	ptr p,q,r,prev;
    	for(q=(*head);q->next!=NULL;q=q->next);
    	//q contains the last element
    	prev=NULL;
    	for(p=(*head);p!=NULL; )
    	{
    		if(p->emp.id%2 == 0 && p->next!=NULL)
    		{
    			for(r=p->next;r!=NULL;r=r->next)
    			{
    				if(r->emp.id%2 != 0)
    				{
    					break;
    				}
    			}
    			if(r==NULL)
    			{
    				break;
    			}
    
    			if(prev == NULL)
    			{
    				*head=p->next;
    				p->next=NULL;
    				q->next=p;
    				p=*head;
    			}
    			else
    			{
    				prev->next=p->next;
    				p->next=NULL;
    				q->next=p;
    				p=prev;
    				p=p->next;
    			}
    			for(q=(*head);q->next!=NULL;q=q->next);
    		}
    		else
    		{
    			prev=p;
    			p=p->next;
    		}
    	}
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21