Member Avatar for RenFromPenn

Hi,

I need to create a menu of items that the user can select from. These choices will either create a linked list, add an item to the linked list, modify a current entry in the list, delete an entry, display the list, or exit the program.

I have looked every where for some examples to help me with this and I have found nothing. I googled. I searched c tutorials. I looked every where that I could think of with no luck. Does anyone know of a few good examples that I could look at to try and piece this thing together?

Thanks!

Ren

And your point being what?
That sometimes no matter how much you google, that you can't find a ready-rolled answer?

What ever happened to trying your own homework.

I imagine the point is to see who's learnt something, not who's got the best google skills.

Member Avatar for RenFromPenn

And your point being what?
That sometimes no matter how much you google, that you can't find a ready-rolled answer?

What ever happened to trying your own homework.

I imagine the point is to see who's learnt something, not who's got the best google skills.

No, my point wasn't that I wanted the answer. My point was that I wanted examples and tutorials because I can't find anything helpful and the book isn't helping me to figure this out.

I never said that I wanted the answer handed to me. I just wanted some help.

Have you first tried to learn how linked lists work?
Here.

Member Avatar for RenFromPenn

Have you first tried to learn how linked lists work?
Here.

Yeah, I've read the book and I've read explanations online. The book has some sample programs for you to mess about with, but nothing like the the one that I described. I just really need a good tutorial to help me get my head around this. Surely there has to be a site that talks about linked lists, user selections, and that offers exercises so that I could get some practice.

Oh come on, any half-assed attempt at a linked list tutorial should tell you how to insert and delete an element.

As for the rest of the "problem", that's just generic menu selection consisting of
- print a number of choices (printf)
- input a choice (scanf)
- do the thing associated with the choice (if/else or a switch/case)

Now, what exactly is it that you're stuck on?

> I just wanted some help.
And how exactly are we supposed to know what you need help with, unless you post some kind of effort?

Because unless you do, then anything we do for you is precisely spoon-feeding, and something which you're claiming you don't want.

this example will help you to understand your query

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>

struct element
{
   int info;
   struct element *next;
}*start,*temp;

typedef struct element node;

void create();
void insert();
void dele();
void display();

void main()
{
	int ch;
	clrscr();

	while(ch!=5)
	{
		printf("\nEnter Your Choice");
		printf("\n1.Create\n2.Insert\n3.Delete\n4.Display\n5.Exit");
		scanf("%d",&ch);


			switch(ch)
			{
			case 1:
				create();
				break;
			case 2:
				insert();
				break;
			case 3:
				dele();
				break;
			case 4:
				display();
				break;
			case 5:
				exit(0);

			}

       }


}

void create()
{
	int ch1;
	node *list,*newnode;
	clrscr();

	printf("Enter The Element(For Exit Press -1):= ");
	scanf("%d",&ch1);

	while(ch1!=-1)
	{

	newnode=(node *)malloc(sizeof(node));

	if(start==NULL)
	{
	start=newnode;
	}

	newnode->info=ch1;
	newnode->next=NULL;

	list->next=newnode;
	list=newnode;

	printf("\nEnter The Element(For Exit Press -1):= ");
	scanf("%d",&ch1);

       }


}

void insert()
{
	char ch;
	int newinfo,posi,i=2;
	node *prev,*newnode;

	printf("\nEnter The Element:= ");
	scanf("%d",&newinfo);

	do
	{
		printf("\nEnter The Position:= ");
		scanf("%d",&posi);

		if(posi<1)
		{

		printf("\nInvalid Position");

		}

	}while(posi<1);

	newnode=(node *)malloc(sizeof(node));
	newnode->info=newinfo;

	if((posi==1) || (start==NULL))
	{
		newnode->next=start;
		start=newnode;
	}
       else
       {
		prev=start;

		while(i < posi )
		{

		prev=prev->next;
		i++;

		}

		newnode->next=prev->next;
		prev->next=newnode;
      }

}

void dele()
{
	int deleinfo;
	node *prev,*list;

	printf("Enter The Element:= ");
	scanf("%d",&deleinfo);

	if(start==NULL)
	{
	   printf("List is Empty");
	   getch();
	}
	else
	{
	   prev=NULL;
	   list=start;

		while((list->info!=deleinfo) && (list->next!=NULL))
		{

		prev=list;
		list=list->next;

		}

       }

	if(list->info!=deleinfo)
	{
	  printf("Element Not Found");
	}

	else
      {
	if(prev==NULL)
	{
	  //temp=temp->next;
	  start=start->next;
	}

	else
	{
	  prev->next=list->next;
	}
     }


}

void display()
{
	temp=start;
	while(temp->next!=NULL)
	{
	   printf("%d->",temp->info);

	   temp=temp->next;
	}

	printf("%d",temp->info);


}
commented: After eleven posts, you should know how to use the proper code tags -2

>this example will help you to understand your query
What makes you think, that broken code is going to be of any help to anyone?

Obviously, you have chosen to not read about the expected conduct for this forum. Please, do, here and here, with an example here.

If you still, choose to ignore them, abstain of posting; it will be the best help you can give.

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.