#include <malloc.h>
typedef struct list_t
{
int id;
struct list_t *prior;
struct list_t *next;
}LIST;
void add_node(int n, LIST *p)
{
LIST *head, *pr, *pp;
int i;
pr = head; p = head->next;
while(p != NULL && p -> id < n) {
pr = p;
p = p -> next;
}
if((pp = (LIST *)malloc(sizeof(LIST))) == NULL) {
printf("Insertion is not success!");
} else {
pp -> id = n;
pp -> next = pr->next;
pr -> next = pp;
}
}
void remove_node(int id)
{
LIST * head, *p1, *p2;
int n;
if(head == NULL) {
printf("\nlist null !\n");
}
p1 = head;
while(id != p1 -> id && p1 -> next != NULL) {
p2 = p1;
p1 = p1 -> next;
}
if(id == p1 -> id) {
if(p1 == head) {
head = p1 -> next;
} else {
p2 -> next = p1 -> next;
printf("delete: %d\n", id);
n = n-1;
}
} else {
printf("%d not been found !\n", id);
}
}
int display(LIST *head)
{
int i = 0;
LIST *p;
p = head->next;
printf("Out the id : ");
while(p != NULL)
{
printf(" %d",p -> id);
i++;
p = p-> next;
}
printf("\n");
return (i);
}
void menu(void)
{
printf("\t\t\n");
printf("\t\t[A]Inserted the id !\n");
printf("\t\t[R]Delete the id !\n");
printf("\t\t[D]Display the id !\n");
printf("\t\t[Q]The program over !\n");
printf("\t\t\n");
printf("\t\t\n");
printf("\t\tPlease input your choose : A R D Q !\n");
}
int main(void)
{
int id;
int choose;
while(1)
menu();
scanf("%d",&choose);
switch(choose) {
case 'A':
printf("\nInput the inserted id %d", id);
scanf("%d",id);
add_node('A');
break;
case 'R':
printf("\nInput the delete id %d", id);
scanf("%d",id);
remove_node('R');
break;
case 'D':
printf("\nInput the display id %d", id);
scanf("%d",id);
display('R');
break;
case 'Q':
default:printf("ERROR!\n");
}
return 0;
}
有很多错误,功能也不能实现
----------------解决方案--------------------------------------------------------
先发一贴,下次再来看,现在没时间……
----------------解决方案--------------------------------------------------------
等到下次的时候我想我的纳期就过了,哎呀。郁闷
----------------解决方案--------------------------------------------------------
void add_node(int n, LIST *p) { LIST *head, *pr, *pp; int i; pr = head; p = head->next; while(p != NULL && p -> id < n) { pr = p; p = p -> next; } if((pp = (LIST *)malloc(sizeof(LIST))) == NULL) { printf("Insertion is not success!"); } else { pp -> id = n; pp -> next = pr->next; pr -> next = pp; } } 看出了点问题,没有初值啊
----------------解决方案--------------------------------------------------------
head 的值从哪来啊?
运行能通过吗?
----------------解决方案--------------------------------------------------------
不能通过啊,所以要请教啊,说实话我对链表真的很头疼,我都把我自己给弄晕了
编制具有下列结构的链表。(使用参考书学习链表的内容)
typedef struct list_t{
int id; (识别结点的值)
struct list_t *prev; (指向前一个结点的指针)
struct list_t *next; (指向后一个结点的指针)
} LIST;
链表的各结点是从表的前头,以id值小到大的顺序链接起来的。
编写在此链表中,增加结点的函数(add_node())和删除结点(remove_node())的函数。
各自的函数式样如下:
void add_node(int id, LIST *p);
功能:&#12539;从表的前面起,按顺序比较各结点的id和通过参数指定的id。
如发现有比参数指定的id大的id时,在其结点的前面插入指定的结点。
&#12539;表中,没有被链接的结点时,在表的前头联接被指定的结点。
&#12539;比较id,达到表的最后时,在表的最后联接被指定的结
void remove_node(int id);
功能:从表中删除以参数指定的结点。
并且,使用上述函数,编写下列程序,
首先,选择表操作(Add(追加):A、Remove(删除):R、Display(显示):D、剿了(结束):Q)的菜单。
;选择A时,输入将要增加的id,追加到表上,
选择R时,输入将要删除的id,从表中删除。
选择D时,显示表中的所有id.
;选择Q时,程序结束。
进行对于表的上述处理后,再次显示选择表操作的菜单。
尚且,链接表的各结点要通过malloc()函数确保,通过free()函
这是这到题的式样书,请帮忙
----------------解决方案--------------------------------------------------------
语法错误.
----------------解决方案--------------------------------------------------------
请改正,休整完整
----------------解决方案--------------------------------------------------------
没事干,帮你看看!
----------------解决方案--------------------------------------------------------
#include <stdio.h> #include <malloc.h>
typedef struct list_t { int id; struct list_t *prior; struct list_t *next; }LIST;
LIST *head;
void add_node(int n, LIST *p) { LIST *pr, *pp;
if(p=NULL) {p=(LIST *)malloc(sizeof(LIST)); p->id=n; p->prior=p->next=NULL; } else if(p->id>n) { pr=(LIST *)malloc(sizeof(LIST)); pr->next=p; p->prior=pr; pr->prior=NULL ; p=pr; } else { pr =p->next; while(pr!=NULL && pr->id < n) pr =pr->next ; pp = (LIST *)malloc(sizeof(LIST)); pp->next=pr; pp->prior=pr->prior; pp->prior->next=pp; pr->prior=pp; } }
void remove_node(int id) { LIST *p1,*p2;
if(head == NULL) { printf("\nlist null !\n"); return; } p1 = head; while(id != p1 -> id && p1 -> next != NULL) { p1 = p1 -> next; } if(id == p1 -> id) { if(p1 == head) { head = p1 -> next;p1->prior=head; } else { p2 = p1 ->prior; p1->next->prior=p2; p2->next=p1->next; printf("delete: %d\n", id); free(p1);
} } else { printf("%d not been found !\n", id); } }
int display(LIST *head) {
LIST *p;
p = head; printf("Out the all id : "); while(p != NULL) { printf("%d\t",p->id);
p = p->next; } printf("\n");
} void menu(void) {
printf("\t\t\n"); printf("\t\t[1]Inserted the id !\n"); printf("\t\t[2]Delete the id !\n"); printf("\t\t[3]Display the id !\n"); printf("\t\t[4]The program over !\n"); printf("\t\t\n"); printf("\t\t\n"); printf("\t\tPlease input your choose : 1 2 3 4 !\n"); }
int main(void) { int id,f=1, choose; head=NULL; while(f) { menu(); scanf("%d",&choose); getchar(); switch(choose) { case 1: printf("\nInput the inserted id :"); scanf("%d",&id); add_node(id,head); break; case 2: printf("\nInput the delete id: "); scanf("%d",id); remove_node(id); break; case 3: display(head); break; case 4:f=0; break; default:printf("ERROR!\n");
}
} getch(); return 0;
}
----------------解决方案--------------------------------------------------------