Skip to content

Commit 85e7db7

Browse files
committed
Lecture 5 codes
1 parent 818be5d commit 85e7db7

14 files changed

+371
-0
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef __DLL_H
2+
#define __DLL_H
3+
4+
typedef struct Node* NodePtr;
5+
typedef int ElementType;
6+
7+
struct Node {
8+
ElementType Elem;
9+
NodePtr Next;
10+
NodePtr Prev;
11+
};
12+
13+
#endif
14+
15+
void DeleteNode(NodePtr P)
16+
{
17+
P->Prev->Next = P->Next;
18+
P->Next->Prev = P->Prev;
19+
free(P);
20+
}

05-LinkedList/.vscode/Stack.h

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#ifndef __STACK_H
2+
#define __STACK_H
3+
4+
#include "StackLL.h"
5+
6+
Stack CreateStack();
7+
8+
void Push(Stack S,ElementType Elem);
9+
void Pop(Stack S);
10+
ElementType Top(const Stack S);
11+
12+
bool IsEmpty(const Stack S);
13+
14+
void ClearStack(Stack S);
15+
void DeleteStack(Stack** S);
16+
17+
for(i = 0 ; i < 10 ; i++)
18+
for(j = 0 ; j < 3 ;j++)
19+
{
20+
if( j > 4)
21+
{
22+
if( i > 3)
23+
{
24+
;
25+
}
26+
}
27+
for(k=9 ; k >=0 ; k--)
28+
{
29+
;
30+
}
31+
}
32+
}
33+
34+
35+
#endif
36+
37+
// Function Call Stack
38+
// Undo/Redo
39+
// Most recently used list
40+
// Intellisense (parantheses matching, code indentation)
41+
// Expression evaluation and Syntax Parsing
42+
// Recursion / Backtracking
43+
44+
// Parantheses matching ()[]{} ...
45+
// Error
46+
/// 1. Create Empty Stack
47+
/// 2. while(notEndOfFile(FP))
48+
/// {
49+
/*
50+
p = ReadChar()
51+
if(openingSymbol(p))
52+
PushOntoStack(S,p)
53+
if(closingSymbol(p))
54+
if(!empty(S))
55+
{
56+
if(matching(Top(S),p))
57+
Pop(S)
58+
else
59+
ERROR // [()}
60+
*/
61+
/// }
62+
/// else
63+
// ERROR // [{}]}()
64+
/// }
65+
/// if(!empty(S)) [(()])}
66+
/// ERROR
67+
///}
68+
///}
69+
// advanced /* comments */
70+
71+
// Expression Evaluation
72+
// Using two stacks - one for operands, one for operators
73+
// x/2 + (a+b)/c

05-LinkedList/.vscode/StackArray.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef __STACKARRAY_H
2+
#define __STACKARRAY_H
3+
4+
typedef int ElementType;
5+
const int STACK_MAX_ELEMS = 8000;
6+
typedef struct StackInfo* Stack;
7+
8+
struct StackInfo {
9+
ElementType StackArr[STACK_MAX_ELEMS];
10+
int Top;
11+
};
12+
13+
#endif

05-LinkedList/.vscode/StackLL.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef __STACK_LINKEDLIST_H
2+
#define __STACKL_LINKEDLIST_H
3+
4+
typedef int ElementType;
5+
6+
struct Node;
7+
typedef struct Node* NodePtr;
8+
typedef NodePtr Stack;
9+
10+
struct Node {
11+
ElementType Elem;
12+
NodePtr Next;
13+
};
14+
15+
#endif

05-LinkedList/.vscode/launch.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "(lldb) Launch",
9+
"type":"cppdbg",
10+
"request": "launch",
11+
"program": "${cwd}/Polynomial",
12+
/*"program": "${fileDirname}/${fileBasenameNoExtension}",*/
13+
"args": [],
14+
"stopAtEntry": false,
15+
"cwd": "${workspaceFolder}",
16+
"environment": [],
17+
"externalConsole": false,
18+
"MIMode": "lldb",
19+
/*"miDebuggerPath":"/usr/local/bin/gdb", */
20+
"preLaunchTask": "GNU gcc build active file"
21+
}
22+
]
23+
}

05-LinkedList/.vscode/tasks.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"tasks": [
3+
{
4+
"type": "shell",
5+
"label": "GNU gcc build active file",
6+
"command": "/usr/local/bin/gcc-10",
7+
"args": [
8+
"-g",
9+
"${cwd}/*.c",
10+
"${cwd}/*.h",
11+
"-o",
12+
"Polynomial"
13+
/*"${fileDirname}/${fileBasenameNoExtension}"*/
14+
],
15+
"options": {
16+
"cwd": "${workspaceFolder}"
17+
},
18+
"problemMatcher": [
19+
"$gcc"
20+
],
21+
"group": "build",
22+
"detail": "compiler: /usr/local/bin/gcc-10"
23+
}
24+
],
25+
"version": "2.0.0"
26+
}

