Circular Linked list in C with Append and display options

This is simpler version of Circular linked list program. If you need more options, please check another program http://thecodecracker.com/c-programming/circular-linked-list/.

/*Circular Linked list in C with Append and display options */

#include<stdio.h>
#include<malloc.h>
void append();
void display();
struct node
{
	int a;
	char n[20];
	struct node *next;
}*last;

int main()
{
	int ch;
	do
	{
		printf("\n1.append 2.display 3.Exit : ");
		scanf("%d",&ch);

		switch(ch)
		{
			case 1:
				append();
				break;
			case 2:
				display();
				break;
		}
	}while(ch!=3);
}

void append()
{
	struct node *newnode;
	newnode=(struct node*)malloc(sizeof(struct node));
	if(newnode==NULL)
	{
		printf("\n memory not allocated");
	}
	printf("enter the val : ");
	scanf("%d",&newnode->a);
	printf("enter the name : ");
	scanf(" %s",newnode->n);
	newnode->next=NULL;
	if(last==NULL)
	{
		last=newnode;
		newnode->next=last;
	}
	else
	{
		newnode->next=last->next;
		last->next=newnode;
	}
	last=newnode;
}

void display()
{
	struct node *temp;
	if(last == NULL)
	{
		printf("list is empty\n");
	}
	else {
		for(temp=last->next;temp!=last;temp=temp->next)
		{
			printf("%d %s\n",temp->a,temp->n);
		}
		printf("%d %s\n",temp->a,temp->n);
	}
}
VN:F [1.9.14_1148]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.14_1148]
Rating: 0 (from 0 votes)

20. February 2012 by divya
Categories: Data Structures | Leave a comment

← Older posts

Newer posts →