Write a C Program to perform following operations on Circular Linked List ADT

4 years ago
C Programming
  1. Create ii. Insert iii.        Delete             iv.        Display

 

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

 

struct cslinklist

{

int data;

struct cslinklist *next;

};

typedef struct cslinklist NODE; NODE *start = NULL;

int nodectr; NODE* getnode()

{

node * newnode;

newnode = (node *) malloc(sizeof(node)); printf("\n Enter data: ");

scanf("%d", &newnode -> data); newnode -> next = NULL;

return newnode;

}

 

int menu()

{

int ch; system("sys");

 

 

 

 

printf("\n 1. Create a list "); printf("\n\n                                                   ");

printf("\n 2. Insert a node at beginning "); printf("\n 3. Insert a node at end");

 

printf("\n\n                                ");

printf("\n 5. Delete a node from beginning"); printf("\n 6. Delete a node from Last");

 

printf("\n\n                                "); printf("\n 8. Display the list"); printf("\n 9. Exit");

printf("\n\n                                ");

 

printf("\n Enter your choice: "); scanf("%d", &ch);

return ch;

}

 

 

void createlist(int n)

{

int i;

NODE *newnode,*temp; nodectr = n;

 

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

{

newnode = getnode(); if(start == NULL)

{

start = newnode;

}

else

{

 

 

 

 

temp = start;

while(temp -> next != NULL) temp = temp -> next;

temp -> next = newnode;

}

}

newnode ->next = start; /* last node is pointing to starting node */

}

 

 

void display()

{

NODE *temp;

 

temp = start;

printf("\n The contents of List (Left to Right): "); if(start == NULL )

printf("\n Empty List"); else

{

do

{

printf("\t %d ", temp -> data); temp = temp -> next;

} while(temp != start);

 

printf(" X ");

}

}

 

void cll_insert_beg()

{

NODE *newnode, *last; newnode = getnode(); if(start == NULL)

 

 

 

 

{

start = newnode; newnode -> next = start;

}

else

{

last = start;

while(last -> next != start) last= last -> next; newnode -> next = start; start = newnode;

last -> next = start;

}

printf("\n Node inserted at beginning.."); nodectr++;

}

 

 

void cll_insert_end()

{

NODE *newnode, *temp; newnode = getnode(); if(start == NULL )

{

start = newnode; newnode -> next = start;

}

else

{

temp = start;

while(temp -> next != start) temp = temp -> next;

 

temp -> next = newnode; newnode -> next = start;

 

 

 

 

}

printf("\n Node inserted at end.."); nodectr++;

}

 

void cll_delete_beg()

{

NODE *temp, *last; if(start == NULL)

{

printf("\n No nodes exist.."); getch();

return ;

}

else

{

last = temp = start;

 

while(last -> next != start) last= last -> next;

 

start = start -> next; last -> next = start; free(temp); nodectr--;

 

printf("\n Node deleted.."); if(nodectr == 0)

start = NULL;

}

}

 

void cll_delete_last()

{

NODE *temp,*prev;

 

 

 

 

if(start == NULL)

{

printf("\n No nodes exist.."); getch();

return ;

}

else

{

temp = start; prev = start;

while(temp -> next != start)

{

prev = temp;

temp = temp -> next;

}

prev -> next = start; free(temp); nodectr--;

 

if(nodectr == 0)

start = NULL; printf("\n Node deleted..");

}

}

 

void main(void)

{

int result; int ch, n;

system ("sys"); while(1)

{

ch = menu(); switch(ch)

{

 

 

 

 

 

case 1 :

 

if(start == NULL)

{

 

 

 

 

 

 

 

 

 

case 2 :

 

 

 

 

}

else break;

 

printf("\n Enter Number of nodes to create: "); scanf("%d", &n);

createlist(n); printf("\nList created..");

 

 

printf("\n List is already Exist..");

 

 

 

case 3 :

 

 

case 4 :

 

 

case 5 :

 

 

case 6 :

 

 

case 7 :

 

}

 

cll_insert_beg(); break;

 

cll_insert_end(); break;

 

cll_delete_beg(); break;

 

cll_delete_last(); break;

 

display(); break;

 

exit(0);

 

getch();

}

}

 

 

 

 

Implement Stack using List C Programming.

 

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

 

struct node

{

int data;

struct node *next;

};

typedef struct node NODE; NODE *start = NULL;

int menu()

{

int ch; system("cls");

printf("\n 1.Create a list "); printf("\n                                                ");

printf("\n 2. PUSH ");

printf("\n 3. POP");

printf("\n 4. Displaying the list"); printf("\n 5. Quit");

printf("\n--------------------- ");

 

printf("\n\n Enter your choice: "); scanf("%d",&ch);

return ch;

}

 

 

 

 

NODE* getnode()

{

NODE * newnode;

 

newnode = (NODE *) malloc(sizeof(NODE)); printf("\n Enter data: ");

scanf("%d", &newnode -> data); newnode -> next = NULL;

return newnode;

}

 

 

void createlist(int n)

{

int i;

NODE *newnode, *temp;

 

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

{

newnode = getnode(); if(start == NULL)

{

 

 

}

else

{

 

 

 

 

}

}

}

 

start = newnode;

 

 

 

temp = start;

while(temp -> next != NULL) temp = temp -> next;

temp -> next = newnode;

 

 

void display()

 

 

 

 

{

NODE *temp;

 

temp = start;

printf("\n The contents of List (Left to Right): \n"); if(start == NULL)

{

printf("\n Empty List"); return;

}

else

{

while(temp != NULL)

{

printf("%d-->", temp -> data); temp = temp -> next;

}

}

}

 

void push()

{

NODE *newnode; newnode = getnode(); if(start == NULL)

{

start = newnode;

}

else

{

newnode -> next = start; start = newnode;

}

}

 

 

 

 

 

 

void pop()

{

NODE *temp;

 

if(start == NULL)

{

printf("\n No nodes are exist.."); return ;

}

else

{

temp = start;

start = temp -> next;

printf("\n Node deleted %d", temp->data); free(temp);

}

}

 

void main(void)

{

int ch, n;

 

while(1)

{

ch = menu(); switch(ch)

{

 

case 1:

 

if(start == NULL)

{

 

printf("\n Number of nodes you want to create: "); scanf("%d", &n);

createlist(n);

printf("\n List created..");

 

 

 

 

 

 

 

 

 

case 2:

 

 

case 3:

 

 

case 4:

 

}

else

 

break;

 

push(); break;

 

pop(); break;

 

 

printf("\n List is already created..");

 

display(); break;

 

 

case 5:

 

}

 

exit(0);

 

getch();

}

}

More related questions

Questions Bank

View all Questions