this is my code for a simple link list...i cant solve the problem of the while loop.it is only working for "1".and i cant swap two nodes in the list and cant insert nodes in a given position...how can i do those???plz help....

//Pirateā„¢
#include<stdio.h>
#include<string.h>
#include<stdlib.h>



struct list_element
{
	char name[80];

	struct list_element* next;
};
typedef struct list_element node;


int menu();

void create(node *first);
void display(node *first);

node *insert_first(node *start);
void insert_last(node *start);
void insert_after(node *first,char *after);
void insert_before(node *first,char *before);

void remove_after(node *first,char *after);
void remove_before(node *first,char *before);

//void swap(node *first,char *name1,char *name2);


void main()
{
	node *start;
int i;
do
{
	i=menu();
	char before[80];
	char after[80];

	switch(i)
	{
	case 1:
		//create
		printf("......CREATING.....\n");
		start=(node *)malloc(sizeof (node));
		create(start);
		display(start);
		continue;
	case 2:
		//inserting first
		printf("......INSERTING FIRST......\n");
		start=insert_first(start);
		display(start);
		continue;
	case 3:
		//inserting last
		printf("......INSERTING LAST......\n");
		insert_last(start);
		display(start);
		continue;
	case 4:
		//inseting before a node
		//printf("........INSERTING BEFORE ::ARAFAT:: .......\n");
		printf("before which?");
		gets(before);
		insert_before(start,before);
		display(start);
		continue;
	case 5:
		//inserting after a node
		//printf(".....INSERTING AFTER THE NAME ::ARAFAT::  .....\n");
		printf("after which:");
		gets(after);
		insert_after(start,after);
		display(start);
		continue;
	case 6:
		//removing before
		//printf(".....REMOVING BEFORE ::ARAFAT::.....\n");
		printf("remove before which?");
		gets(before);
		remove_before(start,before);
		display(start);
		continue;
	case 7:
		//removing after
		//printf(".....REMOVING AFTER.....\n");
		printf("remove after wnich?");
		gets(after);
		remove_after(start,after);
		display(start);
		continue;
	case 8:
		exit(1);
	

	}
}while(i!=8);	
	printf("\n");
}

int menu()
{
	int choice;

	printf("enter your choice:\n");
	printf("1-for creatin\n");
	printf("2-for inserting in first\n");
	printf("3-for inserting in last\n");
	printf("4-for inserting before\n");
	printf("5-for inserting after\n");
	printf("6-for removing before\n");
	printf("7-for removing after\n");
	printf("8-for exit\n");
	
	scanf("%d",&choice);

	return choice;
}
void create(node *first)
{
	printf("enter the name:");
	fflush(stdin);
	gets(first->name);
	if(!strcmp(first->name,"end"))
	{
		first->next=NULL;
	}
	else 
	{
		first->next=(node *)malloc(sizeof(node));
		create(first->next);
	}
}

void display(node *first)
{
	if(first->next!=NULL)
	{
		printf("%s\n",first->name);
		display(first->next);

	}

}


node *insert_first(node *first)
{
	node *newnode;
	newnode=(node *)malloc(sizeof(node));
	printf("enter name:");
	gets(newnode->name);


	newnode->next=first;
	first=newnode;

	return first;
}

void insert_after(node *first,char *after)
{
	node *tag;
	
	if(first->next->next==NULL)
	{
		printf("name not in list");

		
	}
	
	else if(strcmp(first->name,after)==0)
	{
		tag=first;
		node *newnode;
		newnode=(node *)malloc(sizeof(node));
		printf("enter name: ");
		gets(newnode->name);

		newnode->next=tag->next;
		tag->next=newnode;

	}
		
	else
	{
		insert_after(first->next,after);
	}
}


void insert_before(node *first,char *after)
{
	node *tag;
	
	if(first->next==NULL)
	{
		printf("name not in list");

		
	}
	
	else if(strcmp(first->next->name,after)==0)
	{
		tag=first;
		node *newnode;
		newnode=(node *)malloc(sizeof(node));
		printf("enter name: ");
		gets(newnode->name);

		newnode->next=tag->next;
		tag->next=newnode;
	}

	else
	{
		insert_before(first->next,after);
	}
}

void insert_last(node *first)
{
	if(first->next->next==NULL)
	{
		node *newnode;
		newnode=(node *)malloc(sizeof(node));
		printf("enter name:");
		gets(newnode->name);


		newnode->next=first->next;
		first->next=newnode;

		

	}
	else
	{
		insert_last(first->next);
	}

}


void remove_after(node *first,char *after)
{
	if(strcmp(first->name,after)==0)
	{
		node *temp;
		temp=first->next->next;
		free(first->next);
		first->next=temp;
		
				
	}
	else
	{
		remove_after(first->next,after);
	}
}


void remove_before(node *first,char *before)
{
	if(strcmp(first->next->next->name,before)==0)
	{
		node *temp;
		temp=first->next->next;
		free(first->next);
		first->next=temp;
		
				
	}
	else
	{
		remove_before(first->next,before);
	}

}

first off, function create() is generating nodes that are never used. Let main() pass a pointer to the head of the linked list and let create() malloc it.

void create( node** head)
{
    node* newnode = malloc(sizeof(node));
    newnode->next = NULL;
    if(*head == NULL)
          *head = node;
    else
    {
          // find the tail of the linked list
           node* n = *head;
           while( n->next != NULL)
               n = n->next;
           // insert new node at the tail of the list
           n->next = newnode;
   }
}

int main()
{
    node* head = NULL;
    create( &head );
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.