Untitled
unknown
plain_text
10 months ago
1.5 kB
19
Indexable
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
char x;
struct node *next;
};
struct node *head;
void createlist()
{
head = (struct node*)malloc(sizeof(struct node));
struct node *n1 = (struct node*)malloc(sizeof(struct node));
struct node *tail = (struct node*)malloc(sizeof(struct node));
head->data = 10;
head->x = 'a';
head->next = n1;
n1->data = 20;
n1->x = 'b';
n1->next = tail;
tail->data = 30;
tail->x = 'c';
tail->next = NULL;
}
void print() {
struct node *temp = head;
while (temp != NULL) {
printf("Element: %c - %d\n", temp->x, temp->data);
temp = temp->next;
}
printf("\n");
}
void insert_beg() {
struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = 5;
newnode->x = 'm';
newnode->next = head;
head = newnode;
}
void end() {
struct node *newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = 40;
newnode->x = 'd';
//newnode->next = NULL;
struct node *temp = head;
while (temp->next!= NULL)
{
temp = temp->next;
}
temp->next = newnode;
newnode->next = NULL;
}
int main() {
createlist();
printf("Before inserting\n");
print();
printf("After insering at the begining\n");
insert_beg();
print();
printf("After inserting at the end\n");
end();
print();
return 0;
}
Editor is loading...
Leave a Comment