最近闲暇之际,重新看了一下C语言和数据结构,感觉链表很有意思,按照书上的思路自己写了一个重新,实现单向链表的操作,支持增加,删除,查找,读文件操作。先自己保留着,以后用的时候参考。
/*
* @author: lizhenbin
* @date: 2011-09-14
* @descrption: list operation
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define NULL 0
/* define a link node strut */
struct student
{
long num;
struct student *prior; /* define point to the struct */
struct student *next;
};
/* create struct */
struct student *create()
{
struct student *head; /* link head and node point */
struct student *p;
struct student *q;
long x;
head = (struct student*)malloc(sizeof(struct student));
q = head;
head->next = NULL;
printf("\nPlease input node numbers to the list: \n");
scanf("%ld",&x);
while(x!=0)
{
/* create node */
p = (struct student*)malloc(sizeof(struct student));
p->num = x;
p->next = NULL;
q->next = p;
p->prior = q;
q = p;
scanf("%ld",&x);
}
return head;
}
/* Output list all datas */
void print(struct student *head)
{
struct student *p;
p = head->next;
printf("\nThe list is: ");
if(p==NULL)
{
printf("\nInput list is NULL!");
}
while(p!=NULL)
{
printf("%ld ", p->num);
p = p->next;
}
printf("\n");
}
/* delete list node */
void del(struct student *head)
{
struct student *p;
struct student *q;
int i;
int j=0;
printf("Please input delete position: ");
scanf("%d", &i);
if(i<1)
{
printf("You input %d error.", i);
printf("\nTry again, Input nnum: ");
scanf("%d", &i);
}
p = head;
while(j<=i-1 && p!=NULL)
{
p = p->next;
j++;
}
if(p==NULL)
{
printf("\nThere is no %d in the list.", i);
exit(1);
}
else
{
/* delete list node */
q = p;
p->prior->next = p->next;
p->next->prior = p->prior;
p = p->next;
free(q);
}
}
/* insert list node */
void insert(struct student *head)
{
struct student *p;
struct student *q = head;
int i;
int j = 0;
p = (struct student*)malloc(sizeof(struct student));
printf("\nPlease input the student No you want to insert: ");
scanf("%ld", &p->num);
printf("\nInput the student %d position to insert: ",p->num);
scanf("%d", &i);
if(i<1)
{
printf("\nYou input %d error.", i);
printf("\nTry again, Input nnum: ");
scanf("%d", &i);
}
while(j<=i-1 && q->next!=NULL)
{
q = q->next;
j++;
}
if(q->next==NULL && j!=i)
{
p->next = NULL;
p->prior = q;
q->next = p;
}
else
{
q->prior->next = p;
p->prior = q->prior;
p->next = q;
q->prior = q;
}
}
/* search list node position and data */
void search(struct student *head)
{
struct student *p;
long number, i=1, j=0;
printf("\nPlease input the index you want to search: ");
scanf("%d", &number);
p = head->next;
printf("\nThe number in the list position is: ");
while(p!=NULL)
{
if(p->num==number)
{
printf("%d\n",i);
j = 1;
}
p = p->next;
i++;
}
if(j==0)
{
printf("There is no %ld in the list.\n",number);
}
}
/* read data from file input */
struct student *filein()
{
struct student *p1;
struct student *p2;
struct student *head;
int i = 0;
FILE *fp;
char file[20] = "d:/list/node.txt";
printf("\nFileName is: %s\n", file);
if((fp=fopen(file, "r"))==NULL)
{
printf("Can not open file: %s", file);
exit(0);
}
p2 = (struct student*)malloc(sizeof(*p1));
head = p2;
p1 = (struct student*)malloc(sizeof(*p1));
while(fread(p1, sizeof(*p1), 1, fp)==1)
{
i++;
p2->next = p1;
p2 = p1;
p1->next = NULL;
p1 = (struct student*)malloc(sizeof(*p1));
}
free(p1);
fclose(fp);
if(i==0)
{
printf("\nNo recored in %s ", file);
}
return head;
}
/* save the list result to file */
void savefile(struct student* head)
{
struct student *p1;
FILE *fp;
char file[20] = "d:/list/node.txt";
printf("\nFileName: %s", file);
printf("\nPlease wait.../n");
if((fp=fopen(file,"w"))==NULL)
{
printf("\nCan not open file: %s", file);
exit(0);
}
p1 = head->next;
while(p1!=NULL)
{
fwrite(p1, sizeof(struct student), 1, fp);
p1 = p1->next;
}
fclose(fp);
}
/* print the operate menu */
void showMenu()
{
printf("|-------------------------------------------------|\n");
printf("| Link List Opearte Menu |\n");
printf("|-------------------------------------------------|\n");
printf("| 1: Insert |\n");
printf("| 2: Delete |\n");
printf("| 3: Search |\n");
printf("| 4: FileIn |\n");
printf("| 5: Savefile |\n");
printf("| 6: Print List |\n");
printf("| 7: Print Menu |\n");
printf("| 8: Exit |\n");
printf("|-------------------------------------------------|\n");
}
/* main funtion */
void main()
{
int a=0;
struct student *la;
la = create();
/*
la = (struct student*)malloc(sizeof(struct student));
la->next = NULL;
*/
while(1)
{
showMenu();
scanf("\n%d", &a);
switch(a)
{
case 1: insert(la); break;
case 2: del(la); break;
case 3: search(la); break;
case 4: filein(); break;
case 5: savefile(la); break;
case 6: print(la); break;
case 7: showMenu(); break;
case 8: exit(1);
default: printf("\nInput error,Try again.");break;
}
}
}
分享到:
相关推荐
选择合适的存储方式实现线性表。其中,必须实现的线性表基本操作为:InitList、 ClearList、ListEmpty、ListLength、GetElem、PriorElem、ListInsert、ListDelete这8个基本操作,其余的可以选作。
C语言实现链表通讯录 主要就是链表的应用实例 入门用的 仔细阅读能有不少收获哦 加油~~
C语言实现链表操作,包括增加节点,删除节点,显示链表
C语言实现链表基本操作
C语言实现链表HuffmanTree
用c语言实现链表的合并,是数据结构中重要的基础实验,调试后可以运行
用c语言实现链表排序,利用选择排序的思想,可以供大家学习。
C语言实现 链表操作,链表 建立 输出 删除 插入,整个程序代码,看完后会让你对链表操作有更深的体悟
c语言实现链表,链表是数据结构第一课,希望对你有帮助 实现链表
VC6.0下 用C语言实现单向链表的创建、插入,删除节点,和2个链表合并等操作
链表综合系统链表综合系统链表综合系统链表综合系统
C语言实现多种链表快速排序
c语言实现链表的基本操作,链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态...
这篇文档介绍了C语言中链表的实现,一点个人心得,希望对你能有所帮助。。。
这是利用c语言中的链表来解决的问题,有利于你对C语言链表的更好的了解
window平台,用clion实现c语言链表
自己写的,能够使用,对c语言的指针用的淋漓精致,有需要的小伙伴可以下载看下
是用C语言来实现对链表的各种操作,数据结构课程一般会用到。保证程序正确性