所以我写了这个未完成但正在运行的代码。我是一个非常新的编程,我认为我的逻辑是错误的(虽然代码运行正常),因为有太多的" ifs和elses"我想不出另一个工作逻辑,将数据插入到链表的任何位置,并输出数据+错误处理,如果位置无效我试图开发计算思维 并且有些东西告诉我这段代码可以写得更好
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next;
};
struct node* head = NULL;
void Insert(int c ,int e)
{
int i;
static int p = 1;
struct node* temp = malloc(sizeof(struct node));
temp -> data = c;
temp -> next = NULL;
struct node* temp1 = head;
if (e > p+1)
{
printf("Invalid Position");
return;
}
else if (head == NULL)
{
head = temp;
return;
}
else if (e <= 1)
{
temp -> next = head;
head = temp;
return;
}
else if (e == 2)
{
if (temp1 -> next != NULL)
{
temp -> next = temp1 -> next;
temp1 -> next = temp;
}
else
{
temp1 -> next = temp;
}
}
else
{
for (i = 0;i<e-2;i++)
{
temp1 = temp1 -> next;
}
temp -> next = temp1 -> next;
temp1 -> next = temp;
}
p++;
}
void print() {
struct node* temp = head;
printf("list is: \n");
while (temp != NULL) {
printf( "%d ,",temp->data);
temp = temp->next;
}
}
int main () {
printf("How Many Numbers?\n");
int a ,b ,c, e;
scanf("%d" , &b);
for(a = 0;a<b;a++) {
printf("Enter the numbers \n");
scanf("%d",&c);
printf("Enter position \n");
scanf("%d",&e);
Insert(c , e);
print();
}
return 0;
}
答案 0 :(得分:0)
是的,您可以通过将两个条件合并为一个来降低深度:
if (e > p + 1) {
printf("Invalid Position");
return;
}
else if (head == NULL) {
head = temp;
return;
}
else if (e <= 1) {
temp->next = head;
head = temp;
return;
}
else if (e == 2 && temp1->next != NULL) {
temp->next = temp1->next;
temp1->next = temp;
}
else if (temp1->next == NULL) {
temp1->next = temp;
}
else {
for (i = 0; i < e - 2; i++) {
temp1 = temp1->next;
}
temp->next = temp1->next;
temp1->next = temp;
}
答案 1 :(得分:0)
确定if和else是不是一个好的模式设计,但你应该在C中使用它。当你使用OOP(面向对象编程)语言时,你可以改变你的设计。
在C中你应该有一个模块化的代码,所以只需要用尽可能多的函数来编写代码的较小部分。
答案 2 :(得分:0)
您的insert
功能可以简化一点。基本上你需要考虑三个条件:
- 将第一个节点插入空列表;
- 插入新的第一个节点;和
- 在第一个和最后一个之间插入一个新节点。
醇>
最后插入是默认值,无需特别考虑。单独编码每个,您可以按如下方式重写insert
:
void insert (int c, int e)
{
struct node *temp = malloc (sizeof *temp);
if (!temp) {
fprintf (stderr, "error: virtual memory exhausted.\n");
exit (EXIT_FAILURE);
}
temp->data = c;
temp->next = NULL;
if (!head) { head = temp; goto check; } /* empty list */
struct node *iter = head;
if (!e) { temp->next = head; head = temp; return; } /* new 1st node */
for (--e; e && iter->next; iter = iter->next, e--) {} /* iterate */
/* inserting before end, update temp->next */
if (iter->next) temp->next = iter->next;
iter->next = temp; /* add node as next */
check:;
if (e)
fprintf (stderr, "warning, 'e' beyond list, inserted at end.\n");
}
注意: insert
代替Insert
。 C避免Caps
和caMelCase
变量和函数名称支持所有小写。参见例如NASA - C Style Guide, 1994
如果要为列表分配内存,则应跟踪已分配的内存,以便在不再需要时释放内存。尽早养成良好的习惯,不要仅仅依靠退出为你做好准备。您已撰写print()
,delete()
几乎相同:
void delete ()
{
struct node *tmp = head;
while (tmp != NULL) {
struct node *victim = tmp;
tmp = tmp->next;
free (victim);
}
}
将所有部分组合在一起并稍微清理剩余的代码,您可以按如下方式重写示例:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
void insert (int c, int e);
void print ();
void delete ();
int main (void) {
int a, b, c, e, rtn;
a = b = c = e = rtn = 0;
printf ("\nDefine list, values and positons (zero based positions)\n\n");
do {
printf (" numer of list elements?: ");
if ((rtn = scanf ("%d", &b)) != 1 || b < 1)
fprintf (stderr, "error: invalid input.\n");
if (rtn == EOF) exit (EXIT_FAILURE);
} while (b < 1);
for (a = 0; a < b; a++) {
do printf ("\n number %2d: ", a + 1);
while ((rtn = scanf ("%d", &c)) != 1 && rtn != EOF);
if (rtn == EOF) exit (EXIT_FAILURE);
do printf (" position : ");
while ((rtn = scanf ("%d", &e)) != 1 && e >= 0 && rtn != EOF);
if (rtn == EOF) exit (EXIT_FAILURE);
insert (c, e);
print ();
}
delete ();
return 0;
}
void insert (int c, int e)
{
struct node *temp = malloc (sizeof *temp);
if (!temp) {
fprintf (stderr, "error: virtual memory exhausted.\n");
exit (EXIT_FAILURE);
}
temp->data = c;
temp->next = NULL;
if (!head) { head = temp; goto check; } /* empty list */
struct node *iter = head;
if (!e) { temp->next = head; head = temp; return; } /* new 1st node */
for (--e; e && iter->next; iter = iter->next, e--) {} /* iterate */
/* inserting before end, update temp->next */
if (iter->next) temp->next = iter->next;
iter->next = temp; /* add node as next */
check:;
if (e)
fprintf (stderr, "warning, 'e' beyond list, inserted at end.\n");
}
void print ()
{
struct node *temp = head;
printf (" current list is: ");
while (temp != NULL) {
printf (" %d", temp->data);
temp = temp->next;
}
putchar ('\n');
}
void delete ()
{
struct node *tmp = head;
while (tmp != NULL) {
struct node *victim = tmp;
tmp = tmp->next;
free (victim);
}
}
示例使用/输出
$ ./bin/llins
Define list, values and positons (zero based positions)
numer of list elements?: 5
number 1: 6
position : 0
current list is: 6
number 2: 7
position : 3
warning, 'e' beyond list, inserted at end.
current list is: 6 7
number 3: 8
position : 0
current list is: 8 6 7
number 4: 9
position : 2
current list is: 8 6 9 7
number 5: 10
position : 3
current list is: 8 6 9 10 7
仔细看看,如果您有任何问题,请告诉我。