还是不行#include <stdio.h>
#include <stdlib.h>
struct Node{
int num;
struct Node *next;
};
void function(void);
void ds_init(struct Node **head);
int ds_insert(struct Node **head, int i);
void ds_delete(struct Node **head, int i);
int ds_search(struct Node *head, int i);
void print(struct Node *head);
int ClerrList(struct Node **head);
void function(void){
printf("1.初始化链表\n");
printf("2.插入结点\n");
printf("3.删除结点\n");
printf("4.查找结点\n");
printf("5.遍历链表\n");
printf("0.退出\n");
}
void ds_init(struct Node **head){
int i;
struct Node *node;
struct Node *temp;
printf("输入结点的值,输入-1完成初始化\n");
while(1){
scanf("%d", &i);
if (i == -1){
return ;
}
if (!(*head)){// !(*head) --> *head == NULL
*head = (struct Node *)malloc(sizeof(struct Node));
if(!(*head)){
exit(1);
}
(*head)->num = i;
(*head)->next = *head;
}
else{
for (node = (*head); node->next != (*head); node = node->next)
;
temp = (struct Node *)malloc(sizeof(struct Node));
if(!(*head)){;
exit(1);
}
temp->num = i;
temp->next = *head;
node->next = temp;
}
}
}
int ds_insert(struct Node **head, int i){
int j, k = 1;
struct Node *node;
struct Node *now;
struct Node *temp;
printf("请输入要插入的结点的值");
scanf("%d", &j);
if (i == 1){//新插入的结点作为第一个结点
now = (struct Node *)malloc(sizeof(struct Node));
if(!now){
exit(1);
}
now->num = j;
for (node = *head; node->next != *head; node = node->next)//找到最后一个结点
;
now->next = *head;
node->next = now;//temp放到head前面
*head = now; //head要指向第一个结点
}
else{
node = *head;
while ( ++k < i){
node = node->next;
}
now = (struct Node *)malloc(sizeof(struct Node));
if(!now){
exit(1);
}
now->num = j;
temp = node->next;
node->next = now;
now->next = temp;
}
return j;
}
void ds_delete(struct Node **head, int i){
int j = 1;
struct Node *node;
struct Node *temp;
if (i == 1){//删除的第一个结点
for (node = *head; node->next != (*head); node = node->next)//找到最后一个结点
;
temp = *head;
*head = (*head)->next;
node->next = *head;
free(temp);
}
else{
node = *head;
while ( ++j < i){
node = node->next;
}
temp = node->next;
node->next = temp->next;
free(temp);
}
}
int ds_search(struct Node *head, int i){
int j = 1;
struct Node *node;
for (node = head; node->next != head && node->num != i; j++){//找到最后一个结点
node = node->next;
}
if (node->next == head)
return 0;
else
return j;
}
void print(struct Node *head){//遍历
struct Node *node;
int i = 0;
node = head;
do{
printf("%d ", node->num);
node = node->next;
}while (node != head);
putchar('\n');
}
int ClerrList(struct Node **head){//将单链表清空
struct Node *node, *temp;
node = *head;
while (node->next){
temp = node;
node = node->next;
free(temp);
/*temp = node->next;
free(node);
node = temp;*/
}
if (node)
(*head)->next = NULL;
else
return 0;
return 1;
}
int main(){
char i;
int j;
struct Node *head = NULL;
function();
while (1){
printf("请输入指令:");
scanf("%c", &i);
switch (i){
case '1': ds_init(&head);
print(head);break;
case '2': printf("请输入要插入的位置:");
scanf("%d", &j);
printf("再位置%d插入%d后:", j, ds_insert(&head, j));
print(head);break;
case '3': printf("请输入要删除的位置:");
scanf("%d", &j);
ds_delete(&head, j);
printf("删除第%d个结点后:", j);
print(head);break;
case '4': printf("请输入查找的值:");
scanf("%d", &j);
printf("元素%d所在的位置:%d\n", j, ds_search(head, j));break;
case '5': print(head);break;
case '0': ClerrList(&head);exit(1);
}
}
return 0;
}
|