1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

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

    Your code is tooo long ..
     
  3. shabbir

    shabbir Administrator Staff Member

    Thanks for counting.
     
  4. Programming_Kills

    Programming_Kills New Member

    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

    These codes very helpful for me....
     
  6. Jessica2

    Jessica2 New Member

    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