0% found this document useful (0 votes)
6 views3 pages

Include

The document contains a C program that implements a circular linked list with functions to insert, delete, and display nodes. It defines a structure for the nodes and includes operations to manage the list, such as inserting at the beginning and deleting the first item. The main function demonstrates the insertion of several nodes and the deletion of all items from the list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Include

The document contains a C program that implements a circular linked list with functions to insert, delete, and display nodes. It defines a structure for the nodes and includes operations to manage the list, such as inserting at the beginning and deleting the first item. The main function demonstrates the insertion of several nodes and the deletion of all items from the list.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

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

struct node {
int data;
int key;

struct node *next;


};

struct node *head = NULL;


struct node *current = NULL;

bool isEmpty() {
return head == NULL;
}

int length() {
int length = 0;

//if list is empty


if(head == NULL) {
return 0;
}

current = head->next;

while(current != head) {
length++;
current = current->next;
}

return length;
}

//insert link at the first location


void insertFirst(int key, int data) {

//create a link
struct node *link = (struct node*) malloc(sizeof(struct
node));
link->key = key;
link->data = data;

if (isEmpty()) {
head = link;
head->next = head;
} else {
//point it to old first node
link->next = head;
//point first to new first node
head = link;
}
}

//delete first item


struct node * deleteFirst() {

//save reference to first link


struct node *tempLink = head;

if(head->next == head) {
head = NULL;
return tempLink;
}

//mark next to first link as first


head = head->next;

//return the deleted link


return tempLink;
}

//display the list


void printList() {

struct node *ptr = head;


printf("\n[ ");

//start from the beginning


if(head != NULL) {

while(ptr->next != ptr) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
}

printf(" ]");
}

void main() {
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);

printf("Original List: ");


//print list
printList();

while(!isEmpty()) {
struct node *temp = deleteFirst();
printf("\nDeleted value:");
printf("(%d,%d) ",temp->key,temp->data);
}

printf("\nList after deleting all items: ");

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy