Slip 20_2, Slip 29_2 : There are lists where new elements are always appended at the end of the list. The list can be implemented as a circular list with the external pointer pointing to the last element of the list. Implement singly linked circular list of integers with append and display operations. The operation append(L, n), appends to the end of the list, n integers accepted from user.

 Solution :

#include<stdio.h>

#include<stdlib.h>

struct node

{

 int data;

 struct node *next,*prev;

};

struct node *f;

void create()

{

 struct node *s;

 int i,n;

 printf("enter how many nodes");

 scanf("%d",&n);

 f=(struct node*)malloc(sizeof(struct node));

 printf("enter data");

 scanf("%d",&f->data);

 s=f;

 for(i=1;i<n;i++)

 {

 s->next=(struct node*)malloc(sizeof(struct node));

 s=s->next;

 printf("enter data");

 scanf("%d",&s->data);

 }

 s->next=f;

}

void display()

{

 struct node *s;

 printf("\nCircular linked list is::");

 s=f;

 do

 {

 printf("%d->",s->data);

 s=s->next;

 }

 while(s!=f);

}

void append()

{

 struct node *nw,*s;

 int n,i;

 printf("\nenter how many new nodes");

 scanf("%d",&n);

 for(i=0;i<n;i++)

 {

nw=(struct node*)malloc(sizeof(struct node));

 printf("\nenter new node of data");

 scanf("%d",&nw->data);


 s=f;

 do

 {

 s=s->next;

 }while(s->next!=f);

 s->next=nw;

 nw->next=f;

}

}

main()

{

 create();

 display();

 append();

 display();

}

Comments

Popular posts from this blog

Slip 22_2: Read the data from file 'cities.txt' containing names of cities and their STD codes. Accept a name of the city from user and use sentinel linear search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”. Solution :

Slip10_2, 30_1 : Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using bubble sort or selection sort

Slip 23_2: Read the data from file ‘sortedcities.txt’ containing sorted names of cities and their STD codes. Accept a name of the city from user and use binary search algorithm to check whether the name is present in the file and output the STD code, otherwise output “city not in the list”.