#include "stdio.h"
#include "stdlib.h"
struct node {
int data;
struct node* next;
};
int length(struct node *head){
struct node* current = head;
int count = 0;
while ( current != NULL ){
count++;
current = current -> next;
}
return count;
}
void display(struct node *head) {
struct node* current = head;
int count = 0;
while ( current != NULL ){
printf("%d \t", current -> data);
current = current -> next;
}
printf("\n \n");
}
void delete ( struct node* head, int pos) {
int k = 0;
struct node* current = head;
for( k = 0; k < pos; k++){
current = current -> next;
}
current -> next = current -> next -> next;
}
void add ( struct node* head, int pos, int data) {
int k = 0;
struct node* current = head;
struct node* newNode = malloc(sizeof(struct node));
for( k = 0; k < pos; k++){
current = current -> next;
}
newNode -> next = current -> next;
newNode -> data = data;
current -> next = newNode;
}
int main(){
int k = 0;
int m = 0;
int i =0;
int pos = 3;
struct node* head = NULL;
struct node* current = NULL;
struct node* nextCur = NULL;
head = malloc (sizeof (struct node));
current = malloc (sizeof (struct node));
head -> data = 1;
head -> next = current;
current -> next = NULL;
for(i = 0; i < 5; i++){
current -> data = i;
nextCur = malloc (sizeof (struct node));
current -> next = nextCur;
current = nextCur;
}
current -> data = i;
current -> next = NULL;
display(head);
delete (head, pos);
display(head);
add (head, pos, 10);
display(head);
getch();
return 0;
}
#include "stdlib.h"
struct node {
int data;
struct node* next;
};
int length(struct node *head){
struct node* current = head;
int count = 0;
while ( current != NULL ){
count++;
current = current -> next;
}
return count;
}
void display(struct node *head) {
struct node* current = head;
int count = 0;
while ( current != NULL ){
printf("%d \t", current -> data);
current = current -> next;
}
printf("\n \n");
}
void delete ( struct node* head, int pos) {
int k = 0;
struct node* current = head;
for( k = 0; k < pos; k++){
current = current -> next;
}
current -> next = current -> next -> next;
}
void add ( struct node* head, int pos, int data) {
int k = 0;
struct node* current = head;
struct node* newNode = malloc(sizeof(struct node));
for( k = 0; k < pos; k++){
current = current -> next;
}
newNode -> next = current -> next;
newNode -> data = data;
current -> next = newNode;
}
int main(){
int k = 0;
int m = 0;
int i =0;
int pos = 3;
struct node* head = NULL;
struct node* current = NULL;
struct node* nextCur = NULL;
head = malloc (sizeof (struct node));
current = malloc (sizeof (struct node));
head -> data = 1;
head -> next = current;
current -> next = NULL;
for(i = 0; i < 5; i++){
current -> data = i;
nextCur = malloc (sizeof (struct node));
current -> next = nextCur;
current = nextCur;
}
current -> data = i;
current -> next = NULL;
display(head);
delete (head, pos);
display(head);
add (head, pos, 10);
display(head);
getch();
return 0;
}
No comments:
Post a Comment