#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void addToStart();
void addToEnd();
void printList();
void removeNodeAt();
void createList();
void menu();

int option;

struct node
{
	char cName [50];
	int cNumber;
	char tDescrip [50];
	struct node *next;
}*newnode, *display, *temp, *list, *prev, *head, *tail;

main()
{
	do
	{
		menu();
		switch (option)
		{
		case 1: addToStart();
			break;
		case 2: addToEnd();
			break;
		case 3: printList();
			break;
		case 4: removeNodeAt();
			break;
		case 5: break;
		}
	}while (option !=5);
}

void menu()
{
	printf ("Linked List\n");
	printf ("1. Add a new node to the beginning\n");
	printf ("2. Add a new node to the end\n");
	printf ("3. Print out the entire list\n");
	printf ("4. Remove a node from the list\n");
	printf ("5. Quit program\n");
	printf ("Please select your option\n");
	scanf ("%d", &option);
}

void createList()
{
	list = NULL;
}

void addToStart()
{
	newnode = (struct node*)malloc(sizeof(struct node));

	printf ("Enter the customer name :");
	scanf ("%s", newnode->cName);

	printf ("\nEnter customer number :");
	scanf ("%d", &newnode->cNumber);

	printf ("\nEnter transaction description :");
	scanf ("%s", newnode->tDescrip);

	newnode->next = NULL;
	if(list==NULL)
		list = newnode;
	else if (list != NULL && newnode < list)
	{
		newnode->next = list;
		list = newnode;
	}
	else
	{
		temp = list;
		while (temp != NULL)
		{
			if (newnode > temp)
			{
				prev = temp;
				temp = temp->next;
			}
		}
		newnode->next = prev->next;
		prev->next = newnode;
	}
}


void addToEnd()
{
    newnode = (struct node*)malloc(sizeof(struct node));

	printf ("Enter the customer name :");
	scanf ("%s", newnode->cName);

	printf ("\nEnter customer number :");
	scanf ("%d", &newnode->cNumber);

	printf ("\nEnter transaction description :");
	scanf ("%s", newnode->tDescrip);

	newnode->next = NULL;
	if(list==NULL)
		list = newnode;
	else if (list != NULL && newnode < list)
	{
            newnode->next = NULL;
            if (head == NULL) {
                  head = newnode;
                  tail = newnode;
            } else {
                  tail->next = newnode;
                  tail = newnode;
            }
      }
}

void printList()
{
	system("cls");
	if (list==NULL)
		printf ("\nList is empty");
	else
	{
		display = list;
		printf ("Customer name \t Customer number \t Transaction Description\n");
		while (display != NULL)
		{
			printf ("%s \t\t %d \t\t\t %s", display->cName, display->cNumber, display->tDescrip);
			display = display->next;
		}
	}
	printf ("\n\n");
}

there are showing output of add to start, but when i use the add to end, there are no output coming out,but there are no errors, what happen?

Recommended Answers

All 2 Replies

Can I get a bit more information on the roles of the pointers you have defined? (eg head, tail, etc)

there are showing output of add to start, but when i use the add to end, there are no output coming out,but there are no errors, what happen?

i didnt understand the reason why u compared the new node that is being added with the nodes present..
can i know the reason please

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.