93
2.c
Normal file
93
2.c
Normal file
@@ -0,0 +1,93 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct Student student;
|
||||
struct Student {
|
||||
int sid;
|
||||
struct Student *next;
|
||||
};
|
||||
|
||||
void insert(student **head, int idx, uint sid) {
|
||||
student *new = malloc(sizeof(student));
|
||||
new->sid = sid;
|
||||
|
||||
int i = 0;
|
||||
|
||||
student *prev = NULL, *cur = *head;
|
||||
|
||||
for (int i = 0;; i++) {
|
||||
if (i == idx) {
|
||||
if (prev != NULL)
|
||||
prev->next = new;
|
||||
else
|
||||
*head = new;
|
||||
new->next = cur;
|
||||
return;
|
||||
}
|
||||
|
||||
if (cur == NULL)
|
||||
return;
|
||||
|
||||
prev = cur;
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
printf("Index %d does not exist\n", idx);
|
||||
return;
|
||||
}
|
||||
|
||||
void delete (student **head, int idx) {
|
||||
student *prev = NULL, *cur = *head;
|
||||
|
||||
for (int i = 0; cur != NULL; i++) {
|
||||
if (i == idx) {
|
||||
if (prev != NULL)
|
||||
prev->next = cur->next;
|
||||
else
|
||||
*head = cur->next;
|
||||
free(cur);
|
||||
return;
|
||||
}
|
||||
prev = cur;
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
printf("Index %d does not exist\n", idx);
|
||||
return;
|
||||
}
|
||||
|
||||
void reverse(student **head) {
|
||||
student *prev = NULL, *cur = *head;
|
||||
|
||||
while (cur->next != NULL) {
|
||||
student *next = cur->next;
|
||||
cur->next = prev;
|
||||
prev = cur;
|
||||
cur = next;
|
||||
}
|
||||
|
||||
cur->next = prev;
|
||||
|
||||
*head = cur;
|
||||
return;
|
||||
}
|
||||
|
||||
int main() {
|
||||
student *head = NULL;
|
||||
insert(&head, 0, 1);
|
||||
insert(&head, 1, 2002);
|
||||
insert(&head, 2, -9);
|
||||
insert(&head, 3, 52);
|
||||
insert(&head, 1, 4);
|
||||
delete (&head, 2);
|
||||
reverse(&head);
|
||||
|
||||
while (head != NULL) {
|
||||
printf("%d ", head->sid);
|
||||
head = head->next;
|
||||
}
|
||||
|
||||
// free
|
||||
while (head != NULL)
|
||||
delete(&head, 0);
|
||||
}
|
70
4.c
Normal file
70
4.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct Node node;
|
||||
struct Node {
|
||||
int collegeid;
|
||||
struct Node *next;
|
||||
};
|
||||
|
||||
void insert(node **last, int collegeid) {
|
||||
node *dst = malloc(sizeof(node));
|
||||
dst->collegeid = collegeid;
|
||||
|
||||
if (*last == NULL)
|
||||
*last = dst;
|
||||
|
||||
if ((*last)->next == NULL)
|
||||
(*last)->next = dst, dst->next = *last;
|
||||
else {
|
||||
dst->next = (*last)->next;
|
||||
(*last)->next = dst;
|
||||
}
|
||||
}
|
||||
|
||||
void delete (node **last) {
|
||||
node *prev = NULL;
|
||||
|
||||
if ((*last)->next == NULL)
|
||||
goto cleanup;
|
||||
|
||||
prev = *last;
|
||||
|
||||
while (prev->next != *last)
|
||||
prev = prev->next;
|
||||
|
||||
if (prev->next->next == prev)
|
||||
prev->next = NULL;
|
||||
else
|
||||
prev->next = (*last)->next;
|
||||
|
||||
cleanup:
|
||||
free(*last);
|
||||
*last = prev;
|
||||
}
|
||||
|
||||
void display(node *last) {
|
||||
node *cur = last;
|
||||
while (cur->next != last) {
|
||||
printf("%d ", cur->next->collegeid);
|
||||
cur = cur->next;
|
||||
}
|
||||
printf("%d\n", last->collegeid);
|
||||
}
|
||||
|
||||
int main() {
|
||||
node *last = NULL;
|
||||
|
||||
insert(&last, 1);
|
||||
insert(&last, 4);
|
||||
insert(&last, 99);
|
||||
insert(&last, 1223);
|
||||
display(last);
|
||||
|
||||
delete (&last);
|
||||
display(last);
|
||||
|
||||
// free
|
||||
while (last != NULL)
|
||||
delete (&last);
|
||||
}
|
Reference in New Issue
Block a user