Thread: LinkedList

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    2

    LinkedList

    Hi, im trying to modify this code to have both forward and backward links

    Code:
    /*
     ============================================================================
     Name        : LinkedList.c
     Author      : 
     Version     :
     Copyright   : Your copyright notice
     Description : Hello World in C, Ansi-style
     ============================================================================
     */
    
    
    # include<stdio.h>
    # include<conio.h>
    # include "malloc.h"
    
    
    struct node {
    	int data;
    	struct node *link;
    };
    
    
    void main() {
    	int a=111,b=2,c=3,will,wish,num;
    	struct node *ptr,*ptr2,*result,*temp;
    	void add(struct node **,int );
    	struct node * search(struct node *);
    	void display(struct node *);
    	void invert(struct node *);
    	void del(struct node *,int);
    	struct node * concat(struct node *,struct node *);
    	ptr=NULL;
    	ptr2=NULL;
    	result=NULL;            //result for storing the result of concatenation
    	clrscr();
    	will=1;
    
    
    	while(will==1) {
    		printf("Main Menu\n 1. Add element\n 2.Delete element \n 3.Search element\n");
            printf("4. Linked List concatenation\n 5.Invert linked list\n 6. Display elements\n");
            printf("Please enter the choice");
            scanf("%d",&wish);
    
    
    		switch(wish) {
    			case 1:
    				printf("Enter the element you want to add   ");
    				scanf("%d",&num);
    				add(&ptr,num);
    				display(ptr);
    				break;
    
    
    			case 2:
    				printf("Enter the element to delete ");
    				scanf("%d",&num);
    				del(ptr,num);
    				break;
    
    
    			case 3:
    				printf("Now demonstrating search ");
    				temp = search(ptr);
    				printf("Address of first occurence is  %u ",temp);
    				break;
    
    
    			case 4:
          /* Inputs given internally for demo only */
    				printf(" Now demonstrating linked list concatenation Press any key to continue...");
    				add(&ptr2,2);
    				add(&ptr2,4);
    				add(&ptr2,6);
    				getch();
    				printf("Displaying second Linked List");
    				display(ptr2);
    				getch();
    				result = concat(ptr,ptr2);
    				clrscr();
    				printf("Now Displaying the result of concatenation");
    				display(result);
    				getch();
    				break;
    
    
    			case 5:
    
    
    				printf("Inverting the list ...Press any key to continue...");
    				invert(ptr);
    				break;
    
    
    			case 6:
    				display(ptr);
    				break;
    				default:printf("Illegal choice");
    		}
    
    
    		printf("DO you want to continue ( press 1 for yes ");
    		scanf("%d",&will);
    	}       //end of while
    }
    
    
    
    
    void add(struct node **q,int num) {
    	struct node *temp;
    	temp = *q;
    	if(*q==NULL) {
          *q=malloc(sizeof(struct node));
          temp = *q;
    	}
    
    
    	else {
          while((temp->link)!=NULL) {
                  temp=temp->link;
          }
    
    
          temp->link = malloc(sizeof(struct node));
          temp=temp->link;
    	}
    
    
    	temp->data = num;
    	temp->link  = NULL;
    }
    
    
    void display(struct node *pt) {
    
    
    	while(pt!=NULL)
    	{
    
    
          printf("Data : %d",pt->data);
          printf("Link : %d",pt->link);
          pt=pt->link;
    	}
    }
    
    
    
    
    void invert(struct node *ptr) {
    
    
    	struct node  *p,*q,*r;
    	p=ptr;
    	q=NULL;
    
    
    	while(p!=NULL) {
          r=q;
          q=p;
          p=p->link;
          q->link=r;
    	}
    
    
    	ptr = q;
    	display(ptr);
    }
    
    
    
    
    //      CONCATENATION OF LINKED LISTS
    
    
    struct node * concat(struct node *p,struct node *q) {
    	struct node *x,*r;
    	if (p==NULL)
    		r=q;
    
    
    	if (q==NULL)
    		r=p;
    
    
    	else {
        x=p;
        r=x;
        while(x->link!=NULL)
           x=x->link;
        	x->link=q;
    	}
    	return(r);
    }
    
    
    
    
    // SEARCHING AN ELEMENT IN THE LINKED LIST
    // THIS FUNCTION FINDS THE FIRST OCCURENCE OF
    // A DATA AND RETURNS A POINTER TO ITS ADDRESS
    
    
    struct node * search(struct node *p) {
    	struct node *temp;
    	int num;
    	temp = p;
    	printf("Enter the data that you want to search    ");
    	scanf("%d",&num);
    	printf("Link of temp %u", temp->link);
    
    
    	while(temp->link!=NULL) {
          printf("In while ");
          if(temp->data == num)
          return(temp);
          temp=temp->link;
        }
    
    
    	return(NULL);
    }
    
    
    
    
    
    
    // DELETING DATA FROM THE LINKED LIST//
    
    
    void del(struct node *p,int num) {
    
    
    	struct node *temp,*x;
    	temp=p;
    	x= NULL;
    
    
    	while (temp->link !=NULL) {
    		if(temp->data == num) {
    
    
    			if (x==NULL) {
    				p = temp->link;
    				free(temp);
    				return;
    			}
    
    
    			else {
    				x->link = temp->link;
    				free(temp);
    				return;
    			}
    		}                  //end of outer if
    
    
    		x=temp;
    		temp=temp->link;
    	}
    	//end of while
    	printf("No such entry to delete ");
    }       //end of fn.
    Last edited by DenDen Bata; 05-02-2012 at 01:05 PM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Then I imagine you'll start by inserting something somewhere around line 19.

    You're better off starting it first and then posting when you have a problem.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    2
    thats the thing... i dont know how to start.. im super stuck. plss help

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you cant make a start then you mustn't know how to make singly-linked lists either.

    I would start by learning how to make a singly-linked-list. You have to show some effort for us to be able to help you, in accordance with the rules on this site. I suggest you show some effort by searching for a linked-list tutorial, and demonstrate that you've acquired some knowledge about the subject.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Link to the likely original source of the code.
    C sample code - Program to demonstrate linked list operations - Source code examples

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LinkedList implementation
    By rire1979 in forum C Programming
    Replies: 1
    Last Post: 12-19-2009, 02:39 PM
  2. [help] - adding to end of linkedlist
    By Springy in forum C Programming
    Replies: 3
    Last Post: 10-18-2009, 02:35 AM
  3. Help LinkedList!!
    By dagbai in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 12:56 PM
  4. LinkedList
    By bejiz in forum C++ Programming
    Replies: 1
    Last Post: 06-12-2006, 04:26 AM
  5. A linkedlist that has no end
    By Jslam9 in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2002, 05:43 PM