#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 5
typedef struct stuNode
{
char ID[15];
char name[10];
char sex[4];
char grade[5];
char major[20];
struct stuNode *next;
}stuNode, * stuNodeP;
stuNodeP headStu;
#define LEN sizeof(stuNode)
stuNodeP Create_StuList(int n)
{
int i;
stuNodeP headStu;
stuNodeP p0, p1;
headStu = p0 = (stuNode *)malloc(LEN);
for (i = 1; i <= n; i++)
{
p1 = (stuNode *)malloc(LEN);
printf("請輸入學生的學號,姓名,性別,班級,專業:\n");
scanf("%s%s%s%s%s",p1->ID,p1->name,p1->sex,p1->grade,p1->major);
p0->next = p1;
p0 = p1;
}
p1->next = NULL;
return headStu;
}
stuNodeP delete_StuList(stuNodeP headStu, char delID[15])
{
stuNodeP lp=0, p=0;
lp = headStu;
p = lp->next;
while ((strcmp(p->ID, delID) != 0) && (p->next != NULL))
{
lp = p;
p = p->next;
}
if (strcmp(p->ID, delID) == 0)
{
lp->next = p->next;
free(p);
}
else
printf("沒有查找到該名學生,無法洗掉!\n");
return headStu;
}
stuNodeP add(stuNodeP headStu, char newID[15])
{
stuNodeP p = headStu, q = headStu->next;
stuNodeP newstuNode;
while((strcmp(q->ID, newID) < 0) && (q->next != NULL));
{
p = p->next;
q = q->next;
}
newstuNode = (stuNode *)malloc(sizeof(LEN));
strcpy(newstuNode->ID, newID);
printf("請輸入學生的學號,姓名,性別,班級,專業:");
scanf("%s%s%s%s%s",newstuNode->ID, newstuNode->name, newstuNode->sex, newstuNode->grade, newstuNode->major);
if (strcmp(q->ID, newID) < 0)
p = q;
newstuNode->next = p->next;
p->next = newstuNode;
return headStu;
}
void show(stuNodeP headStu)
{
int num=0;
stuNode *p;
p = headStu->next;
while (p != NULL)
{
printf("%d:%s,%s,%s,%s,%s\n",++num, p->ID, p->name, p->sex, p->grade, p->major);
p = p->next;
}
}
int main()
{
stuNodeP head;
char ID[15];
head = Create_StuList(N);
printf("請輸入要洗掉學生的學號資訊:\n");
scanf("%s", &ID);
head = delete_StuList(headStu, ID);
head = add(headStu,ID);
show(headStu);
return 0;
}
uj5u.com熱心網友回復:
有個報錯,引發了例外: 讀取訪問權限沖突。
**lp** 是 nullptr。
uj5u.com熱心網友回復:
資料結構對單鏈表進行資料排序 http://bbs.csdn.net/topics/392201633uj5u.com熱心網友回復:
謝謝老師了~uj5u.com熱心網友回復:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 5
typedef struct stuNode
{
char ID[15];
char name[10];
char sex[4];
char grade[5];
char major[20];
struct stuNode *next;
}stuNode, * stuNodeP;
stuNodeP headStu;
#define LEN sizeof(stuNode)
stuNodeP Create_StuList(int n)
{
int i;
stuNodeP headStu;
stuNodeP p0, p1;
headStu = p0 = (stuNode *)malloc(LEN);
for (i = 1; i <= n; i++)
{
p1 = (stuNode *)malloc(LEN);
printf("請輸入學生的學號,姓名,性別,班級,專業:\n");
scanf("%s%s%s%s%s",p1->ID,p1->name,p1->sex,p1->grade,p1->major);
p0->next = p1;
p0 = p1;
}
p1->next = NULL;
return headStu;
}
stuNodeP delete_StuList(stuNodeP headStu, char delID[15])
{
stuNodeP lp=0, p=0;
lp = headStu;
p = lp->next;
puts(delID);
while (p && strcmp(p->ID, delID) != 0) {
//puts(p->ID);
lp = p;
p = p->next;
}
if (!p) {
printf("沒有查找到該名學生,無法洗掉!\n");
return headStu;
}
lp->next = p->next;
free(p);
/*
while ((strcmp(p->ID, delID) != 0) && (p->next != NULL))
{
lp = p;
p = p->next;
}
if (strcmp(p->ID, delID) == 0)
{
lp->next = p->next;
free(p);
}
else
printf("沒有查找到該名學生,無法洗掉!\n");
*/
return headStu;
}
/* 添加有序,但是創建鏈表的時候并沒有按照有序操作的,所以這里不能保證一直有意義*/
stuNodeP add(stuNodeP headStu, char newID[15])
{
stuNodeP p = headStu, q = headStu->next;
stuNodeP newstuNode;
newstuNode = (stuNode *)malloc(sizeof(LEN));
strcpy(newstuNode->ID, newID);
printf("請輸入學生的學號,姓名,性別,班級,專業:");
scanf("%s%s%s%s%s",newstuNode->ID, newstuNode->name, newstuNode->sex, newstuNode->grade, newstuNode->major);
while (q && strcmp(q->ID, newID) < 0) {
p = p->next;
q = q->next;
}
if (!q) {
p->next = newstuNode;
newstuNode->next = NULL;
return headStu;
}
newstuNode->next = q;
p->next = newstuNode;
/*
//while((strcmp(q->ID, newID) < 0) && (q->next != NULL)); //多一個分號
while((strcmp(q->ID, newID) < 0) && (q->next != NULL))
{
p = p->next;
q = q->next;
}
newstuNode = (stuNode *)malloc(sizeof(LEN));
strcpy(newstuNode->ID, newID);
printf("請輸入學生的學號,姓名,性別,班級,專業:");
scanf("%s%s%s%s%s",newstuNode->ID, newstuNode->name, newstuNode->sex, newstuNode->grade, newstuNode->major);
if (strcmp(q->ID, newID) < 0)
p = q;
newstuNode->next = p->next;
p->next = newstuNode;
*/
return headStu;
}
void show(stuNodeP headStu)
{
int num=0;
stuNode *p;
p = headStu->next;
while (p != NULL)
{
printf("%d:%s,%s,%s,%s,%s\n",++num, p->ID, p->name, p->sex, p->grade, p->major);
p = p->next;
}
}
int main()
{
stuNodeP head;
char ID[15];
head = Create_StuList(N);
show(head);
printf("請輸入要洗掉學生的學號資訊:\n");
//scanf("%s", &ID);
scanf("%s", ID);
//head = delete_StuList(headStu, ID);
head = delete_StuList(head, ID);
show(head);
//head = add(headStu,ID);
head = add(head,ID);
show(head);
return 0;
}
供參考~
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/248222.html
標籤:C語言
上一篇:GDB腳本問題