05-LinkedList/DoublyLinkedList.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef __DLL_H
2+
#define __DLL_H
3+
4+
typedef struct Node* NodePtr;
5+
typedef int ElementType;
6+
7+
struct Node {
8+
ElementType Elem;
9+
NodePtr Next;
10+
NodePtr Prev;
11+
};
12+
13+
#endif
14+
15+
/*
16+
void DeleteNode(NodePtr P)
17+
{
18+
P->Prev->Next = P->Next;
19+
P->Next->Prev = P->Prev;
20+
free(P);
21+
}
22+
*/

05-Stack/.vscode/launch.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "(lldb) Launch",
9+
"type":"cppdbg",
10+
"request": "launch",
11+
"program": "${cwd}/Stack",
12+
/*"program": "${fileDirname}/${fileBasenameNoExtension}",*/
13+
"args": [],
14+
"stopAtEntry": false,
15+
"cwd": "${workspaceFolder}",
16+
"environment": [],
17+
"externalConsole": false,
18+
"MIMode": "lldb",
19+
/*"miDebuggerPath":"/usr/local/bin/gdb", */
20+
"preLaunchTask": "GNU gcc build active file"
21+
}
22+
]
23+
}

05-Stack/.vscode/tasks.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"tasks": [
3+
{
4+
"type": "shell",
5+
"label": "GNU gcc build active file",
6+
"command": "/usr/local/bin/gcc-10",
7+
"args": [
8+
"-g",
9+
"${cwd}/*.c",
10+
"${cwd}/*.h",
11+
"-o",
12+
"Stack"
13+
/*"${fileDirname}/${fileBasenameNoExtension}"*/
14+
],
15+
"options": {
16+
"cwd": "${workspaceFolder}"
17+
},
18+
"problemMatcher": [
19+
"$gcc"
20+
],
21+
"group": "build",
22+
"detail": "compiler: /usr/local/bin/gcc-10"
23+
}
24+
],
25+
"version": "2.0.0"
26+
}

05-Stack/Stack.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef __STACK_H
2+
#define __STACK_H
3+
4+
#include "StackArr.h"
5+
6+
Stack InitStack(unsigned long int max_items);
7+
8+
void Push(Stack S,ElementType E);
9+
void Pop(Stack S);
10+
ElementType Top(const Stack S);
11+
12+
int IsEmpty(const Stack S);
13+
void ClearStack(Stack S);
14+
void DeleteStack(Stack* S);
15+
16+
#endif

05-Stack/StackArr.c

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "Stack.h"
2+
#include <stdlib.h>
3+
#include <assert.h>
4+
#include <stdio.h>
5+
6+
Stack InitStack(unsigned long int max_items)
7+
{
8+
Stack S;
9+
S = (Stack)malloc(sizeof(struct StackInfo));
10+
assert(S != NULL);
11+
12+
S->MaxNumItems = max_items;
13+
S->StackItem = (ElementType*)malloc(S->MaxNumItems*sizeof(ElementType));
14+
assert(S->StackItem != NULL);
15+
16+
S->TopId = -1;
17+
18+
return S;
19+
}
20+
21+
void Push(Stack S,ElementType E)
22+
{
23+
if(S->TopId < S->MaxNumItems)
24+
{
25+
++S->TopId;
26+
S->StackItem[S->TopId] = E; // CopyItem(S,TopId,E)
27+
}
28+
}
29+
30+
void Pop(Stack S)
31+
{
32+
if(!IsEmpty(S))
33+
--S->TopId;
34+
}
35+
36+
ElementType Top(const Stack S)
37+
{
38+
if(!IsEmpty(S))
39+
return S->StackItem[S->TopId];
40+
41+
}
42+
43+
44+
int IsEmpty(const Stack S)
45+
{
46+
return (S->TopId == -1);
47+
}
48+
49+
void ClearStack(Stack S)
50+
{
51+
S->TopId = -1;
52+
}
53+
54+
void DeleteStack(Stack* SP)
55+
{
56+
if(*SP)
57+
{
58+
free((*SP)->StackItem);
59+
free(*SP);
60+
*SP = NULL;
61+
}
62+
}

05-Stack/StackArr.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef __STACKARR_H
2+
#define __STACKARR_H
3+
4+
typedef double ElementType;
5+
6+
typedef struct StackInfo* Stack;
7+
8+
struct StackInfo {
9+
long int MaxNumItems;
10+
long int TopId;
11+
ElementType *StackItem;
12+
};
13+
14+
#endif

05-Stack/StackLL.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef __STACK_LINKEDLIST_H
2+
#define __STACKL_LINKEDLIST_H
3+
4+
typedef double ElementType;
5+
6+
struct Node;
7+
typedef struct Node* NodePtr;
8+
typedef NodePtr Stack;
9+
10+
struct Node {
11+
ElementType Elem;
12+
NodePtr Next;
13+
};
14+
15+
#endif

05-Stack/main.c

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
#include "Stack.h"
3+
4+
int main(void)
5+
{
6+
Stack S;
7+
8+
int max_elems = 10000;
9+
S = InitStack(max_elems);
10+
11+
Push(S,10);
12+
Push(S,20);
13+
Push(S,30);
14+
Push(S,40);
15+
16+
Pop(S);
17+
Pop(S);
18+
Pop(S);
19+
20+
DeleteStack(&S);
21+
22+
return 0;
23+
}

0 commit comments

Comments
 (0)