C语言链表的使用,帮我写个非常简单的就可以!
只要能说明怎么使用就可以!
易于理解!
多谢大哥大姐了!
C语言链表的使用,帮我写个非常简单的就可以!
只要能说明怎么使用就可以!
易于理解!
多谢大哥大姐了!
#include<stdio.h>
#include<malloc.h>
#define N sizeof(stu)
typedef struct students
{
int num;
struct students *next;
}stu;
int i,j;
void insertValue(stu *head);
void deleteValue(stu *head);
int main()
{
stu *p,*head;
head=(stu *)malloc(N);
p=(stu *)malloc(N);
p->num=7;
p->next=NULL;
head->next=p;
p=(stu *)malloc(N);
p->num=25;
p->next=NULL;
head->next->next=p;
p=head->next;
LL:
printf("\n------潇洒菠菜插入删除链表练习------\n");
printf("\t1.对现有链表的插入。\n");
printf("\t2.对现有链表的删除。\n");
printf("\t3.显示现有链表。\n");
printf("\t4.退出程序。\n");
printf("请选择<1-4>:");
scanf("%d",&i);
if(i==3)
{
p=head->next;
printf("\n现有链表如下:\n");
while(p!=NULL)
{
printf("%d\n",p->num);
p=p->next;
}
}
if(i==4)
{
printf("\n您的宝贵建议是我成功制胜的关键!\n");
printf("\t\t谢谢使用!\n");
return 0;
}
if(i==1)
insertValue(head);
if(i==2)
deleteValue(head);
fflush(stdin);
goto LL;
}
void insertValue(stu *head)
{
int k;
stu *p1,*p2;
p1=head;
p2=(stu *)malloc(N);
printf("输入插入位置:");
scanf("%d",&i);
i--;
k=i;
while(p1!=NULL&&i>0)
{
p1=p1->next;
i--;
}
printf("输入插入的值:");
scanf("%d",&j);
p2->num=j;
if(k==0){
p2->next=head->next;
head->next=p2;
}else{
p2->next=p1->next;
p1->next=p2;
}
i=0;
printf("插入成功啦!...\n\n");
}
void deleteValue(stu *head)
{
stu *p1,*p2;
j=1;
p1=head->next;
printf("\n请输入删除的位置:");
scanf("%d",&i);
i--;
if(i==0)
{
head->next=head->next->next;
printf("删除成功啦!...\n");
}
else{
while(p1!=NULL||j<i)
{
p1=p1->next;
j++;
}
if(j!=i)
{
printf("删除位置错误\n");
}else
{
p1->next=p1->next->next;
printf("删除成功啦!...\n");
}
}
}
这个是我先前写的一个线性链表
对链表进行插入和删除操作!
更简单的就是拼接char*
比如!!!
c 字符串拼接
#include
#include
int main(void)
{
char fullpath [100] = "/home/dimgtui"; //记录路径名
printf("fullpath:%s ",fullpath);
char * ptr;//记录fullpath的最后位置,用于拼接
ptr = fullpath + strlen(fullpath);
*ptr ++ = '/';
*ptr = 0;
char * filename = "filename1";
strcpy(ptr,filename);
printf("fullpath:%s ",fullpath);
//恢复之前的fullpath
ptr[-1] = 0;
printf("fullpath:%s ",fullpath);
exit(0);
}
#include <stdio.h> #include <conio.h> #include <stdlib.h>
#define HEAD struct listhead #define NODE struct listnode
void createList (HEAD *); int insertNode (HEAD *, NODE *, int); int emptyList (HEAD *); int getNext (HEAD *, int, int *); void printList (HEAD *); void destroyList (HEAD *); char menu(); void removeNode (HEAD *, int); void deleteNode (HEAD *, NODE *, NODE *, int); int searchList (HEAD *, NODE **, NODE **, int); int getdata();
HEAD { int count; NODE *pos; NODE *head; };
NODE { int data; NODE *link; };
void createList (HEAD *list) { list->count = 0; list->pos = NULL; list->head = NULL; }
int getdata() { int InsertData; printf("\nEnter the key to be inserted:"); scanf("%d", &InsertData); return InsertData; }
int insertNode (HEAD *list, NODE *pPre, int dataIn) { NODE *pNew; pNew = (NODE *)malloc(sizeof(NODE));
if (pNew == NULL) return 0;
pNew->data = dataIn; if (pPre == NULL) { pNew->link = list->head; list->head = pNew; } else { pNew->link = pPre->link; pPre->link = pNew; } list->count ++; return 1; }
int emptyList(HEAD *list) { return (list->count == 0); }
int getNext(HEAD *list, int fromWhere, int *dataOut) { int success; if (fromWhere == 0) if (emptyList(list) == 1) success = 0; else { list->pos = list->head; *dataOut = list->pos->data; success = 1; } else if (list->pos->link == NULL) success = 0; else { list->pos = list->pos->link; *dataOut = list->pos->data; success = 1; } return success; }
void printList(HEAD *list) { int count, moreData; int dataPtr; if (emptyList(list) == 1) printf("\nNo data in the list.\n"); else { printf("\n\nBegin data print...\n"); count = 0; moreData = getNext(list, 0, &dataPtr); while (moreData == 1) { count ++; printf(" %d: %d\n", count, dataPtr); moreData = getNext(list, 1, &dataPtr); } printf("End data print...\n"); } }
void destroyList (HEAD *list) { NODE *dltPtr; while (list->count > 0) { dltPtr = list->head; list->head = dltPtr->link; list->count --; free(dltPtr); } list->pos = NULL; }
char menu() { int valid, choice; printf("\n---Menu---\n"); printf("\n"); printf("A: Add new data\n"); printf("D: Delete data\n"); printf("P: Print List\n"); printf("Q: Quit\n"); printf("\n"); valid = 0; while (valid == 0) { printf("Enter your choice:"); choice = getch(); printf("\n"); if (strchr("AaDdPpQq", choice) != NULL) valid = 1; else printf("\nInvalid choice. Choices are <A, D, P, Q>:"); } return choice; }
void removeNode (HEAD *list, int dataOut) { int found; NODE *pPre, *pLoc; found = searchList(list, &pPre, &pLoc, dataOut); if (found) { deleteNode(list, pPre, pLoc, dataOut); printf("Data:%d was removed !",dataOut); } else printf("Data:%d not found, remove node fail!",dataOut); }
void deleteNode (HEAD *list, NODE *pPre, NODE *pLoc, int dataOut) { dataOut = pLoc->data; if (pPre == NULL) list->head = pLoc->link; else pPre->link = pLoc->link; list->count --; free(pLoc); }
int searchList(HEAD *list, NODE **pPre, NODE **pLoc, int target) { *pPre = NULL; *pLoc = list->head; while (*pLoc != NULL && target != (*pLoc)->data) { *pPre = *pLoc; *pLoc = (*pLoc)->link; } if (*pLoc == NULL) return 0; else if (target == (*pLoc)->data) return 1; else return 0; }
void main() { HEAD *list1; NODE *pPre, *pLoc; int dataIn; char option; int deleteKey; clrscr(); list1 = (HEAD *)malloc(sizeof(HEAD)); createList(list1); option = ' '; while (strchr("Qq", option) == NULL) { option = menu(); switch (option) { case 'a': case 'A': dataIn = getdata(); insertNode(list1, NULL, dataIn); break; case 'd': case 'D': printf("\nEnter the key to be deleted:"); scanf("%d", &deleteKey); removeNode(list1, deleteKey); break; case 'p': case 'P': printList(list1); break; } printf("\n"); } destroyList(list1); printf("\nPress any key to return..."); getch(); }
有哪儿不懂,来我们团队问吧~