Link List

This is a discussion on Link List within the C Programming forums, part of the General Programming Boards category; Hi! I have some problems with this prgram on link list.I am unable to insert and delete nodes. I also ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    8

    Link List

    Hi!
    I have some problems with this prgram on link list.I am unable to insert and delete nodes. I also want to know how to display the items in a link list. I suspect there may be pointer problem but am unable to correct it.

    I am grateful for your help.
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    Code:
    void InsertInFront(NodePointer Head, int Num)
    {
    	NodePointer NewNode;
    	NewNode=(NodePointer)malloc(sizeof(struct Node));
    	NewNode->Data=Num;
    	NewNode->Link=Head;
    	Head=NewNode;
    }
    There is a problem with your insert function. At the end you set Head equal to the new node, but this change does not affect the Head declared in main at all.
    Change the function to return a NodePointer, and do "return (NewNode);"
    The call would then look like this

    Head = InsertInFront(Head,number);

    Code:
    int DeleteFront(NodePointer Head)
    {
    	NodePointer Temp;
    	int num;
    	if(Head==NULL) {
    		printf("The list is empty.Cannot delete.");
    		return ;
    	}
    
    	while(Head->Link!=NULL)
    	{
    
    		Temp=Head;
    
    		printf ( "%d\n", Temp->Data);
    		Head=Head->Link;
    
    		free(Temp);
    	}
    	return ;
    
    }
    This function is a bit buggy, instead of deleteing the front element, you iterate through the list freeing all the nodes, except for the last one. Try this function.
    Code:
    NodePointer DeleteFront(NodePointer Head)
    {
    	NodePointer Temp;
    	int num;
    	if(Head==NULL) {
    		printf("The list is empty.Cannot delete.");
    		return NULL; /*return the same empty list we recieved */
    	}
    	Temp = Head->Link; /* This will become the new head */
    	free(Head); /*Free the memory */
    	return Temp; /*Return the new head of the list */
    }
    This would also be called like this
    Head = DeleteFront(Head);

  3. #3
    Registered User zahid's Avatar
    Join Date
    Aug 2001
    Posts
    532
    When you are inserting it at the front end then it's pretty okay.

    I guess you have to clear first the pointer then list concept more..

    traveling path pointer=pointer->next
    [ Never code before desk work ]
    -------------------------------------:-->
    A man who fears Nothing is the man who Loves Nothing
    If you Love Nothing, what joy is there in your life.
    =------------------------------------------------------= - I may be wrong.

  4. #4
    Unregistered
    Guest

    this is for dobuol link list

    hi i wesh that i have the singel code BUT sorry,,,
    this is the dobuol operatios like
    insert first
    insert last
    delete first
    delete last
    delete target
    search
    and display.
    /////////////////////////////////////////////////////////////////////////////////


    /////////////// DUOBLE LINK LIST OPERATIONS ///////////////////////

    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    //-------------------------- >>> a structuer <<<
    typedef struct
    {
    int id;
    float gpa;
    }data;
    //-------------------------->>> a defanition of a duoble link list <<<
    typedef struct node
    {
    data e;
    struct node *next;
    struct node *prev;
    }recnode, *ptr;
    //-------------------------->>> insert first in a duoble link list <<<
    void ins_first(ptr *h,data newe)
    {
    ptr p;
    p=malloc(sizeof(recnode));
    p->e=newe;
    p->prev=NULL;

    if(*h==NULL)
    {
    p->next=NULL;
    (*h)=p;
    }
    else
    {
    p->next=*h;
    (*h)->prev=p;
    (*h)=p;
    }
    }
    //-------------------------->>> insert last in a duoble link list <<<
    void ins_last(ptr *h,data newe)
    {

    ptr p,q;
    p=malloc(sizeof(recnode));
    p->e=newe;
    p->next=NULL;
    if((*h)==NULL)
    {
    (*h)=p;
    (*h)->prev=NULL;
    }
    else
    {
    q=*h;
    while(q->next!=NULL)
    q=q->next;
    q->next=p;
    p->prev=q;
    }
    }
    //-------------------------->>> new link <<<
    void new_D(ptr *h)
    {
    *h=NULL;
    }
    //-------------------------->>> sertch for an element <<<
    void sertch(ptr h,int t,ptr *cur)
    {

    *cur=NULL;

    while(h!=NULL && *cur==NULL)
    {
    if(h->e.id==t)
    *cur=h;
    else h=h->next;
    }
    }
    //-------------------------->>> delete first <<<
    void del_first(ptr *h,data *z)
    {
    ptr p;

    if(*h==NULL)printf("\nSory no list...");
    else
    {
    if((*h)->next==NULL)
    {
    *z=(*h)->e;
    free(*h);
    }
    else
    {
    p=*h;
    *z=p->e;
    (*h)=(*h)->next;
    (*h)->prev=NULL;
    free(p);
    }
    }
    }
    //-------------------------->>> delete last <<<
    void del_last(ptr *h,data *z)
    {
    ptr p;

    if((*h)==NULL) printf("\nSory no list...");
    else
    {
    if((*h)->next==NULL)
    {
    *z=(*h)->e;
    free(*h);
    }
    else
    {
    p=*h;
    while(p->next!=NULL)
    p=p->next;
    *z=p->e;
    p->prev->next=NULL;
    free(p);
    }
    }
    }
    //-------------------------->>> delete target <<<
    void del_target(ptr *h, int t, data *z)
    {
    ptr cur;
    sertch(*h,t,&cur);
    if(cur==NULL) printf("\nSory target not found...");
    else
    {
    if(cur==(*h))
    del_first(h,z);
    else
    {
    if(cur->next==NULL)
    del_last(h,z);
    else
    {
    *z=cur->e;
    cur->prev->next=cur->next;
    cur->next->prev=cur->prev;
    free(cur);
    }
    }
    }
    }
    //-------------------------->>> read recored <<<
    void read_rec(data *r)
    {
    printf("\nEnter the ID:");
    scanf("%d",&r->id);
    printf("\nEnter the GPA:");
    scanf("%f",&r->gpa);
    }
    //-------------------------->>> display the link <<<
    void dis(ptr h)
    {
    printf("\n-------------------------------");
    printf("\nID GPA ");
    printf("\n-------------------------------");
    while(h!=NULL)
    {
    printf("\n%d\t\t\t%.2f",h->e.id,h->e.gpa);
    h=h->next;
    }
    }
    //////////////////////////////////////////////////////////////////////////
    //>>>>>>>>>>>>>>>>>>>>>>>>>> MAIN PROGRAMM <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    //////////////////////////////////////////////////////////////////////////
    main()
    {
    int c,t;
    data r;
    ptr h,cur;
    new_D(&h);
    do{
    printf("\n\n\n\n +-------------------------------------------------+");
    printf("\n |(1) insret first. |");
    printf("\n | |");
    printf("\n |(2) insert last. |");
    printf("\n | |");
    printf("\n |(3) delete first. |");
    printf("\n | |");
    printf("\n |(4) delete last. |");
    printf("\n | |");
    printf("\n |(5) delete a gevin id. |");
    printf("\n | |");
    printf("\n |(6) sertch for ID. |");
    printf("\n | |");
    printf("\n |(7) deisplay the list. |");
    printf("\n | |");
    printf("\n |(8) Exit. |");
    printf("\n +-------------------------------------------------+");
    scanf("%d",&c);



    switch(c)
    {

    case 1:read_rec(&r);
    ins_first(&h,r);system("cls");
    break;
    case 2:read_rec(&r);
    ins_last(&h,r);system("cls");
    break;
    case 3:del_first(&h,&r);
    break;
    case 4:del_last(&h,&r);
    break;
    case 5:printf("\nEnter the ID:");
    scanf("%d",&t);
    del_target(&h,t,&r);
    break;
    case 6:printf("\nEnter the ID:"); scanf("%d",&t);
    sertch(h,t,&cur);
    if(cur==NULL)printf("\nno such ID...");
    else
    printf("\nID:%d\t\t\tGPA:%f",cur->e.id,cur->e.gpa);
    break;
    case 7:dis(h);
    break;
    default:printf("\nNO SUTCH CHOIS....TRY AGEN...");
    }
    }while(c!=8);
    system("cls");

    printf("\n\n\n\n\n\n\n\n\n\n THANK YOU FOR USEING MY PROGRAMM...");
    }


    ////////////////////////////////////////////////////////////////////////////////////

    i hope that you find the prolem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. Link List Insert prob
    By Bebs in forum C Programming
    Replies: 8
    Last Post: 12-03-2008, 10:28 PM
  3. reading data from a file - link list
    By peter_hii in forum C++ Programming
    Replies: 7
    Last Post: 10-25-2006, 10:11 AM
  4. compiler build error
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2003, 10:16 AM
  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