Basic operations in Linked List

Discussion in 'C' started by shabbir, Aug 27, 2006.

  1. I thought of sharing the code snippet. It just does the basic operations like inserting a node at the end of the linked list and deletion of any particular node in the linked list. The deletion of a node in linked list is based on the data of the node and not on the index at which data is located.
    Code:
    //Basic Linked list operation
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*
     *
     * Program for single linked list which inserts / deletes
     * data into linked list. 
     *
     */
    
    // Structure templates
    struct list
    {
    	int no;
    	struct list *shortlink;
    };
    typedef struct list node;
    
    node *head = NULL;
    
    void init(node*);
    void InsertNode();
    void DeleteNode();
    void DisplayList();
    
    void main()
    {
    	char ch = 'n';
    	int opt;
    	do
    	{
    		printf("\nEnter your option\n");
    		printf("\n1. Insert a node\n");
    		printf("\n2. Delete a particular node\n");
    		printf("\n3. Display all the nodes\n");
    		printf("\n4. Exit the application\n");
    		scanf("%d",&opt);
    		switch(opt)
    		{
    		case 1:
    			InsertNode();
    			DisplayList();
    			break;
    		case 2:
    			DeleteNode();
    			DisplayList();
    			break;
    		case 3:
    			DisplayList();
    			break;
    		case 4:
    			ch = 'y';
    			break;
    		}
    	}while(ch != 'y');
    	printf("\nDone by \"SHABBIR\"\n");
    	getch();
    }
    
    void init(node *current)
    {
    	printf("\nEnter number\n");
    	scanf("%d",&current->no);
    	current->shortlink = NULL;
    }
    
    void InsertNode()
    {
    	node *newnode;
    	
    	newnode=(node*)malloc(sizeof(node));
    
    	init(newnode);
    
    	if(head == NULL)
    	{
    		head = newnode;
    	}
    	else
    	{
    		node *current = head;
    
    		// Move to the end of the linked list
    		while(current->shortlink!=NULL)
    		{
    			current=current->shortlink;
    		}
    		if(current->shortlink==NULL)
    		{
    			newnode->shortlink = current->shortlink;
    			current->shortlink = newnode;
    		}
    	}
    }
    
    void DeleteNode()
    {
    	node* current = head;
    	int rno;
    	node *newnode,*temp;
    
    	printf("\nEnter the number whose node you want to delete\n");
    	scanf("%d",&rno);
    	newnode=current;
    
    	if(current->no==rno)
    	{
    		// Deleting the first node
    		newnode=current;
    		current=current->shortlink;
    		free(newnode);
    		head = current;
    		return;
    	}
    	else
    	{
    		while(newnode->shortlink->shortlink!=NULL)
    		{
    			// Checking condition for deletion of
    			// all nodes except first and last node
    			if(newnode->shortlink->no==rno)
    			{
    				temp=newnode->shortlink;
    				newnode->shortlink=newnode->shortlink->shortlink;
    				free(temp);
    				head = current;
    				return;
    			}
    			newnode=newnode->shortlink;
    		}
    		if(newnode->shortlink->shortlink==NULL && newnode->shortlink->no==rno)
    		{
    			// Checking condition for deletion of last node
    			free(newnode->shortlink);
    			newnode->shortlink=NULL;
    			head = current;
    			return;
    		}
    	}
    }
    
    void DisplayList()
    {
    	node* current = head;
    
    	while(current!=NULL)
    	{
    		printf("\n %d ",current->no);
    		current=current->shortlink;
    	}
    }
    
    You can improve on the code by making the DeleteNode function independent on data stored and making it based on which index needs deletion.
     
  2. sreeramu

    sreeramu New Member

    Joined:
    Oct 18, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Your code is tooo long ..
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Thanks for counting.
     
  4. Programming_Kills

    Programming_Kills New Member

    Joined:
    Jun 14, 2010
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    thank you very much...
    please shabir if you can do all these while reading data from a file please help me.
     
  5. seangtz

    seangtz New Member

    Joined:
    Jun 6, 2008
    Messages:
    126
    Likes Received:
    3
    Trophy Points:
    0
    These codes very helpful for me....
     
  6. Jessica2

    Jessica2 New Member

    Joined:
    Aug 18, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Break down the required features into separate classes then have your main loop initialize and cycle through the interpreter instructions until end-of-file. Writing a full parser would be overkill so a simple main loop should be sufficient. Separate classes to encapsulate all file activity, manipulate the symbol table, stack and another for "interpreting" instructions/evaluation model might be best.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice