Untitled
unknown
plain_text
a year ago
2.4 kB
10
Indexable
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *link;
};
struct node *head;
struct node *temp;
struct node *head = NULL;
struct node *create(struct node *head){
struct node *temp;
struct node *new;
int data;
printf("Enter data (-1 to stop): ");
scanf("%d", &data);
while (data != -1) { // Accept values until user enters -1
new = (struct node*)malloc(sizeof(struct node));
if (new == NULL) {
printf("Memory allocation failed!\n");
return head;
}
new->data = data;
new->link = NULL;
if (head == NULL) {
head = new; // First node
temp = head;
} else {
temp->link = new;
temp = new;
}
printf("Enter data (-1 to stop): ");
scanf("%d", &data);
}
return head;
}
void display(struct node *head){
struct node *temp;
temp = head;
if(temp == NULL){
printf("No list Found!");
}
else{
while(temp->link != NULL){
printf("%d->", temp->data);
temp = temp->link;
}
temp->link = NULL;
}
}
void alternate(struct node *head){
struct node *temp;
temp = head;
if(temp == NULL){
printf("No list Found!");
}
else{
while(temp->link != NULL){
printf("%d->", temp->data);
temp = temp->link->link;
}
temp->link = NULL;
}
}
// void InsertAtBegin(struct node *head){
// struct node *temp , *new;
// new = (struct node *)malloc(sizeof(struct node));
// printf("enter data:");
// scanf("%d",&new->data);
// // new->data=data;
// new->link = head;
// head=new;
// //return head;
// }
int main(){
int ch;
printf("Enter choice: ");
do{
printf("1.create:\n");
printf("2.display:\n");
printf("3.alternate:\n");
printf("4.InsertAtBegin:\n");
printf("0.exit:\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
head = create(head);
break;
case 2:
display(head);
break;
case 3:
alternate(head);
break;
case 4:
InsertAtBegin(head);
break;
case 0:
printf("Exiting...\n");
break;
Default:
Printf("Invalid option:");
break;
}
} while(ch!=0);
return 0;
}
Editor is loading...
Leave a Comment